Search code examples
graphqlgraphql-java

graphql-java: Can an object definition be both a GraphQLObjectType & GraphQLInputObjectType


I'm using graphql-java. I have customized object, e.g.: MyObject (which has some fields...).

The request is that i want to use it as a Parameter in another mutation object. And i don't want to define it twice (GraphQLObjectType & GraphQLInputObjectType).

Could it be done and how to do it? thanks

  • GraphQLObjectType

    public GraphQLObjectType myObjectType() {
        return newObject()
                .name("MyObj")
                .description("My object")
    
                .field(newFieldDefinition()
                        .name("field1")
                        .description("field 1")
                        .type(GraphQLString))
                // ...
                .build();
    }
    
  • GraphQLInputObjectType

    public GraphQLInputObjectType myInputObjectType() {
        return newInputObject()
            .name("MyInputObj")
            .description("My input object")
    
            .field(newInputObjectField()
                    .name("field1")
                    .description("field 1")
                    .type(GraphQLString))
            // ...
            .build();
        }
    

Solution

  • No, they're quite different things, and you'll usually find your input types diverge from the equivalent object types anyway. I'd rely on other patterns for code-reuse instead.