Skip to content

Commit c4bea46

Browse files
authored
feat: generate typescript definitions for resolvers (#6)
1 parent af2f02e commit c4bea46

File tree

9 files changed

+1947
-416
lines changed

9 files changed

+1947
-416
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This package allows you to easily develop a GraphQL server in your [nuxt](v3.nux
1010
## Features
1111

1212
- Provides a virtual module `#graphql/schema` from where you can import your schema. Under the hood, it automatically merges multiple schema files together into a complete schema. Moreover, you no longer need to worry about deploying schema `graphql` files.
13+
- Automatically generates typescript definitions for your resolvers.
1314

1415
## Installation
1516

@@ -24,6 +25,31 @@ yarn add @apollo/server graphql @as-integrations/h3 @tobiasdiez/nuxt-graphql-ser
2425
pnpm add @apollo/server graphql @as-integrations/h3 @tobiasdiez/nuxt-graphql-server
2526
```
2627

28+
## Usage
29+
30+
1. Define the GraphQL schema in `.graphql` files located in the `server` folder.
31+
2. Expose the GraphQL API endpoint by creating `server/api/graphql.ts` with the following content:
32+
33+
```ts
34+
import { Resolvers } from '#graphql/resolver'
35+
import { schema } from '#graphql/schema'
36+
import { ApolloServer } from '@apollo/server'
37+
import { startServerAndCreateH3Handler } from '@as-integrations/h3'
38+
39+
const resolvers: Resolvers = {
40+
Query: {
41+
// Typed resolvers
42+
},
43+
}
44+
45+
const apollo = new ApolloServer({typeDefs: schema, resolvers})
46+
47+
export default startServerAndCreateH3Handler(apollo, {
48+
// Optional: Specify context
49+
context: (event) => {...},
50+
})
51+
```
52+
2753
## 💻 Development
2854

2955
- Clone this repository

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"release": "standard-version && git push --follow-tags && pnpm publish"
2828
},
2929
"dependencies": {
30+
"@graphql-codegen/core": "^2.6.2",
31+
"@graphql-codegen/plugin-helpers": "^2.7.1",
32+
"@graphql-codegen/typescript": "^2.8.0",
33+
"@graphql-codegen/typescript-resolvers": "^2.7.5",
3034
"@graphql-tools/graphql-file-loader": "^7.5.5",
3135
"@graphql-tools/load": "^7.8.0",
3236
"@nuxt/kit": "^3.0.0-rc.12"
@@ -35,6 +39,8 @@
3539
"graphql": "^16.6.0"
3640
},
3741
"devDependencies": {
42+
"@apollo/server": "^4.0.4",
43+
"@as-integrations/h3": "^1.1.0",
3844
"@nuxt/module-builder": "^0.2.0",
3945
"@nuxt/schema": "^3.0.0-rc.12",
4046
"@nuxtjs/eslint-config-typescript": "^11.0.0",

playground/server/api/graphql.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Resolvers } from '#graphql/resolver'
2+
import { schema } from '#graphql/schema'
3+
import { ApolloServer } from '@apollo/server'
4+
import { startServerAndCreateH3Handler } from '@as-integrations/h3'
5+
6+
const resolvers: Resolvers = {
7+
Query: {
8+
books: () => {
9+
return [
10+
{
11+
title: 'GraphQL with Nuxt',
12+
},
13+
]
14+
},
15+
},
16+
}
17+
18+
const apollo = new ApolloServer({ typeDefs: schema, resolvers })
19+
20+
export default startServerAndCreateH3Handler(apollo)

playground/server/schema.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
type Query {
2+
books: [Book]
3+
}
4+
15
type Book {
26
title: String
37
author: Author

0 commit comments

Comments
 (0)