graphene icon indicating copy to clipboard operation
graphene copied to clipboard

graphene fails on null field argument

Open yeedle opened this issue 4 years ago • 3 comments

  • What is the current behavior? It breaks when a literal null value is provided to a nullable field argument
from graphene import ObjectType, String, Schema, Argument

class Query(ObjectType):
  hello = String(name=Argument(String, required=False, default_value=None))

  def resolve_hello(root, info, name=None):
    return f'Hello {name}!'

schema = Schema(query=Query)

query_with_argument = 'query { hello(name: null) }'
result = schema.execute(query_with_argument)
print(result)

output:

{'errors': [{'message': 'Syntax Error GraphQL (1:21) Unexpected Name "null"\n\n1: query { hello(name: null) }\n                       ^\n', 'locations': [{'line': 1, 'column': 21}]}]}

Check out the repl.it repro.

  • What is the expected behavior?

According to the spec query { field(arg: null) } and query { field } are both valid. The expected behavior would be for the above to output:

{'data': {'hello': 'Hello None!'}}
  • What is the motivation / use case for changing the behavior? I have a queries (and fragments) that get reused in multiple places in the codebase. Some places require the use of optional arguments (like filters), and some don't. rather than defining two separate queries, I would like to be able to define a variable (e.g. query Foo($arg: Boolean = null) { filed(arg: $arg) } or equivalently query Foo($arg: Boolean) { field: $arg } and pass in {"arg": null} whenever I want to avoid the optional argument. This gets particularly cumbersome if there are multiple optional arguements and currently I need to define multiple queries that are exactly equivalent but for one optional argument i.

  • Please tell us about your environment:

    • Version: graphene 2.1.9
    • Platform: python3.8

yeedle avatar Sep 05 '21 19:09 yeedle

@yeedle v3 of graphene supports using null literals. There is a beta that you can try.

jkimbo avatar Sep 06 '21 07:09 jkimbo

exp

  • What is the current behavior? It breaks when a literal null value is provided to a nullable field argument
from graphene import ObjectType, String, Schema, Argument

class Query(ObjectType):
  hello = String(name=Argument(String, required=False, default_value=None))

  def resolve_hello(root, info, name=None):
    return f'Hello {name}!'

schema = Schema(query=Query)

query_with_argument = 'query { hello(name: null) }'
result = schema.execute(query_with_argument)
print(result)

output:

{'errors': [{'message': 'Syntax Error GraphQL (1:21) Unexpected Name "null"\n\n1: query { hello(name: null) }\n                       ^\n', 'locations': [{'line': 1, 'column': 21}]}]}

Check out the repl.it repro.

  • What is the expected behavior?

According to the spec query { field(arg: null) } and query { field } are both valid. The expected behavior would be for the above to output:

{'data': {'hello': 'Hello None!'}}
  • What is the motivation / use case for changing the behavior? I have a queries (and fragments) that get reused in multiple places in the codebase. Some places require the use of optional arguments (like filters), and some don't. rather than defining two separate queries, I would like to be able to define a variable (e.g. query Foo($arg: Boolean = null) { filed(arg: $arg) } or equivalently query Foo($arg: Boolean) { field: $arg } and pass in {"arg": null} whenever I want to avoid the optional argument. This gets particularly cumbersome if there are multiple optional arguements and currently I need to define multiple queries that are exactly equivalent but for one optional argument i.

  • Please tell us about your environment:

    • Version: graphene 2.1.9
    • Platform: python3.8

Experienced the same issue. to make it work I used to send NULL

NwawelAIroume avatar Aug 12 '22 16:08 NwawelAIroume

@NwawelAIroume are you on 3.x already?

erikwrede avatar Aug 12 '22 17:08 erikwrede