question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`make_executable_schema`'s omits default enum values in args and inputs

See original GitHub issue

GraphQL enums are represented in Python via the GraphQLEnumType. If those types are created from the SDL, they define no Python representation for GraphQL enum items. Developers can use Ariadne-provided EnumType to specify how enum items should be represented in Python themselves, but we also include “fallback” step that fills enum values with their str representations.

However neither of those approaches takes care of enum items defined as default values for field arguments. Eg:

type Query {
    users(role: UserRole = ADMIN): [User]
}

input FooBarInput {
    foo: FooEnum! = FOO
}

We will need to add additional step to make_executable_schema that gathers all enums from schema together with their Python representations, and then introspects schema’s fields values for places where field or input has default value type of enum. If this is the case, we should set correct default_value on GraphQLArgument in the schema.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
dkbarncommented, Apr 6, 2021

I don’t know if this is the right place to discuss this issue (it seems related to enums and default values):

There seems to be a regression in ariadne 0.13.0. Or at least an unexpected change in behaviour when I upgraded from 0.12.0.

If you run the following:

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.3

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'
0reactions
jhuitemacommented, Oct 13, 2021

Has there been any movement on the schema validation solution/workaround that would unblock this issue? My team is working on a project that is currently using workarounds for this and we’d like to remove them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't custom value of graphql enum - Stack Overflow
You are misunderstanding custom values. These will be the values on the server side not on the client side. GraphQL will always return...
Read more >
Schema Directives – GraphQL Tools
Using and implementing custom directives to transform schema types, fields, and arguments.
Read more >
Default enum associated values - Swift by Sundell
New in Swift 5.1: Enum cases can now specify defaults for their associated values — which comes very much in handy when we...
Read more >
MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
An ENUM is a string object with a value chosen from a list of permitted ... The strings you specify as input values...
Read more >
What you need to know about GraphQL enums
Apollo Server will resolve enum values to their proper internal values ( resolvers.AuthType ) for both input and output data ( Mutation ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found