graphene-django icon indicating copy to clipboard operation
graphene-django copied to clipboard

Nullable field does not work.

Open glusa8 opened this issue 9 years ago • 3 comments

I have a field:

client_users = List(ClientUserType, ids=List(Int)) that accepts null as an argument to the param ids because clearly I did not say NonNull(List(Int)), but when I do a query in GraphiQL using null as argument like so:

query {
  clientUsers(ids: null) {
    id
  }
}

I get the following error:

"message": "Syntax Error GraphQL request (2:20) Unexpected Name \"null\"\n\n1: query {\n2:   clientUsers(ids: null) {\n                      ^\n3:     id\n",

Clearly, something is not right. Is my understanding incorrect?

glusa8 avatar Sep 10 '17 20:09 glusa8

Try this instead? I think it will be null already.

query {
  clientUsers {
    id
  }
}

clodal avatar Sep 22 '17 12:09 clodal

I think GraphQL does not allow for null fields.

glusa8 avatar Sep 22 '17 16:09 glusa8

If that's the case, then there's a bug I've encountered. Given this query:

{
    player {
        games {
            ...
        }
    }
}

This schema works without error:

class Game(ObjectType):
    ...

class Player(ObjectType):
    games = List(Game, recent_count=Int(default_value=0))

But this does not (the default is None instead of 0):

class Game(ObjectType):
    ...

class Player(ObjectType):
    games = List(Game, recent_count=Int(default_value=None))
    def resolve_games(root, info, recent_count):
        ...

Even though both of the schemas provide a default value, only the latter actually uses the default value; the latter fails with resolve_games() missing 1 required positional argument: 'recent_count'

HOWEVER:

When I change resolve_games to this:

def resolve_games(root, info, recent_count=None):  # Added a default to the function

The query works without arguments. It should be made clear that GraphQL does support queries without arguments, or with optional arguments, but implementing that in the schema requires modifying the resolver function, not the class field.

This is hinted at because the error message, " missing positional argument", comes from the TypeError of trying and failing to call a method with the wrong arguments, not a GraphQL parse error.

Lucretiel avatar Jan 29 '18 02:01 Lucretiel