function createClient(headers, token, initialState) { let accessToken = token; (async () => { // eslint-disable-next-line no-param-reassign accessToken = token || (await persist.willGetAccessToken()); })(); const authLink = new ApolloLink((operation, forward) => { operation.setContext({ headers: { authorization: accessToken } }); return forward(operation); }).concat(httpLink); return new ApolloClient({ headers, link: authLink, connectToDevTools: process.browser, ssrMode: !process.browser, cache: new InMemoryCache().restore(initialState || {}) }); }
return response; }); }).concat( createHttpLink({ uri: 'http://localhost:13377/graphql',
const queryOrMutationLink = (config = {}) => // turn on CDN support via GET createPersistedQueryLink({ useGETForHashedQueries: true }).concat( new HttpLink({ ...config, credentials: 'same-origin', }) )
split( ({ query }) => { const { kind, operation } = getMainDefinition(query) return kind === 'OperationDefinition' && operation === 'subscription' }, wsLink, authLink.concat(httpLink) )
const queryOrMutationLink = (config = {}) => new ApolloLink((operation, forward) => { operation.setContext({ credentials: 'same-origin', headers: { authorization: `Bearer ${localStorage.getItem('auth_token')}` } }); return forward(operation); }).concat( new HttpLink({ uri: 'http://localhost:5000/graphql', ...config, }) )
componentDidMount() { if (this.props.item) { this.setState({item: this.props.item}) } const authLink = setContext(async (_, {headers}) => { const token = await this.props.auth.getAccessToken(); const user = await this.props.auth.getUser(); // return the headers to the context so httpLink can read them return { headers: { ...headers, authorization: token ? `Bearer ${token}` : '', 'x-forwarded-user': JSON.stringify(user) } } }); this.client = new ApolloClient({ link: authLink.concat(httpLink), cache: new InMemoryCache() }); }
const GraphQL = ({children}) => { const token = useToken(); const accessToken = token ? token.access_token : ''; const httpLink = createHttpLink({ uri: 'http://localhost:8000/graphql/', }); const authLink = setContext((_, { headers }) => { return { headers: { ...headers, authorization: token ? `Bearer ${accessToken}` : "", } } }); const client = new ApolloClient({ link: authLink.concat(httpLink), cache: new InMemoryCache() }); return ( <ApolloProvider client={client}> {children} </ApolloProvider> ); }
export default function createApolloClient(uri, token) { const httpLink = createHttpLink({uri}); if (token) { const authLink = setContext((_, {headers}) => ({ headers: { ...headers, authorization: `Bearer ${token}`, }, })); return new ApolloClient({ link: authLink.concat(httpLink), cache: new InMemoryCache(), }); } else { return new ApolloClient({ link: httpLink, cache: new InMemoryCache(), }); } }
link: authLink.concat(httpLink), cache: new InMemoryCache(), connectToDevTools: true
link: authLink.concat(link) });
constructor(uri) { this.uri = uri const defaultOptions = { query: { fetchPolicy: 'network-only', errorPolicy: 'all', }, } const link = createHttpLink({ uri: this.uri, fetch: fetch }) // eslint-disable-line const cache = new InMemoryCache() const authLink = setContext((_, { headers }) => { // generate the authentication token (maybe from env? Which user?) const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYWRtaW4iLCJuYW1lIjoiUGV0ZXIgTHVzdGlnIiwiYXZhdGFyIjoiaHR0cHM6Ly9zMy5hbWF6b25hd3MuY29tL3VpZmFjZXMvZmFjZXMvdHdpdHRlci9qb2huY2FmYXp6YS8xMjguanBnIiwiaWQiOiJ1MSIsImVtYWlsIjoiYWRtaW5AZXhhbXBsZS5vcmciLCJzbHVnIjoicGV0ZXItbHVzdGlnIiwiaWF0IjoxNTUyNDIwMTExLCJleHAiOjE2Mzg4MjAxMTEsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6MzAwMCIsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDAwMCIsInN1YiI6InUxIn0.G7An1yeQUViJs-0Qj-Tc-zm0WrLCMB3M02pfPnm6xzw' // return the headers to the context so httpLink can read them return { headers: { ...headers, Authorization: token ? `Bearer ${token}` : '', }, } }) this.client = new ApolloClient({ link: authLink.concat(link), cache: cache, defaultOptions, }) }
split( ({ query }) => { const { kind, operation } = getMainDefinition(query) return kind === 'OperationDefinition' && operation === 'subscription' }, wsLink, authLink.concat(httpLink), )
export default function createApolloClient(uri, token) { const httpLink = createHttpLink({ uri }); if (token) { const authLink = setContext((_, { headers }) => { return { headers: { ...headers, authorization: `Bearer ${token}`, }, }; }); return new ApolloClient({ link: authLink.concat(httpLink), cache: new InMemoryCache(), }); } else { return new ApolloClient({ link: httpLink, cache: new InMemoryCache(), }); } }