Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add headers per-query? #96

Closed
MarcelloTheArcane opened this issue Feb 11, 2021 · 4 comments
Closed

How to add headers per-query? #96

MarcelloTheArcane opened this issue Feb 11, 2021 · 4 comments
Labels
✨ enhancement New feature or request

Comments

@MarcelloTheArcane
Copy link

With Hasura, you specify user roles with a header called X-Hasura-Role.

I've managed to set the JWT using the example, however that seems to set the header for the whole client, rather than each request.

In Apollo, I'd do something like this:

apollo.query({
  query: // some query
  context: {
    headers: {
      'X-Hasura-Role': 'user'
    }
  }
})
@MarcelloTheArcane
Copy link
Author

Looks like #28 will be helpful. It would mean I need to create a separate client for each role I think

@logaretm
Copy link
Owner

Sounds like you want to provide a custom object to the plugin's function, I think it's simple to add to villus. So would something like this work?

useQuery({
  query: SomeQuery,
  context: { hasuraRole: 'user' }
})

Then you would have a global plugin handling this:

function rolePlugin({ queryContext }) {
  opContext.headers['X-Hasura-Role'] = queryContext.hasuraRole;
}

Not yet sure about the naming but it's reasonable to have.

@MarcelloTheArcane
Copy link
Author

@logaretm thanks for the quick reply!

Yes, that looks great.

@logaretm logaretm added the ✨ enhancement New feature or request label Feb 14, 2021
@logaretm
Copy link
Owner

This was tagged in rc.12, although a little bit different.

It's similar to what you suggested initially:

useQuery({
  query: SomeQuery,
  context: {
    headers: {
      // some headers
    }
  }
})

// can be reactive as well
useQuery({
  query: SomeQuery,
  context: computed(() => { .... })
})
useMutation(SomeMutation, {
  context: {
    headers: {
      // some headers
    }
  }
})

// can be reactive as well
useMutation(SomeMutation, {
  context: computed(() => { .... })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants