Skip to content

can't use typereference as possibleType in uniontype #382

Closed

Description

@jauco

A uniontype accepts only ObjectTypes as argument for possibleType. Not a TypeReference.

        newUnionType()
          .possibleType(newObject().build()) //Works
          .possibleType(new GraphQLTypeReference("SomeOtherType")) //doesn't work!
          .build();

Is this on purpose? Is there a workaround to allow two objects to reference each other in fields with a union type? i.e. How can I make the code below work?

GraphQLObjectType extraObject = newObject()
  .name("extraObject")
  .field(newFieldDefinition()
    .name("some field")
    .type(Scalars.GraphQLString)
  ).build();

GraphQLObjectType objA = newObject()
  .name("objA")
  .field(newFieldDefinition()
    .name("some field")
    .type(newUnionType()
      .possibleType(extraObject)
      .possibleType(new GraphQLTypeReference("objB")) //doesn't work
    )
  ).build();
GraphQLObjectType objB = newObject()
  .name("objB")
  .field(newFieldDefinition()
    .name("some field")
    .type(newUnionType()
      .possibleType(extraObject)
      .possibleType(objA)
    )
  ).build();

Activity

  1. jauco commented on Apr 20, 2017

    @jauco
    Author

    I'm now fairly sure this is a bug:

    1. GraphQLUnionType line 39 contains a method for resolving type references.
    2. In the method a check is performed if the type is a type reference, this check will always fail because the type is an object type
    3. And of course you cannot add a type reference because the builder or constructor won't allow you to
  2. jauco commented on Apr 21, 2017

    @jauco
    Author

    I made a fix in jauco/graphql-java@85453f8483b74238bc303f0112776acca8be82e5 I'm not sure if this is the best solution, and I don't know how to run gradle unittests so I'm not submitting a PR right now.

    But if you think this is a good approach I will.

  3. bbakerman commented on Apr 27, 2017

    @bbakerman
    Member

    to run tests in gradle just do

    ./gradlew clean build

    This will build and run all the tests.

    I personally use IDEA and run the tests from inside the project.

  4. bbakerman commented on Apr 28, 2017

    @bbakerman
    Member

    Ok I looked into this issue. There is a way to do what you want but the method is perhaps hidden from you

    I created #393 to improve the documentation

    You need to use this format

    newUnionType()
        .name("Pet")
        .possibleType(GraphQLObjectType.reference("Cat"))
        .possibleType(DogType)
    

    This creates a reference that IS a GraphQLObjectType and also a TypeReference

  5. jauco commented on Apr 28, 2017

    @jauco
    Author

    thanks!

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions