Apollo Server rejects a request as a potential CSRF
This operation has been blocked as a potential Cross-Site Request Forgery (CSRF).Short answer
Apollo Server's CSRF prevention is on by default. It runs an operation only if the request has a JSON content type, or an Apollo-Require-Preflight header, or an X-Apollo-Operation-Name header. Make your client send one of them. Do not switch csrfPrevention off; fix the client instead, as the Apollo docs advise.
Fix it fast
1Find out which cause you have
Send the same query yourself with a JSON content type, and then without one. If only the second is rejected, the client is not sending an accepted header.
curl -i -X POST http://localhost:4000/graphql -H "content-type: application/json" -d '{"query":"{ __typename }"}' curl -i "http://localhost:4000/graphql?query=%7B__typename%7D"2Match what you see, then jump to its fix
- 1
400 on GET queries (useGETForQueries, link prefetch, or a plain browser URL)A GET request without any header Apollo acceptsFix - 2
Queries work, but mutations with a File variable return 400A file upload sends multipart/form-dataFix - 3
Works from Apollo Client Web, fails from your own fetch call or a server-side scriptA custom client or tool sends a non-JSON content typeFix
- 1
What does the error look like?
This operation has been blocked as a potential Cross-Site Request Forgery (CSRF).HTTP 400 Bad Request from the GraphQL endpoint
Cause 1 of 3
A GET request without any header Apollo accepts
1Check
The docs say a request runs only if it has a JSON-type Content-Type, a non-empty X-Apollo-Operation-Name, or a non-empty Apollo-Require-Preflight header. From v5.5.0 a GET with a non-empty Content-Type other than application/json is also rejected.
curl -i "http://localhost:4000/graphql?query=%7B__typename%7D" -H "Apollo-Require-Preflight: true"2Fix
Configure the client to send a non-empty Apollo-Require-Preflight header on every request.
import { HttpLink } from "@apollo/client";
const link = new HttpLink({
uri: "http://localhost:4000/graphql",
headers: { "Apollo-Require-Preflight": "true" },
});3Verify
The curl command above returns data instead of a 400.
curl -i "http://localhost:4000/graphql?query=%7B__typename%7D" -H "Apollo-Require-Preflight: true"Confirmed by apollographql.com
Cause 2 of 3
A file upload sends multipart/form-data
1Check
multipart/form-data counts as a simple content type, so the JSON content-type rule does not apply. The docs give the client-side fix for apollo-upload-client.
grep -rn "createUploadLink" src app lib 2>/dev/null2Fix
Pass the preflight header to createUploadLink.
// createUploadLink comes from the apollo-upload-client package
const link = createUploadLink({
uri: "http://localhost:4000/graphql",
headers: { "Apollo-Require-Preflight": "true" },
});3Verify
Upload a small file again and check the response status is 200.
# repeat the upload in the app and watch the Network tabConfirmed by apollographql.com
Cause 3 of 3
A custom client or tool sends a non-JSON content type
1Check
Apollo Client Web sends Content-Type: application/json, so it passes. A hand-written fetch or another tool may send text/plain or form encoding.
grep -rn "fetch(.*graphql" src app lib 2>/dev/null2Fix
Set content-type: application/json on the request, or add the Apollo-Require-Preflight header. If you must accept other headers, csrfPrevention can list your own, which replaces the two header checks.
await fetch("http://localhost:4000/graphql", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ query: "{ __typename }" }),
});3Verify
Repeat the failing call; it should return a JSON body.
curl -i -X POST http://localhost:4000/graphql -H "content-type: application/json" -d '{"query":"{ __typename }"}'Confirmed by apollographql.com
How do I stop it coming back?
- Add the Apollo-Require-Preflight header in one shared link so every client call has it.
- Keep csrfPrevention on in every environment, as the docs recommend.
- Test one GET query and one upload in your integration tests.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Web apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Jest fails with SyntaxError: Unexpected token when testing a Next.js component
Jest failed to parse a file.
Jest only understands plain JavaScript until a transform and a few file mappings tell it more.
3 causes, each with a fixWeb apps
NextAuth sends the user to ?error=AccessDenied after Google sign in
error=AccessDenied
AccessDenied is not a Google or MongoDB error.
2 causes, each with a fix