2016-09-21 11:16:35 +03:00
Schema
======
2019-06-10 02:49:56 +03:00
A GraphQL **Schema** defines the types and relationship between **Fields** in your API.
A Schema is created by supplying the root :ref: `ObjectType` of each operation, query (mandatory), mutation and subscription.
Schema will collect all type definitions related to the root operations and then supplied to the validator and executor.
2016-09-21 11:16:35 +03:00
.. code :: python
2016-10-07 11:56:01 +03:00
2016-09-21 11:16:35 +03:00
my_schema = Schema(
query=MyRootQuery,
mutation=MyRootMutation,
2019-06-10 02:49:56 +03:00
subscription=MyRootSubscription
2016-09-21 11:16:35 +03:00
)
2019-06-10 02:49:56 +03:00
A Root Query is just a special :ref: `ObjectType` that :ref: `defines the fields <Scalars>` that are the entrypoint for your API. Root Mutation and Root Subscription are similar to Root Query, but for different operation types:
* Query fetches data
* Mutation to changes data and retrieve the changes
* Subscription to sends changes to clients in real time
Review the `GraphQL documentation on Schema`_ for a brief overview of fields, schema and operations.
.. _GraphQL documentation on Schema: https://graphql.org/learn/schema/
Querying
--------
To query a schema, call the `` execute `` method on it. See :ref: `SchemaExecute` for more details.
.. code :: python
query_string = 'query whoIsMyBestFriend { myBestFriend { lastName } }'
my_schema.execute(query_string)
2016-09-21 11:16:35 +03:00
Types
-----
2016-10-07 11:56:01 +03:00
There are some cases where the schema cannot access all of the types that we plan to have.
For example, when a field returns an `` Interface `` , the schema doesn't know about any of the
2016-09-21 11:16:35 +03:00
implementations.
2016-10-07 11:56:01 +03:00
In this case, we need to use the `` types `` argument when creating the Schema.
2016-09-21 11:16:35 +03:00
.. code :: python
my_schema = Schema(
query=MyRootQuery,
types=[SomeExtraObjectType, ]
)
2019-06-10 02:49:56 +03:00
.. _SchemaAutoCamelCase:
2016-09-21 11:16:35 +03:00
Auto CamelCase field names
--------------------------
2016-10-07 11:56:01 +03:00
By default all field and argument names (that are not
2016-09-21 11:16:35 +03:00
explicitly set with the `` name `` arg) will be converted from
2016-10-07 11:56:01 +03:00
`` snake_case `` to `` camelCase `` (as the API is usually being consumed by a js/mobile client)
2016-09-21 11:16:35 +03:00
2016-10-07 11:56:01 +03:00
For example with the ObjectType
2016-09-21 11:16:35 +03:00
.. code :: python
class Person(graphene.ObjectType):
last_name = graphene.String()
other_name = graphene.String(name='_other_Name')
2016-10-07 11:56:01 +03:00
the `` last_name `` field name is converted to `` lastName `` .
2016-09-21 11:16:35 +03:00
2016-10-07 11:56:01 +03:00
In case you don't want to apply this transformation, provide a `` name `` argument to the field constructor.
`` other_name `` converts to `` _other_Name `` (without further transformations).
2016-09-21 11:16:35 +03:00
2016-10-07 11:56:01 +03:00
Your query should look like
2016-09-21 11:16:35 +03:00
2016-09-25 15:01:12 +03:00
.. code ::
2016-09-21 11:16:35 +03:00
{
lastName
_other_Name
}
2016-10-07 11:56:01 +03:00
To disable this behavior, set the `` auto_camelcase `` to `` False `` upon schema instantiation.
2016-09-21 11:16:35 +03:00
.. code :: python
my_schema = Schema(
query=MyRootQuery,
auto_camelcase=False,
)