Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using enums as argument default values is broken with graphql-core 3.1 #547

Closed
dkbarn opened this issue Apr 16, 2021 · 4 comments
Closed
Labels
bug Something isn't working
Milestone

Comments

@dkbarn
Copy link
Contributor

dkbarn commented Apr 16, 2021

Ariadne 0.13.0 updated its dependency on graphql-core from 3.0 to 3.1. Something has changed in the treatment of argument default values in this version of graphql-core which means that any schema which defines an argument's default value using an enum no longer works in ariadne. The introspection query which is issued by GraphQL Playground throws an error with such a schema. The only way to avoid the error is to remove the default values from the schema definition entirely.

Example:

import ariadne
import enum
import pprint

type_defs = """
    enum Role {
        ADMIN
        USER
    }

    type Query {
        hello(r: Role = USER): String
    }
"""

class Role(enum.Enum):
    ADMIN = "admin"
    USER = "user"

RoleGraphQLType = ariadne.EnumType("Role", Role)

QueryGraphQLType = ariadne.QueryType()

schema = ariadne.make_executable_schema(
    type_defs,
    QueryGraphQLType,
    RoleGraphQLType,
)

query = "{__schema{types{name,fields{name,args{name,defaultValue}}}}}"

result = ariadne.graphql_sync(schema, {"query": query}, debug=True)
pprint.pprint(result)

This works fine under the environment:

ariadne 0.12.0
graphql-core 3.0.5

But when run under the environment:

ariadne 0.13.0
graphql-core 3.1.4

It produces the error:

Traceback (most recent call last):
  File "venv/lib/python3.8/site-packages/graphql/execution/execute.py", line 678, in complete_value_catching_error
    completed = self.complete_value(
  File "venv/lib/python3.8/site-packages/graphql/execution/execute.py", line 745, in complete_value
    raise result
  File "venv/lib/python3.8/site-packages/graphql/execution/execute.py", line 637, in resolve_field_value_or_error
    result = resolve_fn(source, info, **args)
  File "venv/lib/python3.8/site-packages/graphql/type/introspection.py", line 399, in default_value
    value_ast = ast_from_value(item[1].default_value, item[1].type)
  File "venv/lib/python3.8/site-packages/graphql/utilities/ast_from_value.py", line 108, in ast_from_value
    serialized = type_.serialize(value)  # type: ignore
  File "venv/lib/python3.8/site-packages/graphql/type/definition.py", line 1146, in serialize
    raise GraphQLError(
graphql.error.graphql_error.GraphQLError: Enum 'Role' cannot represent value: 'USER'
@trbhoang
Copy link

This might work:

RoleGraphQLType = ariadne.EnumType("Role", {r.name: r.value for r in Role})

@hexular
Copy link

hexular commented Jul 20, 2021

Are there any updates on this, or any help needed fixing this issue?

@rafalp
Copy link
Contributor

rafalp commented Jul 20, 2021

This has PR that I need to give another look to see if it can be merged.

@rafalp
Copy link
Contributor

rafalp commented Nov 8, 2021

This appears to be working on master:

(True,
 {'data': {'__schema': {'types': [{'fields': None, 'name': 'Role'},
                                  {'fields': [{'args': [{'defaultValue': 'USER',
                                                         'name': 'r'}],
                                               'name': 'hello'}],
                                   'name': 'Query'},
                                  {'fields': None, 'name': 'String'},
                                  {'fields': None, 'name': 'Boolean'},
                                  {'fields': [{'args': [],
                                               'name': 'description'},
                                              {'args': [], 'name': 'types'},
                                              {'args': [], 'name': 'queryType'},
                                              {'args': [],
                                               'name': 'mutationType'},
                                              {'args': [],
                                               'name': 'subscriptionType'},
                                              {'args': [],
                                               'name': 'directives'}],
                                   'name': '__Schema'},
                                  {'fields': [{'args': [], 'name': 'kind'},
                                              {'args': [], 'name': 'name'},
                                              {'args': [],
                                               'name': 'description'},
                                              {'args': [],
                                               'name': 'specifiedByUrl'},
                                              {'args': [{'defaultValue': 'false',
                                                         'name': 'includeDeprecated'}],
                                               'name': 'fields'},
                                              {'args': [],
                                               'name': 'interfaces'},
                                              {'args': [],
                                               'name': 'possibleTypes'},
                                              {'args': [{'defaultValue': 'false',
                                                         'name': 'includeDeprecated'}],
                                               'name': 'enumValues'},
                                              {'args': [{'defaultValue': 'false',
                                                         'name': 'includeDeprecated'}],
                                               'name': 'inputFields'},
                                              {'args': [], 'name': 'ofType'}],
                                   'name': '__Type'},
                                  {'fields': None, 'name': '__TypeKind'},
                                  {'fields': [{'args': [], 'name': 'name'},
                                              {'args': [],
                                               'name': 'description'},
                                              {'args': [{'defaultValue': 'false',
                                                         'name': 'includeDeprecated'}],
                                               'name': 'args'},
                                              {'args': [], 'name': 'type'},
                                              {'args': [],
                                               'name': 'isDeprecated'},
                                              {'args': [],
                                               'name': 'deprecationReason'}],
                                   'name': '__Field'},
                                  {'fields': [{'args': [], 'name': 'name'},
                                              {'args': [],
                                               'name': 'description'},
                                              {'args': [], 'name': 'type'},
                                              {'args': [],
                                               'name': 'defaultValue'},
                                              {'args': [],
                                               'name': 'isDeprecated'},
                                              {'args': [],
                                               'name': 'deprecationReason'}],
                                   'name': '__InputValue'},
                                  {'fields': [{'args': [], 'name': 'name'},
                                              {'args': [],
                                               'name': 'description'},
                                              {'args': [],
                                               'name': 'isDeprecated'},
                                              {'args': [],
                                               'name': 'deprecationReason'}],
                                   'name': '__EnumValue'},
                                  {'fields': [{'args': [], 'name': 'name'},
                                              {'args': [],
                                               'name': 'description'},
                                              {'args': [],
                                               'name': 'isRepeatable'},
                                              {'args': [], 'name': 'locations'},
                                              {'args': [], 'name': 'args'}],
                                   'name': '__Directive'},
                                  {'fields': None,
                                   'name': '__DirectiveLocation'}]}}})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants