mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 09:36:44 +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``.
|
||||
|
||||
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
|
||||
``ObjectType``.
|
||||
attempt to use a property with the same name on the object or dict that is
|
||||
passed to the ``ObjectType``.
|
||||
|
||||
.. 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):
|
||||
me = graphene.Field(Person)
|
||||
best_friend = graphene.Field(Person)
|
||||
|
||||
def resolve_me(_, info):
|
||||
# returns an object that represents a Person
|
||||
return get_human(name='Luke Skywalker')
|
||||
|
||||
If you are passing a dict instead of an object to your ``ObjectType`` you can
|
||||
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):
|
||||
def resolve_best_friend(_, info):
|
||||
return {
|
||||
"first_name": "Luke",
|
||||
"last_name": "Skywalker",
|
||||
"first_name": "R2",
|
||||
"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
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
Loading…
Reference in New Issue
Block a user