Skip to content

Conversation

asvrada
Copy link

@asvrada asvrada commented Apr 8, 2020

Changed JavaScript syntax to Python one.

@syrusakbary
Copy link
Member

I'm not sure if I understand. Using object attributes is also Pythonic, in fact we did the addition that resolver arguments are inside a python object (instead of a dict) to make it much easier to reason about.

I'm curious to hear thoughts from more people here

@asvrada
Copy link
Author

asvrada commented Apr 13, 2020

Using object attributes won't work in my project, might be I did something wrong, could you have a look? Thanks!

# schema.py
class CreateIcon(relay.ClientIDMutation):
    class Input:
        keyword = graphene.String(required=True)
        path = graphene.String(required=True)

    # output
    icon = graphene.Field(IconType)

    @classmethod
    def mutate_and_get_payload(cls, root, info, **payload):
        keyword = payload.keyword
        path = payload["path"]
        icon = models.Icon.objects.create(keyword=keyword, path=path)
        return CreateIcon(icon=icon)

class Mutation(graphene.ObjectType):
    create_icon = CreateIcon.Field()

Would throw error:

Bad Request: /graphql/
[13/Apr/2020 xx:48:18] "POST /graphql/ HTTP/1.1" 400 116
Traceback (most recent call last):
  File "yyy/python3.7/site-packages/promise/promise.py", line 489, in _resolve_from_executor
    executor(resolve, reject)
  File "yyy/python3.7/site-packages/promise/promise.py", line 756, in executor
    return resolve(f(*args, **kwargs))
  File "yyy/python3.7/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
    return next(*args, **kwargs)
  File "yyy/python3.7/site-packages/graphene/relay/mutation.py", line 70, in mutate
    result = cls.mutate_and_get_payload(root, info, **input)
  File "xxx/bill/schema.py", line 83, in mutate_and_get_payload
    keyword = payload.keyword
graphql.error.located_error.GraphQLLocatedError: 'dict' object has no attribute 'keyword'

@asvrada asvrada closed this Apr 18, 2020
@asvrada asvrada deleted the patch-1 branch April 26, 2020 05:06
@hf-krechan
Copy link

I have the same error.
It would be nice to use the attribute notations. So the IDE and linter-tools could support you.
Maybe one could make use of the namedtuple to create an object from a dictionary behind the scences:

from collections import namedtuple

input = {'first_name': 'Arty', 'last_name': 'Fischel'}
input_object = namedtuple('input_object', input.keys())(*input.values())
print(input_object.first_name)

But for now I would stick to the input.get("first_name") approach to avoid errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants