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

Documentation - Correction required in Swapping to expressMiddleware #7641

Open
cgn-ca opened this issue Jul 12, 2023 · 3 comments
Open

Documentation - Correction required in Swapping to expressMiddleware #7641

cgn-ca opened this issue Jul 12, 2023 · 3 comments

Comments

@cgn-ca
Copy link

cgn-ca commented Jul 12, 2023

Description

After trying to implement the Swapping to expressMiddleware for healthchecks I noticed an error in the documentation, ended up costing me an hour or two.

The documentation is incorrect at standalone.mdx (I would create a PR but I don't have write access to the repository, I'd prefer to just create the PR myself)

app.use('/',
  cors<cors.CorsRequest>(),
  // 50mb is the limit that `startStandaloneServer` uses, but you may configure this to suit your needs
  bodyParser.json({ limit: '50mb' }),
  // expressMiddleware accepts the same arguments:
  // an Apollo Server instance and optional configuration options
  expressMiddleware(server, {
    context: async ({ req }) => ({ token: req.headers.token }),
  }),
);

should be changed to

app.use('/graphql',
  cors<cors.CorsRequest>(),
  // 50mb is the limit that `startStandaloneServer` uses, but you may configure this to suit your needs
  bodyParser.json({ limit: '50mb' }),
  // expressMiddleware accepts the same arguments:
  // an Apollo Server instance and optional configuration options
  expressMiddleware(server, {
    context: async ({ req }) => ({ token: req.headers.token }),
  }),
);
@trevor-scheer
Copy link
Member

/ is the default, /graphql is more of a relic of Apollo Server v3. I would say that here it's correct as is. But if you were also referencing the health check page here, I think we should switch this to / instead.

You are welcome to fork and PR even without write permissions! You can even edit straight in the Github UI with the little pencil icon if you'd rather not clone the repo:
Untitled

@cgn-ca
Copy link
Author

cgn-ca commented Jul 16, 2023

For what it's worth, this is the error I get if I use / and not /graphql , running the following curl command

curl http://localhost:3000/healthz
{"errors":[{"message":"This operation has been blocked as a potential Cross-Site Request Forgery (CSRF). Please either specify a 'content-type' header (with a type that is not one of application/x-www-form-urlencoded, multipart/form-data, text/plain) or provide a non-empty value for one of the following headers: x-apollo-operation-name, apollo-require-preflight\n","extensions":{"code":"BAD_REQUEST","stacktrace":["BadRequestError: This operation has been blocked as a potential Cross-Site Request Forgery (CSRF). Please either specify a 'content-type' header (with a type that is not one of application/x-www-form-urlencoded, multipart/form-data, text/plain) or provide a non-empty value for one of the following headers: x-apollo-operation-name, apollo-require-preflight","","    at new GraphQLErrorWithCode (/Users/me/Dev/Apollo-Example/node_modules/@apollo/server/src/internalErrorClasses.ts:15:5)","    at new BadRequestError (/Users/me/apollo-example/node_modules/@apollo/server/src/internalErrorClasses.ts:116:5)","    at preventCsrf (/Users/me/apollo-example/node_modules/@apollo/server/src/preventCsrf.ts:91:9)","    at ApolloServer.executeHTTPGraphQLRequest (/Users/me/apollo-example/node_modules/@apollo/server/src/ApolloServer.ts:1044:20)","    at processTicksAndRejections (node:internal/process/task_queues:95:5)"]}}]}

@trevor-scheer
Copy link
Member

What if you define the routes more specifically instead of .use()?

app.post('/', ...);
app.get('/healthz', ...);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants