2016-09-12 07:47:34 +03:00
|
|
|
|
ObjectTypes
|
|
|
|
|
===========
|
|
|
|
|
|
|
|
|
|
An ObjectType is the single, definitive source of information about your
|
|
|
|
|
data. It contains the essential fields and behaviors of the data you’re
|
|
|
|
|
querying.
|
|
|
|
|
|
|
|
|
|
The basics:
|
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
|
- Each ObjectType is a Python class that inherits from
|
2016-09-25 15:01:12 +03:00
|
|
|
|
``graphene.ObjectType``.
|
2016-09-12 07:47:34 +03:00
|
|
|
|
- Each attribute of the ObjectType represents a ``Field``.
|
|
|
|
|
|
|
|
|
|
Quick example
|
|
|
|
|
-------------
|
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
|
This example model defines a Person, with a first and a last name:
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
|
|
import graphene
|
|
|
|
|
|
|
|
|
|
class Person(graphene.ObjectType):
|
|
|
|
|
first_name = graphene.String()
|
|
|
|
|
last_name = graphene.String()
|
|
|
|
|
full_name = graphene.String()
|
|
|
|
|
|
|
|
|
|
def resolve_full_name(self, args, context, info):
|
|
|
|
|
return '{} {}'.format(self.first_name, self.last_name)
|
|
|
|
|
|
|
|
|
|
**first\_name** and **last\_name** are fields of the ObjectType. Each
|
|
|
|
|
field is specified as a class attribute, and each attribute maps to a
|
|
|
|
|
Field.
|
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
|
The above ``Person`` ObjectType has the following schema representation:
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
2016-09-25 15:01:12 +03:00
|
|
|
|
.. code::
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
type Person {
|
|
|
|
|
firstName: String
|
|
|
|
|
lastName: String
|
|
|
|
|
fullName: String
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Resolvers
|
|
|
|
|
---------
|
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
|
A resolver is a method that resolves certain fields within a
|
|
|
|
|
``ObjectType``. If not specififed otherwise, the resolver of a
|
|
|
|
|
field is the ``resolve_{field_name}`` method on the ``ObjectType``.
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
|
By default resolvers take the arguments ``args``, ``context`` and ``info``.
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
2017-07-11 06:08:39 +03:00
|
|
|
|
NOTE: The resolvers on a ``ObjectType`` are always treated as ``staticmethod``\ s,
|
2016-10-07 11:56:01 +03:00
|
|
|
|
so the first argument to the resolver method ``self`` (or ``root``) need
|
|
|
|
|
not be an actual instance of the ``ObjectType``.
|
2016-09-28 23:57:41 +03:00
|
|
|
|
|
|
|
|
|
|
2016-09-12 07:47:34 +03:00
|
|
|
|
Quick example
|
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
This example model defines a ``Query`` type, which has a reverse field
|
|
|
|
|
that reverses the given ``word`` argument using the ``resolve_reverse``
|
|
|
|
|
method in the class.
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
|
|
import graphene
|
|
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
|
reverse = graphene.String(word=graphene.String())
|
|
|
|
|
|
|
|
|
|
def resolve_reverse(self, args, context, info):
|
|
|
|
|
word = args.get('word')
|
|
|
|
|
return word[::-1]
|
|
|
|
|
|
|
|
|
|
Resolvers outside the class
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
|
A field can use a custom resolver from outside the class:
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
|
|
import graphene
|
|
|
|
|
|
|
|
|
|
def reverse(root, args, context, info):
|
|
|
|
|
word = args.get('word')
|
|
|
|
|
return word[::-1]
|
|
|
|
|
|
|
|
|
|
class Query(graphene.ObjectType):
|
|
|
|
|
reverse = graphene.String(word=graphene.String(), resolver=reverse)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Instances as data containers
|
|
|
|
|
----------------------------
|
|
|
|
|
|
2016-10-07 11:56:01 +03:00
|
|
|
|
Graphene ``ObjectType``\ s can act as containers too. So with the
|
|
|
|
|
previous example you could do:
|
2016-09-12 07:47:34 +03:00
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
|
|
peter = Person(first_name='Peter', last_name='Griffin')
|
|
|
|
|
|
|
|
|
|
peter.first_name # prints "Peter"
|
|
|
|
|
peter.last_name # prints "Griffin"
|
|
|
|
|
|
|
|
|
|
.. _Interface: /docs/interfaces/
|