0

I am testing out a graphql query in flutter using the graphql_flutter package.

Below is my code executing the query -

final phoneNumbers = ["4160000000", "7780000000"];

    final filters = phoneNumbers.map((phoneNumber) => phoneNumber).toList();

    Map<String, dynamic> filter = {
      "or": filters,
    };

    const String getAzureContacts = """
      query filterItems (\$filter: filters!) {
        items(filter: \$filter) {
          items {
            partitionKey
            userPhoneNumber
            userDisplayName
          } 
        }
      }
    """;

    HttpLink link = GlobalVariables().graphqlEndpoint;
    GraphQLClient graphQlClient = GraphQLClient(
      link: link,
      cache: GraphQLCache(
        store: InMemoryStore(),
      ),
    );
    try {
      QueryResult queryResult = await graphQlClient.query(
        QueryOptions(
            fetchPolicy: FetchPolicy.networkOnly,
            document: gql(
              getAzureContacts,
            ),
            variables: {'filter': filter}),
      );
      print(queryResult.data);
    } catch (e) {
      print('Query failed: $e');
    }

Here is the error after running the code -

I/flutter ( 7103): Query failed: Error on line 6, column 5: Expected a selection set starting with '{'
I/flutter ( 7103):   ╷
I/flutter ( 7103): 6 │     }
I/flutter ( 7103):   │     ^
I/flutter ( 7103):   ╵

Update - April 22nd 2023

I have added curly braces, now I am getting another error.

const String getAzureContacts = """ {
  query(\$filter: filters!) {
    items(filter: \$filter) {
      items {
        partitionKey
        userPhoneNumber
        userDisplayName
      }
    }
  }
}  
""";

I/flutter (11573): Query failed: Error on line 2, column 22: Expected an argument name
I/flutter (11573):   ╷
I/flutter (11573): 2 │   query filterItems ($filter: filters!) {
I/flutter (11573):   │                      ^
I/flutter (11573):   ╵
Sumchans
  • 3,088
  • 6
  • 32
  • 59

1 Answers1

1

The query from graphql package isn't exactly like yours

take care of the "r" before the three single quote and "\" before "$"

https://pub.dev/packages/graphql#query

bk3
  • 41
  • 3
  • Tried your answer I get another error now. I have added the update to the original post. – Sumchans Apr 23 '23 at 03:36
  • https://pub.dev/packages/graphql#query check the doc there some differences with your query – bk3 Apr 24 '23 at 02:38