GraphQL – SchemaError:

huangapple go评论76阅读模式
英文:

GraphQL - SchemaError:

问题

使用Java构建微服务 -

spring-boot版本2.2.6.RELEASE
graphql-spring-boot-starter版本5.0.2

尝试使用GraphQL mutation将记录持久化到MongoDB,我成功地通过以下方式持久化了单个对象 -

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

但是,当尝试使用嵌套对象时,我看到以下错误 -

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?

我的Schema如下 -

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类和Mutation如下 -

@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>{
}

我想在MongoDB中保存以下JSON表示 -

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

我尝试了很多方法,但每次启动服务器时都会收到相同的异常。上面的JSON表示是一个简单的示例。我想要保存更复杂的表示。与在线示例不同的是,我不想为子元素创建单独的Mongo对象,就像在线示例中的Book-Author示例那样。

英文:

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=&quot;custom_element&quot;)
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&lt;ParentElement&gt; findAllElements() {
	    return elementRepository.findAll();
	}
}

@Repository
public interface ElementRepository extends MongoRepository&lt;ParentElement, String&gt;{
}

I want to save following json representation in mongo db -

{
    &quot;id&quot;: &quot;custom_id&quot;,
    &quot;type&quot;: &quot;custom_type&quot;,
    &quot;child&quot;: {
      &quot;make&quot;: &quot;custom_make&quot;,
      &quot;model&quot;: &quot;Toyota V6&quot;,
     }
}

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

得分: 2

GraphQL中的typeinput是两种不同的东西。您不能将type用作mutation的input。这正是异常的原因:Expected type 'ChildElement' to be a GraphQLInputType, but it wasn't,库期望一个input,但找到了其他东西(一个对象类型)。

要解决这个问题,请创建一个子输入类型:

input ChildInput {
  make: String
  model: String
}

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

您还可以参考这个问题:https://stackoverflow.com/questions/41515679/can-you-make-a-graphql-type-both-an-input-and-output-type

英文:

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 &#39;ChildElement&#39; to be a GraphQLInputType, but it wasn&#39;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: https://stackoverflow.com/questions/41515679/can-you-make-a-graphql-type-both-an-input-and-output-type

huangapple
  • 本文由 发表于 2020年8月6日 14:09:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63277798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定