Skip to content

Commit 565a2fa

Browse files
charlypolyijjk
andauthored
refactor(examples/with-typescript-graphql): use codegen TypedDocumentNode and GraphQL Yoga for better DX and smaller bundle size (#36240)
Improve the Next.js with TypeScript + GraphQL example: - [x] use GraphQL Code Generator instead of `graphql-let`: more widespread tool and smaller bundle size (types only generation vs code generation) - [x] use GraphQL Yoga instead of Apollo Server Micro: for lighter bundle size as [stated here](#36155) - [x] introduces GraphQL Code Generator on the API side for Resolvers typing Co-authored-by: JJ Kasper <[email protected]>
1 parent 874957e commit 565a2fa

File tree

20 files changed

+126
-140
lines changed

20 files changed

+126
-140
lines changed

examples/with-typescript-graphql/.babelrc

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/with-typescript-graphql/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ yarn-error.log*
3838
*.graphql.d.ts
3939
*.graphqls.d.ts
4040
.cache
41+
42+
lib/resolvers-types.ts
43+
lib/graphql-operations.ts

examples/with-typescript-graphql/.graphql-let.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/with-typescript-graphql/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
One of the strengths of GraphQL is [enforcing data types on runtime](https://graphql.github.io/graphql-spec/June2018/#sec-Value-Completion). Further, TypeScript and [GraphQL Code Generator](https://graphql-code-generator.com/) (graphql-codegen) make it safer by typing data statically, so you can write truly type-protected code with rich IDE assists.
44

5-
This template extends [Apollo Server and Client Example](https://github.com/vercel/next.js/tree/canary/examples/api-routes-apollo-server-and-client#readme) by rewriting in TypeScript and integrating [graphql-let](https://github.com/piglovesyou/graphql-let#readme), which runs [TypeScript React Apollo](https://graphql-code-generator.com/docs/plugins/typescript-react-apollo) in [graphql-codegen](https://github.com/dotansimha/graphql-code-generator#readme) under the hood. It enhances the typed GraphQL use as below:
5+
This template gives you the best start to use GraphQL with fully typed queries (client-side) and resolvers (server-side), all this with minimum bundle size 📦
66

77
```tsx
8-
import { useNewsQuery } from './news.graphql'
8+
import { useQuery } from '@apollo/client'
9+
import { ViewerDocument } from 'lib/graphql-operations'
910

1011
const News = () => {
11-
// Typed already️⚡️
12-
const { data: { news } } = useNewsQuery()
12+
// Typed already️⚡️
13+
const {
14+
data: { viewer },
15+
} = useQuery(ViewerDocument)
1316

14-
return <div>{news.map(...)}</div>
17+
return <div>{viewer.name}</div>
1518
}
1619
```
1720

18-
By default `**/*.graphqls` is recognized as GraphQL schema and `**/*.graphql` as GraphQL documents. If you prefer the other extensions, make sure the settings of the webpack loader in `next.config.js` and `.graphql-let.yml` are consistent.
19-
2021
## Deploy your own
2122

2223
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-typescript-graphql)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
schema:
2+
- 'lib/schema.ts':
3+
noRequire: true
4+
documents: ./lib/documents/*.graphql
5+
generates:
6+
./lib/graphql-operations.ts:
7+
plugins:
8+
- typescript
9+
- typescript-operations
10+
- typed-document-node
11+
./lib/resolvers-types.ts:
12+
plugins:
13+
- typescript
14+
- typescript-resolvers

examples/with-typescript-graphql/graphql.d.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/with-typescript-graphql/lib/apollo.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
InMemoryCache,
66
NormalizedCacheObject,
77
} from '@apollo/client'
8+
import resolvers from './resolvers'
9+
import typeDefs from './schema'
810

911
let apolloClient: ApolloClient<NormalizedCacheObject> | undefined
1012

@@ -16,7 +18,12 @@ export type ResolverContext = {
1618
function createIsomorphLink(context: ResolverContext = {}) {
1719
if (typeof window === 'undefined') {
1820
const { SchemaLink } = require('@apollo/client/link/schema')
19-
const { schema } = require('./schema')
21+
const { makeExecutableSchema } = require('@graphql-tools/schema')
22+
23+
const schema = makeExecutableSchema({
24+
typeDefs,
25+
resolvers,
26+
})
2027
return new SchemaLink({ schema, context })
2128
} else {
2229
const { HttpLink } = require('@apollo/client')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mutation UpdateName($name: String!) {
2+
updateName(name: $name) {
3+
id
4+
name
5+
status
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
query Viewer {
2+
viewer {
3+
...Partial
4+
status
5+
}
6+
}
7+
8+
fragment Partial on User {
9+
id
10+
name
11+
}

examples/with-typescript-graphql/lib/partial.graphql

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)