-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathgraphql.js
75 lines (70 loc) · 1.67 KB
/
graphql.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// :snippet-start: graphql-client
// 1. Import dependencies
import {
ApolloClient,
ApolloProvider,
HttpLink,
InMemoryCache,
useQuery,
gql,
} from "@apollo/client";
import { useApp } from "../components/useApp";
// 2. Add GraphQL client provider
function GraphQLProvider({ children }) {
const app = useApp();
const client = new ApolloClient({
link: new HttpLink({
uri: process.env.NEXT_PUBLIC_GRAPHQL_API_ENDPOINT,
// We get the latest access token on each request
fetch: async (uri, options) => {
const accessToken = app.currentUser?.accessToken;
options.headers.Authorization = `Bearer ${accessToken}`;
return fetch(uri, options);
},
}),
cache: new InMemoryCache(),
});
return <ApolloProvider client={client}>{children}</ApolloProvider>;
}
// 3. GraphQL query
const GET_PLANT = gql`
query Plant($name: String!) {
plant(query: { name: $name }) {
_id
sunlight
name
color
type
_partition
}
}
`;
// 4. Consumer of provider and query
function PlantInformation({ name }) {
const { loading, error, data } = useQuery(GET_PLANT, {
variables: { name },
});
if (loading || !data) return <p>Loading ...</p>;
if (error) console.error("Failed with error:", error);
return (
<div>
{data.plant ? (
<div>
<p>{data.plant.name}</p>
<p>{data.plant.color}</p>
</div>
) : (
"no plant"
)}
</div>
);
}
// 5. Export page with the GraphQL query
export default function FullGraphQLPage() {
return (
<GraphQLProvider>
<PlantInformation name="daffodil" />
</GraphQLProvider>
);
}
// :snippet-end: