-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGithubClient.tsx
40 lines (33 loc) · 1013 Bytes
/
GithubClient.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import {
ApolloClient,
InMemoryCache,
ApolloLink,
HttpLink,
ApolloProvider,
} from '@apollo/client';
import React, { ReactNode } from 'react';
const httpLink = new HttpLink({ uri: 'https://api.github.com/graphql' });
// Create an ApolloLink middleware
const authLink = new ApolloLink((operation, forward) => {
// Get the token from your token storage (e.g., AsyncStorage)
const token = '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';
// Add the Authorization header to the operation
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
Authorization: token ? `Bearer ${token}` : '',
},
}));
return forward(operation);
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
defaultOptions: { watchQuery: { fetchPolicy: 'cache-and-network' } },
});
type props = {
children: ReactNode;
};
export default function GithubApolloProvider({ children }: props) {
return <ApolloProvider client={client}>{children}</ApolloProvider>;
}