zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.25k stars 620 forks source link

How to attach Context to a Query or Mutation Widget #1280

Closed TesfahunK closed 1 year ago

TesfahunK commented 1 year ago

I have recently ran into a situation where i have to attach custom header for some queries and mutations on my app, the docs indicate that this package support a Context for an operation but fails to give any example, is there any example that shows the use of a Context in Query/Mutation widget? if there is none can you please include that in the docs?

Thanks

wildsurfer commented 10 months ago

@TesfahunK could you share your solution, please?

TesfahunK commented 10 months ago

@wildsurfer Sure , here is my workaround


Context customGraphQueryContext({String? role, Map<String, String>? headers}) {
  return Context.fromList(
    [
      HttpLinkHeaders(
        headers: headers ?? {"x-hasura-role": role!},
      ),
    ],
  );
}

you can use the above Context in your MutationOptions & QueryOptions paramenters of your Query or Mutation widgets