.. _query_validation:
Client Side Query Validation#
Because we make use of GraphQL Core 3 <https://github.com/graphql-python/graphql-core-next>_, client-side
query validation is performed by default.
.. code-block:: python
request = GraphQLRequest(
query="""
query {
bad {
login
}
}
"""
)
response: GraphQLResponse = await client.query(request=request)
This will raise :class:aiographql.client.GraphQLClientValidationException. The message
will also contain information about the error.
.. code-block:: python
aiographql.client.exceptions.GraphQLClientValidationException: Query validation failed
Cannot query field 'bad' on type 'Query'.
GraphQL request:3:15
2 | query {
3 | bad {
| ^
4 | login
Process finished with exit code 1
Server Side Query Validation#
You can skip the client side validation, forcing server side validation instead by setting
the :attr:aiographql.client.GraphQLRequest.validate to False before making the request.
.. code-block:: python
request = GraphQLRequest(
query="""
query {
bad {
login
}
}
""",
validate=False
)
response: GraphQLResponse = await client.query(request=request)
If you are working with a GraphQL server that has introspection disabled, you can disable
validation globally when initializing the :class:aiographql.client.GraphQLClient.
.. code-block:: python
client = GraphQLClient(
endpoint="https://api.github.com/graphql",
validate=False
)
response: GraphQLResponse = await client.query("query { viewer { login } }")
response.data
{}
response.errors
[GraphQLError(extensions={'code': 'undefinedField', 'typeName': 'Query', 'fieldName': 'bad'}, locations=[{'line': 3, 'column': 15}], message="Field 'bad' doesn't exist on type 'Query'", path=['query', 'bad'])]
Introspection Caching#
The client introspects the server to get the schema for validation. This schema is cached internally. For long-running production services, if the server schema changes, you can force a refresh:
.. code-block:: python
await client.get_schema(refresh=True)
This is particularly useful if your GraphQL server is updated with new fields or types without restarting the client application.