How to Test a GraphQL API in CI
A GraphQL endpoint is one POST route, so a test sends a query in the body and asserts on data and errors.
POST a JSON { "query": "..." } payload to /graphql, then assert the errors array is absent and data has the shape you expect.
Steps
- POST the query as JSON to the
/graphqlroute. - Assert
errorsis null or absent, then checkdata. - Pass variables in the
variablesfield, not by string interpolation.
Workflow
.github/workflows/ci.yml
jobs:
graphql:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: |
npm start &
npx wait-on http://localhost:4000/graphql
- run: |
resp=$(curl -sf http://localhost:4000/graphql \
-H 'content-type: application/json' \
-d '{"query":"{ viewer { id name } }"}')
echo "$resp" | jq -e '.errors | not'
echo "$resp" | jq -e '.data.viewer.id'Gotchas
- GraphQL returns HTTP 200 even for query errors, so assert on the
errorsfield, not the status code. - For typed suites, generate a client from the schema so query changes fail at compile time.
Related guides
How to Assert an API Response Matches a JSON Schema in CIValidate that an API response matches a JSON Schema in GitHub Actions with ajv or jsonschema, so a shape chan…
How to Handle Auth Tokens for API Tests in CIAuthenticate API tests in GitHub Actions by fetching a token at runtime or reading one from secrets, then pas…
How to Start the API and Wait for It Before Testing in CIStart your API in the background and block until it is ready with wait-on before API tests run in GitHub Acti…