nhost / nhost-dart

Nhost Dart & Flutter packages
https://nhost.io
MIT License
90 stars 33 forks source link

How to choose user role when doing a request? #126

Closed wildsurfer closed 11 months ago

wildsurfer commented 11 months ago

I have a logged-in user. User has 2 roles: user and me. By default, all requests are going out with the user default role. How to use the me role for a specific request?

final nhostClient = NhostClient(
  subdomain: Subdomain(
    region: 'eu-west-2',
    subdomain: 'absdef...',
  ),
  authStore: SecureStorageProvider(),
);

final graphqlClient = createNhostGraphQLClient(nhostClient);

final QueryOptions options = QueryOptions(
  document: gql(someQuery),
);

final result = await graphqlClient.query(options);
dbarrosop commented 11 months ago

You need to pass the header X-Hasura-Role with the value of the role you want to use. You can refer to https://pub.dev/packages/graphql for more details on how to specify headers in your requests.

Closing as I don't think there is anything to do here but feel free to reopen otherwise.

Thanks!

wildsurfer commented 11 months ago

@dbarrosop thanks for the quick response.

Here is a solution I ended up with. Leaving it here in case anyone will face the same question.

final nhostClient = NhostClient(
  subdomain: Subdomain(
    region: 'eu-west-2',
    subdomain: 'absdef...',
  ),
  authStore: SecureStorageProvider(),
);

final graphqlClient = createNhostGraphQLClient(nhostClient);

final QueryOptions options = QueryOptions(
  document: gql(someQuery),
  context: Context.fromList(const [
    HttpLinkHeaders(headers: {'X-Hasura-Role': 'me'})
  ]),
);