From 0805436d45736b552ba5e41967cf070f4f289e35 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Sat, 16 Mar 2019 21:58:49 +0000 Subject: [PATCH 1/2] Docs on changing the name of an ObjectType (#922) Fixes https://github.com/graphql-python/graphene/issues/839 --- docs/types/objecttypes.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/types/objecttypes.rst b/docs/types/objecttypes.rst index b6eb3087..c15d608b 100644 --- a/docs/types/objecttypes.rst +++ b/docs/types/objecttypes.rst @@ -230,4 +230,17 @@ previous example you could do: peter.first_name # prints "Peter" peter.last_name # prints "Griffin" +Changing the name +----------------- + +By default the type name in the GraphQL schema will the same as the class name +that defines the ``ObjectType``. This can be changed by setting the ``name`` +property on the ``Meta`` class: + +.. code:: python + + class MyGraphQlSong(graphene.ObjectType): + class Meta: + name = 'Song' + .. _Interface: /docs/interfaces/ From 21cccf4c96951ceddaf6248540a2d206ac4a5ced Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Sun, 17 Mar 2019 19:43:49 +0000 Subject: [PATCH 2/2] Update object type docs (#921) Now that the default resolver handles both objects and dicts --- docs/types/objecttypes.rst | 48 +++++--------------------------------- 1 file changed, 6 insertions(+), 42 deletions(-) diff --git a/docs/types/objecttypes.rst b/docs/types/objecttypes.rst index c15d608b..18f91bd3 100644 --- a/docs/types/objecttypes.rst +++ b/docs/types/objecttypes.rst @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~