generated from rhdeck/ts-template
/
index.tsx
57 lines (56 loc) · 1.62 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React, { FC } from "react";
import { createAuthLink, AUTH_TYPE, AuthOptions } from "aws-appsync-auth-link";
import { createSubscriptionHandshakeLink } from "aws-appsync-subscription-link";
import { ApolloProvider } from "@apollo/react-common";
import { ApolloLink } from "apollo-link";
import ApolloClient from "apollo-client";
import { createHttpLink } from "apollo-link-http";
import {
InMemoryCache,
IntrospectionFragmentMatcher,
IntrospectionResultData,
} from "apollo-cache-inmemory";
import Auth from "@aws-amplify/auth";
const makeClient = ({
fragmentTypes: introspectionQueryResultData,
graphqlEndpoint,
region,
}: {
fragmentTypes: IntrospectionResultData;
graphqlEndpoint: string;
region: string;
}) => {
const auth: AuthOptions = {
type: "AMAZON_COGNITO_USER_POOLS",
jwtToken: async () =>
(await Auth.currentSession()).getIdToken().getJwtToken(),
};
const config = {
url: graphqlEndpoint,
region,
auth,
};
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData,
});
const httpLink = createHttpLink({ uri: graphqlEndpoint });
const client = new ApolloClient({
link: ApolloLink.from([
createAuthLink(config),
httpLink,
// createSubscriptionHandshakeLink(config, httpLink),
// ,
]),
cache: new InMemoryCache({ fragmentMatcher }),
defaultOptions: {
watchQuery: {
fetchPolicy: "cache-and-network",
},
},
});
const Provider: FC = ({ children }) => (
<ApolloProvider client={client}>{children}</ApolloProvider>
);
return { Provider, client };
};
export default makeClient;