Testing
GraphQL Yoga makes it easy to test your GraphQL API. It has built-in support for HTTP injection. You can use any testing framework of your choice.
You can use the fetch
method on your yoga instance for calling the yoga instance as if you were doing an HTTP request.
import { createYoga } from 'graphql-yoga'
import { schema } from './schema'
const yoga = createYoga({ schema })
const response = await yoga.fetch('http://localhost:4000/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: '{ greetings }',
}),
})
console.assert(response.status === 200, 'Response status should be 200')
const executionResult = await response.json()
console.assert(
executionResult.data.greetings ===
'This is the `greetings` field of the root `Query` type',
`Expected 'This is the `greetings` field of the root `Query` type' but got ${executionResult.data.greetings}`,
)
Last updated on November 15, 2022