0

Building a microservice in java using -

spring-boot version 2.2.6.RELEASE graphql-spring-boot-starter version 5.0.2

Trying to persist record in MongoDB using graphql mutation, I successfully persisted through Single Object like below -

type ParentElement {
  id: ID!
  type: String
  child: String
}

But when trying out with nested object, I am seeing following error -

Caused by: com.coxautodev.graphql.tools.SchemaError: Expected type 'ChildElement' to be a GraphQLInputType, but it wasn't! Was a type only permitted for object types incorrectly used as an input type, or vice-versa?

My Schema is as follows -

schema {
    query: Query
    mutation: Mutation
}

type ChildElement {
  make: String
  model: String
}

type ParentElement {
  id: ID!
  type: String
  child: ChildElement
}

type Query {
    findAllElements: [ParentElement]
}

type Mutation {
    createElement(id: String, type: String, child: ChildElement): ParentElement
}

Pojo Classes & Mutation are as follows -

@Document(collection="custom_element")
public class ParentElement {    
    private String id;
    private String type;
    private ChildElement child;
}

public class ChildElement {
    private String make;
    private String model;
}

@Component
public class ElementMutation implements GraphQLMutationResolver {
    private ElementRepository elementRepository;
    public ElementMutation(ElementRepository elementRepository) {
        this.elementRepository = elementRepository;
    }
    public ParentElement createElement(String id, String type, ChildElement child) {        
        ParentElement element = new ParentElement()

        elementRepository.save(element);
        return element;
    }
}

@Component
public class ElementQuery implements GraphQLQueryResolver {
    private ElementRepository elementRepository;
    @Autowired
    public ElementQuery(ElementRepository elementRepository) {
        this.elementRepository = elementRepository;
    }
    public Iterable<ParentElement> findAllElements() {
        return elementRepository.findAll();
    }
}

@Repository
public interface ElementRepository extends MongoRepository<ParentElement, String>{
}

I want to save following json representation in mongo db -

{
    "id": "custom_id",
    "type": "custom_type",
    "child": {
      "make": "custom_make",
      "model": "Toyota V6",
     }
}

I tried several things but when starting server always getting same exception. The above json is a simple representation. I want to save much more complex one, the difference with other examples available online is that I don't want to create separate mongo object for child element as shown with Book-Author example available online.

1 Answer 1

2

Graphql type and input are two different things. You cannot use a type as a mutation input. That is exactly what the exception is about: Expected type 'ChildElement' to be a GraphQLInputType, but it wasn't, the library was expecting an input but found something else (an object type).

To solve that problem, create a child input:

input ChildInput {
  make: String
  model: String
}

type Mutation {
    createElement(id: String, type: String, child: ChildInput): ParentElement
}

You can also have a look at this question: Can you make a graphql type both an input and output type?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You AlliorionX for the detail. I changed it to Input & it worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.