graphene icon indicating copy to clipboard operation
graphene copied to clipboard

graphene.List(graphene.String()) throws error.

Open glusa8 opened this issue 10 years ago • 2 comments

It seems like with the newest version of Graphene 1.0,

you have to write:

graphene.List(graphene.String) instead of graphene.List(graphene.String()). The latter throws an error saying there is no name field in type String (i.e. AttributeError: 'String' object has no attribute 'name').

Is this the intended? If so, is there any documentation?

glusa8 avatar Oct 25 '16 03:10 glusa8

I ran into this gotcha too... really confusing error message as well :/

chaffeqa avatar Oct 29 '16 07:10 chaffeqa

I agree that this could probably use a little clarification in the docs, but if you look at it as pure Python:

1- graphene.String is a class, you use the class when you want to say I want a List of Strings. List(String) means you want a list of object of type String. The argument to List(Structure).init is actually called 'of_type': https://github.com/graphql-python/graphene/blob/master/graphene/types/structures.py#L10


class Structure(UnmountedType):
    '''
    A structure is a GraphQL type instance that
    wraps a main type with certain structure.
    '''

    def __init__(self, of_type, *args, **kwargs):
        super(Structure, self).__init__(*args, **kwargs)
        self.of_type = of_type

2- graphene.String() is an instance of the UnmountedType String just like List() is an instance of the type List.

It wouldn't make sense to say you want a list of a particular instance of the String class. Inversely, when you're using String to create a Field, you do want an instance in that case. I don't want a field to equal the String class, I want an instance of the type, an actual String instance.

I hope this clarifies it a bit.

yfilali avatar Nov 02 '16 04:11 yfilali