Pokko
Searchโ€ฆ
NextJS
Getting started guide for using Pokko with NextJS websites
Pokko provides a GraphQL endpoint for querying content that integrates with any platform that can consume data coming from such an endpoint.
Using the content hierarchy feature of Pokko you can easily map content in Pokko to web pages on your website.
The website for Pokko - pokko.io - is a NextJS website with Typescript connected to Pokko. You can view the source code for the site here.
It's worth noting that the guide below isn't necessarily tied to NextJS, but could apply to any Javascript-based application.

Getting started

The simplest way to connect NextJS to Pokko is to add a thin API layer using the Apollo GraphQL client.
Start by adding the required dependencies.
1
yarn add @apollo/client graphql
2
โ€‹
3
# or, if you are using npm
4
โ€‹
5
npm install --save @apollo/client graphql
Copied!
Then create a file somewhere in your project; we tend to use lib/pokko.ts in our projects.
1
import {
2
ApolloClient,
3
ApolloClientOptions,
4
InMemoryCache,
5
NormalizedCacheObject,
6
} from "@apollo/client";
7
โ€‹
8
const config = {
9
environment: process.env.POK_ENVIRONMENT!,
10
project: process.env.POK_PROJECT!,
11
token: process.env.POK_TOKEN!,
12
};
13
โ€‹
14
const options = (
15
token: string
16
): ApolloClientOptions<NormalizedCacheObject> => ({
17
cache: new InMemoryCache(),
18
โ€‹
19
headers: {
20
"X-Token": token,
21
},
22
โ€‹
23
uri: `https://au-syd1.pokko.io/${config.project}/${config.environment}/graphql`,
24
});
25
โ€‹
26
export const client = new ApolloClient(options(config.token));
Copied!
This expects the environment variables for POK_ENVIRONMENT , POK_PROJECT and POK_TOKEN. These can go into your .env file.
aWith that, you can query content using the client export from this file, it will provide you with a raw GraphQL interface to query content.

Code generation

We like to use a library called GraphQL Codegen that allows us to write GraphQL queries and store them in .graphql files that are then validated against the Pokko API and then output into Typescript files for easier consumption.
It only takes a few moments to configure but will save you tonnes of time in the long run.
Start by installing the dependencies.
1
yarn add --dev @graphql-codegen/cli @graphql-codegen/fragment-matcher @graphql-codegen/typescript @graphql-codegen/typescript-apollo-client-helpers @graphql-codegen/typescript-operations @graphql-codegen/typescript-react-apollo
2
โ€‹
3
# or, with npm
4
โ€‹
5
npm install --save-dev @graphql-codegen/cli @graphql-codegen/fragment-matcher @graphql-codegen/typescript @graphql-codegen/typescript-apollo-client-helpers @graphql-codegen/typescript-operations @graphql-codegen/typescript-react-apollo
Copied!
Then create a codegen.yml file alongside your package.json with the contents below. You can put these files wherever you like, this is just how we set things up.
1
schema:
2
- https://au-syd1.pokko.io/<< PROJECT ID >>/<< ENVIRONMENT ID >>/graphql:
3
headers:
4
X-Token: ${POK_TOKEN}
5
documents:
6
- pokko/queries/*.graphql
7
generates:
8
./pokko/queries.ts:
9
plugins:
10
- typescript
11
- typescript-operations
12
- typescript-react-apollo
13
- fragment-matcher
Copied!
The URL on line 2 can be found by following the steps here, the URL for the "GraphQL Playground" is what will go here.
This will look for .graphql files in the pokko/queries folder and output the generated Typescript to pokko/queries.ts
This also looks for your API key in your environment variables, you can create a .env file with POK_TOKEN=<< API TOKEN >> alongside the codegen.yml file.
With this in place, you can run yarn run graphql-codegen to generate the Typescript file.
There is one extra piece needed if you use data model inheritance, that is to update your lib/pokko.ts file to the following.
1
import {
2
ApolloClient,
3
ApolloClientOptions,
4
InMemoryCache,
5
NormalizedCacheObject,
6
} from "@apollo/client";
7
import intro from "../pokko/queries"; // <-- add this
8
โ€‹
9
const config = {
10
environment: process.env.POK_ENVIRONMENT!,
11
project: process.env.POK_PROJECT!,
12
token: process.env.POK_TOKEN!,
13
};
14
โ€‹
15
const options = (
16
token: string
17
): ApolloClientOptions<NormalizedCacheObject> => ({
18
cache: new InMemoryCache({
19
possibleTypes: intro.possibleTypes, // <-- and this
20
}),
21
โ€‹
22
headers: {
23
"X-Token": token,
24
},
25
โ€‹
26
uri: `https://au-syd1.pokko.io/${config.project}/${config.environment}/graphql`,
27
});
28
โ€‹
29
export const client = new ApolloClient(options(config.token));
Copied!
Last modified 4mo ago