graphql-python / graphql-python/graphene
[Documentation] Exception thrown with code copied from documentation
- Dominant language
- Python
- Stars
- 8.2k
- Forks
- 818
- PR merge metrics
- No merged PRs in 30d
Description
**Note: for support questions, please use stackoverflow**. This repository's issues are reserved for feature requests and bug reports.
* **What is the current behavior?**
The code listed in ["ObjectType -> Quick Example"](https://docs.graphene-python.org/en/latest/types/objecttypes/#quick-example) throws exceptions.
My code:
```python
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:
```python
def resolve_full_name(parent, info):
return f"{parent['first_name']} {parent['last_name']}"
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the "ObjectType -> Quick Example" section at the linked Graphene documentation page and compare its resolver example with the reported Graphene 2.1.8 behavior. Update the example so it works with the documented return value, then verify that the complete example runs without the reported exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100