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

Does this awesome package support headers? #30

Closed
leopku opened this issue Sep 28, 2019 · 1 comment
Closed

Does this awesome package support headers? #30

leopku opened this issue Sep 28, 2019 · 1 comment

Comments

@leopku
Copy link

leopku commented Sep 28, 2019

How to add headers for queries or mutations?
It's useful in auth or something else.

@JustinVoitel
Copy link

You can set the header when creating your ApolloClient. You just need to concat the link attribute with a provided context function. Here an example:

...
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
//like you would normally do

const httpLink = new createHttpLink({
   uri:yourEndpoint
   fetch,
})
//-this function will get called every time
//your client tries to communicate with your graphql endpoint
//-you can also import your svelte stores as shown in this example

const authLink = setContext((_, { headers }) => {
   return {
      headers: {
         ...headers,
         authorization: get(account) ? `Bearer ${get(account).token}` : '',
      },
   }
})
//now use the provided concat method from your context variable

const client = new ApolloClient({
   link: authLink.concat(httpLink)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants