Graphene: The code sample in the official documentation can't execute correctly

Created on 25 Nov 2019  路  4Comments  路  Source: graphql-python/graphene

I follow this section of official guide:

https://docs.graphene-python.org/en/latest/types/objecttypes/#default-resolver

from collections import namedtuple
PersonValueObject = namedtuple('Person', 'first_name', 'last_name')

When run this script, it throw an error:

TypeError: namedtuple() takes 2 positional arguments but 3 were given

After change it to

PersonValueObject = namedtuple('Person', ('first_name', 'last_name'))

It works fine.

Besides, there is another issue in the code sample below:

assert result['data']['me'] == {"firstName": "Lin", 'lastName': 'Du'}

When I run the script, it throw an error:

Traceback (most recent call last):
  File "./src/01-getting-started/default-resolver.py", line 38, in <module>
    assert result['data']['me'] == {"firstName": "Lin", 'lastName': 'Du'}
TypeError: 'ExecutionResult' object is not subscriptable

After change it to:

assert result.data['me'] == {'firstName': 'Lin', 'lastName': 'Du'}

It works fine.

So, the code sample in the official documentation can't execute correctly. Do I miss anything or any context?

environment

Python 3.7.4
graphene 2.1.8

All 4 comments

PersonValueObject = namedtuple('Person', 'first_name', 'last_name')

This is wrong Python syntax.

namedtuple 's first parameter is the typename wihch can be used to construct an object. The second argument is `field_names, which

are a sequence of strings such as ['x', 'y']. Alternatively, field_names can be a single string with each fieldname separated by whitespace and/or commas, for example 'x y' or 'x, y'

So, either of

PersonValueObject = namedtuple('Person', ('first_name', 'last_name'))

or

PersonValueObject = namedtuple('Person', ['first_name', 'last_name'])

or

PersonValueObject = namedtuple('Person', 'first_name last_name')

or

PersonValueObject = namedtuple('Person', 'first_name, last_name')

will work

This is wrong Python syntax.
...

@KIRA009 to clarify, the fact that it is incorrect syntax is what @mrdulin is pointing out.

So, the code sample in the official documentation can't execute correctly. Do I miss anything or any context?

@mrdulin was a bit unsure about the fact, so I stated the docs, that's all.

Good spot @mrdulin ! Fixed in #1120

Was this page helpful?
0 / 5 - 0 ratings