Skip to content

Instantly share code, notes, and snippets.

@umutyerebakmaz
Created September 27, 2024 09:27
Show Gist options
  • Save umutyerebakmaz/ef3a6e2db80ea3b7711e6fb1204e05b4 to your computer and use it in GitHub Desktop.
Save umutyerebakmaz/ef3a6e2db80ea3b7711e6fb1204e05b4 to your computer and use it in GitHub Desktop.
import { HttpLink } from 'apollo-angular/http';
import { ApolloLink, InMemoryCache } from '@apollo/client/core';
import { setContext } from '@apollo/client/link/context';
import { environment } from '@environments/environment';
import { extractFiles } from 'extract-files';
export function createApolloClient(httpLink: HttpLink) {
const uri = `${environment.backendURL}/graphql`;
const auth = setContext((operation, context) => {
const accessToken = localStorage.getItem('accessToken');
return {
...context['headers'],
headers: {
Authorization: accessToken ? `Bearer ${accessToken}` : '',
},
};
});
const link = ApolloLink.from([
auth,
httpLink.create({
uri,
useMultipart: true,
extractFiles,
}),
]);
const cache = new InMemoryCache();
const defaultOptions = {
watchQuery: {
fetchPolicy: 'cache-first',
},
query: {
fetchPolicy: 'cache-first',
},
};
return {
link,
cache,
defaultOptions,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment