Skip to content

Errors in a non-nullable field throw exception out of GraphQL.execute() #268

Closed

Description

@dllx

When an exception (field error) happens during resolution of a field, completeValue is called with a null resolvedValue. If the field is a GraphQLNonNull, that leads to a GraphQLException being thrown which is never caught.

According to the GraphQL spec, 6.4.4 Errors and Nullability (http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability), the null value should be propagated up to the parent field in this case (and if that is non-nullable, up again until ultimately a null value under the data key of the response object), without adding additional errors to the result error list.

Activity

  1. dminkovsky commented on Dec 15, 2016

    @dminkovsky
    Contributor
  2. added this to the 2.3.0 milestone on Dec 15, 2016
  3. dllx commented on Dec 16, 2016

    @dllx
    Author

    Thank you, that would be great. No, I don't have a patch for that ready, sorry.

  4. modified the milestones: 2.2, 2.3.0 on Jan 3, 2017
  5. dminkovsky commented on Jan 3, 2017

    @dminkovsky
    Contributor

    This didn't make 2.3.0 but should be included in 2.4.0. Thanks again for reporting @dllx.

  6. jplock commented on Jan 3, 2017

    @jplock

    @dminkovsky when 2.4.0 is tagged, would it be possible to update https://github.com/graphql-java/graphql-java-servlet as well?

  7. dminkovsky commented on Jan 3, 2017

    @dminkovsky
    Contributor

    I believe graphql-java-servlet is maintained by @yrashk. Any particular reason not 2.3.0, too?

  8. jplock commented on Jan 3, 2017

    @jplock

    Nope, it wasn't updated for 2.3.0 yet either, but that would be appreciated. :)

  9. vojtechmasa commented on Mar 5, 2017

    @vojtechmasa

    Do you have ETA to resolve this issue? Thank you.

  10. bbakerman commented on Apr 1, 2017

    @bbakerman
    Member

    I started to look into this problem and the code as is has some serious trouble in implementing this

    The spec says

    http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability

    Since Non-Null type fields cannot be null, field errors are propagated to be handled by the parent field. If the parent field may be null then it resolves to null, otherwise if it is a Non-Null type, the field error is further propagated to it’s parent field.
    
    If all fields from the root of the request to the source of the error return Non-Null types, then the  "data" entry in the response should be null.
    

    Currently the invocation of resolveField is recursive. It descends down into the child fields but it does NOT allow for a result such that this non null precondition can bubble up to the calling layers.

    Today is considers the parent object "resolved" then looks for child field objects. But if one of the children is non null then it must make the parent null and the code today cant easily do that. I think this will take some refactoring to come to spec

    this test should pass

         def "#268 - null child field values are allowed in nullable parent type"() {
    
        // see https://github.com/graphql-java/graphql-java/issues/268
    
        given:
    
    
        GraphQLOutputType parentType = newObject()
                .name("parentType")
                .field(newFieldDefinition().name("nullChild")
                .type(new GraphQLNonNull(GraphQLString)))
                .field(newFieldDefinition().name("nonNullChild")
                .type(new GraphQLNonNull(GraphQLString)))
                .build()
    
        GraphQLSchema schema = newSchema().query(
                newObject()
                        .name("RootQueryType")
                        .field(newFieldDefinition()
                        .name("parent").type(parentType)
                        .dataFetcher({ env -> new ParentTypeImplementation() })
    
                ))
                .build()
    
        def query = """
        query { 
            parent {
                nonNullChild
                nullChild
            }
        }
        """
    
        when:
        def result = GraphQL.newGraphQL(schema).build().execute(query)
    
        then:
    
        result.errors.size() == 0
        result.data["parent"] == null
    }
    
  11. bbakerman commented on Apr 1, 2017

    @bbakerman
    Member

    Also another hard problem here is the AbstractExecutionStrategy uses inheritance and hence each other type level code MUST now handle this exception themselves, and it cant be done in once place

  12. bbakerman commented on Apr 3, 2017

    @bbakerman
    Member

    OK having struggled with a fix for this locally I think the fundamental problem is that the graphql types system we have in the library does not hold hierarchy information and the concept of non nullness on the type itself

    The spec says http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability it should bubble up to the parent type

    But we cant currently do that since the code only knows about the "current type" and graphqlnonnull type is a dumb simple wrapper.

    So given a type T (that we have unwrapped) we cant easily answer the questions

    • Is this a non null type
    • What is the parent type of T (and its parents etc..)

    I think we need to introduce a TypeInfo kind of thing for "execution" so we can help answer these questions

  13. dllx commented on Apr 3, 2017

    @dllx
    Author

    Unfortunately, I have very few time looking into this. In my Ceylon implementation of a GraphQL engine (https://github.com/dlkw/ceylon-graphql, see code at https://github.com/dlkw/ceylon-graphql/blob/master/source/de/dlkw/graphql/exp/Schema.ceylon#L664) I have split the completeValues function and used an input flag to signal "this happens in a non null field" and a special return value to signal the error the non-null case.

    Maybe this is transferrable to this project and you can take some inspiration from there.

  14. added a commit that references this issue on Apr 3, 2017
  15. bbakerman commented on Apr 3, 2017

    @bbakerman
    Member

    Yeah I used exceptions in the new code which is logically equivalent to Either types X | Y

  16. added this to the 3.0.0 milestone on Apr 13, 2017
  17. added a commit that references this issue on Apr 16, 2017
  18. andimarek commented on May 14, 2017

    @andimarek
    Member

    this solved by #353

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions