Debian Codegraph @

docs/usage/validator.rst

raw
1 Validating GraphQL Queries
2 --------------------------
3
4 .. currentmodule:: graphql.validation
5
6 When executing GraphQL queries, the second step that happens under the hood after
7 parsing the source code is a validation against the given schema using the rules of the
8 GraphQL specification. You can also run the validation step manually by calling the
9 :func:`validate` function, passing the schema and the AST document::
10
11 from graphql import parse, validate
12
13 errors = validate(schema, parse("""
14 {
15 human(id: NEWHOPE) {
16 name
17 homePlace
18 friends
19 }
20 }
21 """))
22
23 As a result, you will get a complete list of all errors that the validators has found.
24 In this case, we will get the following three validation errors::
25
26 [GraphQLError(
27 'String cannot represent a non string value: NEWHOPE',
28 locations=[SourceLocation(line=3, column=17)]),
29 GraphQLError(
30 "Cannot query field 'homePlace' on type 'Human'."
31 " Did you mean 'homePlanet'?",
32 locations=[SourceLocation(line=5, column=9)]),
33 GraphQLError(
34 "Field 'friends' of type '[Character]' must have a selection of subfields."
35 " Did you mean 'friends { ... }'?",
36 locations=[SourceLocation(line=6, column=9)])]
37
38 These rules are available in the :data:`specified_rules` list and implemented in the
39 :mod:`graphql.validation.rules` subpackage. Instead of the default rules, you can also
40 use a subset or create custom rules. The rules are based on the :class:`ValidationRule`
41 class which is based on the :class:`~graphql.language.Visitor` class which provides a
42 way of walking through an AST document using the visitor pattern.