Java 图ql-方案错误:
使用java构建微服务- spring boot版本2.2.6.0发布 graphql弹簧启动启动器版本5.0.2 尝试使用graphql变异在MongoDB中持久化记录,我成功地通过如下所示的单个对象持久化了记录-Java 图ql-方案错误:,java,mongodb,spring-boot,graphql-java,Java,Mongodb,Spring Boot,Graphql Java,使用java构建微服务- spring boot版本2.2.6.0发布 graphql弹簧启动启动器版本5.0.2 尝试使用graphql变异在MongoDB中持久化记录,我成功地通过如下所示的单个对象持久化了记录- type ParentElement { id: ID! type: String child: String } 但当尝试使用嵌套对象时,我看到以下错误- 原因:com.coxautodev.graphql.tools.SchemaError:类型“ChildEle
type ParentElement {
id: ID!
type: String
child: String
}
但当尝试使用嵌套对象时,我看到以下错误-
原因:com.coxautodev.graphql.tools.SchemaError:类型“ChildElement”应为GraphQLInputType,但不是!类型是否仅允许对象类型被错误地用作输入类型,反之亦然
我的模式如下-
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
}
@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>{
}
Pojo类和突变如下-
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
}
@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>{
}
我尝试了几种方法,但在启动服务器时总是出现相同的异常。上面的json是一个简单的表示。我想保存更复杂的一个,与在线可用的其他示例不同的是,我不想为子元素创建单独的mongo对象,如在线可用的Book Author示例所示。Graphql
type
和input
是两件不同的事情。不能将类型
用作变异输入
。这正是异常的原因:期望类型“ChildElement”是GraphQLInputType,但它不是
,库期望输入,但找到了其他类型(对象类型)
要解决该问题,请创建子输入:
input ChildInput {
make: String
model: String
}
type Mutation {
createElement(id: String, type: String, child: ChildInput): ParentElement
}
你也可以看看这个问题:图形ql
类型
和输入
是两个不同的东西。不能将类型
用作变异输入
。这正是异常的原因:期望类型“ChildElement”是GraphQLInputType,但它不是
,库期望输入,但找到了其他类型(对象类型)
要解决该问题,请创建子输入:
input ChildInput {
make: String
model: String
}
type Mutation {
createElement(id: String, type: String, child: ChildInput): ParentElement
}
您也可以看看这个问题:感谢AlliorionX提供的详细信息。我把它改成了Input&它对我很有用。谢谢AlliorionX提供的详细信息。我把它改为输入&它对我有用。