Ashootosh Bhardwaj
Ashootosh Bhardwaj

Reputation: 408

Flutter: Querying AWS AppSync

I recently started using AWS AppSync & Flutter. I am having a bit of hard time to connect them with each other.

I am using graphql_flutter package and haven't been able to figure out how to use it for querying purposes.

Here is my Code Snippet in question:

final HttpLink httpLink = HttpLink(
              uri: 'https://myapi.xyz.com/graphql',
            );


            final AuthLink authLink = AuthLink(
              getToken: () async => 'Bearer ${globals.token}',  
            );

            final Link link = authLink.concat(httpLink);

            GraphQLClient client = GraphQLClient(link: link, cache: InMemoryCache());

            QueryOptions query = QueryOptions(documentNode: gql(queries.getAll));

            var result = await client.query(query);

I am getting below error:

Error: 'await' can only be used in 'async' or 'async*' methods.
            var result = await client.query(query);  

How can I make the whole thing work?

Upvotes: 2

Views: 517

Answers (1)

Akif
Akif

Reputation: 7640

You need to use a Future function while working with async methods. You can change your method like this:


 Future<QueryResult> getRepositories(int numOfRepositories) async {
            final HttpLink httpLink = HttpLink(
              uri: 'https://myapi.xyz.com/graphql',
            );


            final AuthLink authLink = AuthLink(
              getToken: () async => 'Bearer ${globals.token}',  
            );

            final Link link = authLink.concat(httpLink);

            GraphQLClient client = GraphQLClient(link: link, cache: InMemoryCache());

            QueryOptions query = QueryOptions(documentNode: gql(queries.getAll));

            var result = await client.query(query);

           } 
    

You can read more about asynchronous programming from the official documentation.

Upvotes: 1

Related Questions