mirror of
https://github.com/graphql-python/graphene.git
synced 2025-04-25 20:13:40 +03:00
Update object type docs (#921)
Now that the default resolver handles both objects and dicts
This commit is contained in:
parent
0805436d45
commit
21cccf4c96
|
@ -57,8 +57,8 @@ so the first argument to the resolver method ``self`` (or ``root``) need
|
||||||
not be an actual instance of the ``ObjectType``.
|
not be an actual instance of the ``ObjectType``.
|
||||||
|
|
||||||
If an explicit resolver is not defined on the ``ObjectType`` then Graphene will
|
If an explicit resolver is not defined on the ``ObjectType`` then Graphene will
|
||||||
attempt to use a property with the same name on the object that is passed to the
|
attempt to use a property with the same name on the object or dict that is
|
||||||
``ObjectType``.
|
passed to the ``ObjectType``.
|
||||||
|
|
||||||
.. code:: python
|
.. code:: python
|
||||||
|
|
||||||
|
@ -70,54 +70,18 @@ attempt to use a property with the same name on the object that is passed to the
|
||||||
|
|
||||||
class Query(graphene.ObjectType):
|
class Query(graphene.ObjectType):
|
||||||
me = graphene.Field(Person)
|
me = graphene.Field(Person)
|
||||||
|
best_friend = graphene.Field(Person)
|
||||||
|
|
||||||
def resolve_me(_, info):
|
def resolve_me(_, info):
|
||||||
# returns an object that represents a Person
|
# returns an object that represents a Person
|
||||||
return get_human(name='Luke Skywalker')
|
return get_human(name='Luke Skywalker')
|
||||||
|
|
||||||
If you are passing a dict instead of an object to your ``ObjectType`` you can
|
def resolve_best_friend(_, info):
|
||||||
change the default resolver in the ``Meta`` class like this:
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
import graphene
|
|
||||||
from graphene.types.resolver import dict_resolver
|
|
||||||
|
|
||||||
class Person(graphene.ObjectType):
|
|
||||||
class Meta:
|
|
||||||
default_resolver = dict_resolver
|
|
||||||
|
|
||||||
first_name = graphene.String()
|
|
||||||
last_name = graphene.String()
|
|
||||||
|
|
||||||
class Query(graphene.ObjectType):
|
|
||||||
me = graphene.Field(Person)
|
|
||||||
|
|
||||||
def resolve_me(_, info):
|
|
||||||
return {
|
return {
|
||||||
"first_name": "Luke",
|
"first_name": "R2",
|
||||||
"last_name": "Skywalker",
|
"last_name": "D2",
|
||||||
}
|
}
|
||||||
|
|
||||||
Or you can change the default resolver globally by calling ``set_default_resolver``
|
|
||||||
before executing a query.
|
|
||||||
|
|
||||||
.. code:: python
|
|
||||||
|
|
||||||
import graphene
|
|
||||||
from graphene.types.resolver import dict_resolver, set_default_resolver
|
|
||||||
|
|
||||||
set_default_resolver(dict_resolver)
|
|
||||||
|
|
||||||
schema = graphene.Schema(query=Query)
|
|
||||||
result = schema.execute('''
|
|
||||||
query {
|
|
||||||
me {
|
|
||||||
firstName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
''')
|
|
||||||
|
|
||||||
|
|
||||||
Resolvers with arguments
|
Resolvers with arguments
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
Loading…
Reference in New Issue
Block a user