nhost / nhost-dart

Nhost Dart & Flutter packages

Home Page:https://nhost.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to choose user role when doing a request?

wildsurfer opened this issue · comments

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);

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!

@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'})
  ]),
);