Skip to content

Commit d4c3ef5

Browse files
authored
Add support for Next.js route handlers (#84)
1 parent 64f948f commit d4c3ef5

File tree

10 files changed

+591
-419
lines changed

10 files changed

+591
-419
lines changed

.changeset/new-lemons-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@as-integrations/next': minor
3+
---
4+
5+
Added support for Next.js route handlers

README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ An Apollo Server integration for use with Next.js.
44

55
## Getting started
66

7-
First create a Next.js API route by creating a file at for example `pages/api/graphql.ts`.
8-
This route will be accessible at `/api/graphql`.
7+
First create a Next.js API route by creating a file at for example `pages/api/graphql.js`.
8+
This API route will be accessible at `/api/graphql`.
99

1010
Next create an Apollo Server instance and pass it to `startServerAndCreateNextHandler`:
1111

@@ -43,3 +43,66 @@ export default startServerAndCreateNextHandler(server, {
4343
```
4444

4545
The Next.js `req` and `res` objects are passed along to the context function.
46+
47+
## Route Handlers (experimental)
48+
49+
This integration has experimental support for Next.js' new Route Handlers feature.
50+
51+
To use this integration with Route Handlers, first opt into Next.js' beta app directory by adding the following to `next.config.js`:
52+
53+
```js
54+
module.exports = {
55+
experimental: {
56+
appDir: true,
57+
},
58+
};
59+
```
60+
61+
Then create a new file at for example `app/graphql/route.js`.
62+
This file's route handlers will be accessible at `/graphql`.
63+
64+
Next create an Apollo Server instance, pass it to `startServerAndCreateNextHandler` and finally pass the handler to both a GET and a POST route handler:
65+
66+
```js
67+
import { startServerAndCreateNextHandler } from '@as-integrations/next';
68+
import { ApolloServer } from '@apollo/server';
69+
import { gql } from 'graphql-tag';
70+
71+
const resolvers = {
72+
Query: {
73+
hello: () => 'world',
74+
},
75+
};
76+
77+
const typeDefs = gql`
78+
type Query {
79+
hello: String
80+
}
81+
`;
82+
83+
const server = new ApolloServer({
84+
resolvers,
85+
typeDefs,
86+
});
87+
88+
const handler = startServerAndCreateNextHandler(server);
89+
90+
export async function GET(request) {
91+
return handler(request);
92+
}
93+
94+
export async function POST(request) {
95+
return handler(request);
96+
}
97+
```
98+
99+
## Typescript
100+
101+
When using this integration with Route Handlers you will have to specify the type of the incoming request object (`Response` or `NextResponse`) for the context function to receive the correct type signature:
102+
103+
```ts
104+
import { NextRequest } from 'next/server';
105+
106+
// req has the type NextRequest
107+
const handler = startServerAndCreateNextHandler<NextRequest>(server, { context: async req => ({ req }) });
108+
```

0 commit comments

Comments
 (0)