Search⌘ K
AI Features

Validate GraphQL Query

Explore how to validate GraphQL queries using the type system to ensure correctness before execution. Understand common errors like invalid fragments, querying non-existent fields, and missing subfield selections. Learn to handle these through schema validation and error messages to improve query reliability.

By leveraging the type system, we can determine the validity of any query. The system allows servers and clients to notify developers when they send GraphQL queries without relying on runtime checks.

Can’t spread fragment

A fragment can’t refer to itself since this could cause an unbounded result. Here’s the pizzas query with an invalid GraphQL fragment:

Javascript (babel-node)
query GetPizzas {
Neapolitan: pizzas(name: "Neapolitan Pizza") {
...pizzaFields
}
Chicago: pizzas(name: "Chicago Pizza") {
...pizzaFields
}
}
fragment pizzaFields on Pizza {
... on Pizza {
id
pizza
}
... on ChicagoPizza {
...pizzaFields // this is invalid!
}
}

If we execute the ...