Skip to content

assertion error in ast_from_value during introspection query when providing enum member as default value #756

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

Closed
DrPyser opened this issue Jun 5, 2018 · 5 comments

Comments

@DrPyser
Copy link

DrPyser commented Jun 5, 2018

So I have an InputObjectType with an Enum field, and I want to provide a default value. The enum field is created from a pre-existing python enum:

MyGrapheneEnumType = graphene.Enum.from_enum(
    MyEnumType
)
class MyInput(graphene.InputObjectType):
    my_enum_field = graphene.Field(MyGrapheneEnumType, default_value=MyGrapheneEnumType.MEMBER)

I'm not sure it's the right way to do it(documentation would help), but I don't think an assertion error is the expected result.

If I then do an introspection query(I'm using insomnia which does them automatically), like this one:

query IntrospectionQuery {
      __schema {
        queryType { name }
        mutationType { name }
        subscriptionType { name }
        types {
          ...FullType
        }
        directives {
          name
          description
          locations
          args {
            ...InputValue
          }
        }
      }
    }

    fragment FullType on __Type {
      kind
      name
      description
      fields(includeDeprecated: true) {
        name
        description
        args {
          ...InputValue
        }
        type {
          ...TypeRef
        }
        isDeprecated
        deprecationReason
      }
      inputFields {
        ...InputValue
      }
      interfaces {
        ...TypeRef
      }
      enumValues(includeDeprecated: true) {
        name
        description
        isDeprecated
        deprecationReason
      }
      possibleTypes {
        ...TypeRef
      }
    }

    fragment InputValue on __InputValue {
      name
      description
      type { ...TypeRef }
      defaultValue
    }

    fragment TypeRef on __Type {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                  ofType {
                    kind
                    name
                  }
                }
              }
            }
          }
        }
      }
    }

I get the following stack trace:

2018-06-05 03:05:09,963 - graphql.execution.executor - ERROR - An error occurred while resolving field __InputValue.defaultValue
Traceback (most recent call last):
  File "/home/drpyser/Documents/python/kioto_rest/.env/lib/python3.6/site-packages/graphql/execution/executor.py", line 311, in resolve_or_error
    return executor.execute(resolve_fn, source, info, **args)
  File "/home/drpyser/Documents/python/kioto_rest/.env/lib/python3.6/site-packages/graphql/execution/executors/sync.py", line 7, in execute
    return fn(*args, **kwargs)
  File "/home/drpyser/Documents/python/kioto_rest/.env/lib/python3.6/site-packages/graphql/type/introspection.py", line 349, in <lambda>
    else print_ast(ast_from_value(input_val.default_value, input_val))
  File "/home/drpyser/Documents/python/kioto_rest/.env/lib/python3.6/site-packages/graphql/utils/ast_from_value.py", line 49, in ast_from_value
    assert isinstance(value, dict)
AssertionError
Traceback (most recent call last):
  File "/home/drpyser/Documents/python/kioto_rest/.env/lib/python3.6/site-packages/graphql/execution/executor.py", line 330, in complete_value_catching_error
    exe_context, return_type, field_asts, info, result)
  File "/home/drpyser/Documents/python/kioto_rest/.env/lib/python3.6/site-packages/graphql/execution/executor.py", line 383, in complete_value
    raise GraphQLLocatedError(field_asts, original_error=result)
graphql.error.located_error.GraphQLLocatedError
@syrusakbary
Copy link
Member

default_value have to be MyGrapheneEnumType.MEMBER.value.
Let me know if this fixes your issue:

MyGrapheneEnumType = graphene.Enum.from_enum(
    MyEnumType
)
class MyInput(graphene.InputObjectType):
    my_enum_field = graphene.Field(MyGrapheneEnumType, default_value=MyGrapheneEnumType.MEMBER.value)

@DrPyser
Copy link
Author

DrPyser commented Jun 5, 2018

Yup, it does. Is it possible to explain why? I feel the default value should be of the same type as the field.

@DrPyser
Copy link
Author

DrPyser commented Jun 5, 2018

Also, I feel this should raise a more user friendly exception, like a type error.

zhammer added a commit to zhammer/morning-cd that referenced this issue Oct 4, 2018
zhammer added a commit to zhammer/morning-cd that referenced this issue Oct 5, 2018
zhammer added a commit to zhammer/morning-cd that referenced this issue Oct 5, 2018
zhammer added a commit to zhammer/morning-cd that referenced this issue Oct 5, 2018
zhammer added a commit to zhammer/morning-cd that referenced this issue Oct 5, 2018
zhammer added a commit to zhammer/morning-cd that referenced this issue Oct 6, 2018
zhammer added a commit to zhammer/morning-cd that referenced this issue Oct 6, 2018
zhammer added a commit to zhammer/morning-cd that referenced this issue Oct 11, 2018
@zhammer
Copy link

zhammer commented Oct 16, 2018

ugh i'm super sorry for this ^. just realized this is happening while going through my issues. i put an internal note in a commit on a development branch and have done a bunch of rebases. i'll remove the comment on my next rebase.

@stale
Copy link

stale bot commented Aug 3, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Aug 3, 2019
@stale stale bot closed this as completed Aug 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants