graphene icon indicating copy to clipboard operation
graphene copied to clipboard

[Documentation] Exception thrown with code copied from documentation

Open wkeithvan opened this issue 5 years ago • 1 comments

Note: for support questions, please use stackoverflow. This repository's issues are reserved for feature requests and bug reports.

My code:

from graphene import ObjectType, String, Field, Schema

class Person(ObjectType):
    first_name = String()
    last_name = String()
    full_name = String()

    def resolve_full_name(parent, info):
        return f"{parent.first_name} {parent.last_name}"

def person_resolver(root, info):
    return {
        "first_name": "John",
        "last_name": "Doe",
    }

class Query(ObjectType):
    Person = Field(Person, resolver=person_resolver)

schema = Schema(query=Query, auto_camelcase=False)

data = schema.execute(
"""
query{
  Person{
    first_name
    last_name
    full_name
  }
}
"""
)

print(data)

Exception thrown:

An error occurred while resolving field Person.full_name
Traceback (most recent call last):
  File "C:\Users\keith.vandermeulen\Documents\play\venv\lib\site-packages\graphql\execution\executor.py", line 452, in resolve_or_error
    return executor.execute(resolve_fn, source, info, **args)
  File "C:\Users\keith.vandermeulen\Documents\play\venv\lib\site-packages\graphql\execution\executors\sync.py", line 16, in execute
    return fn(*args, **kwargs)
  File "test.py", line 7, in resolve_full_name
    return f"{parent.first_name} {parent.last_name}"
AttributeError: 'dict' object has no attribute 'first_name'
Traceback (most recent call last):
  File "C:\Users\keith.vandermeulen\Documents\play\venv\lib\site-packages\graphql\execution\executor.py", line 452, in resolve_or_error
    return executor.execute(resolve_fn, source, info, **args)
  File "C:\Users\keith.vandermeulen\Documents\play\venv\lib\site-packages\graphql\execution\executors\sync.py", line 16, in execute
    return fn(*args, **kwargs)
  File "test.py", line 7, in resolve_full_name
    return f"{parent.first_name} {parent.last_name}"
graphql.error.located_error.GraphQLLocatedError: 'dict' object has no attribute 'first_name'

OrderedDict([('errors', [{'message': "'dict' object has no attribute 'first_name'", 'locations': [{'line': 6, 'column': 5}], 'path': ['Person', 'full_name']}]), ('data', OrderedDict([('Person', OrderedDict([('first_name', 'John'), ('last_name', 'Doe'), ('full_name', None)]))]))])
  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via a github repo, https://repl.it or similar.

  • What is the expected behavior? The documentation should contain working code.

  • What is the motivation / use case for changing the behavior? The documentation first example doesn't work. This seems like a bug...

  • Please tell us about your environment:

    • Version: 2.1.8
    • Platform: Windows 10

Here is my pip list command from a fresh virtualenv:

Package       Version
------------- -------
aniso8601     7.0.0
graphene      2.1.8
graphql-core  2.3.2
graphql-relay 2.0.1
pip           20.0.2
promise       2.3
Rx            1.6.1
setuptools    46.0.0
six           1.15.0
wheel         0.34.2
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow) Please note that I created a brand new virtualenv with a fresh pip install today to try this out. It seems as though parent is actually a dictionary and the resolver works if changed to the following:
def resolve_full_name(parent, info):
    return f"{parent['first_name']} {parent['last_name']}"

wkeithvan avatar Jul 31 '20 20:07 wkeithvan

I think the documentation needs a proper update

akshaybabloo avatar Nov 18 '22 02:11 akshaybabloo