Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite the nextjs example to use Yoga #762

Merged
merged 2 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@pothos/core": "workspace:*",
"@tanstack/react-query": "^4.19.0",
"graphql": "16.6.0",
"graphql-helix": "^1.13.0",
"graphql-yoga": "3.1.1",
"graphql-tag": "^2.12.6",
"next": "13.0.6",
"react": "18.2.0",
Expand Down
52 changes: 21 additions & 31 deletions examples/nextjs/pages/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
import {
getGraphQLParameters,
processRequest,
renderGraphiQL,
sendResult,
shouldRenderGraphiQL,
} from 'graphql-helix';
import { NextApiHandler } from 'next/types';
import { schema } from '../../graphql/schema';

export default (async (req, res) => {
const request = {
body: req.body as object,
headers: req.headers,
method: req.method!,
query: req.query,
};
/** GraphQL API entrypoint */

if (shouldRenderGraphiQL(request)) {
res.send(renderGraphiQL({ endpoint: '/api/graphql' }));
} else {
const { operationName, query, variables } = getGraphQLParameters(request);
import { createYoga } from 'graphql-yoga';
import type { NextApiRequest, NextApiResponse } from 'next';
import { schema } from '../../graphql/schema';

const result = await processRequest({
operationName,
query,
variables,
request,
schema,
});
export const config = {
api: {
// Disable body parsing (required for file uploads)
bodyParser: false,
},
};

void sendResult(result, res);
}
}) as NextApiHandler;
export default createYoga<{
req: NextApiRequest;
res: NextApiResponse;
}>({
schema,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know not all existing examples have this, but all examples should have a context factory that at a minimum creates an empty context object. Some Pothos plugins require a context object, and it's good practice to set this up for any new graphql api

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree and I'll add a context factory

graphqlEndpoint: '/api/graphql',
context: (ctx) => ({
// Note: you can use 'ctx.req' if you need access to the default Next.js 'req' object
user: { id: Number.parseInt(ctx.request.headers.get('x-user-id') ?? '1', 10) },
}),
});
Loading