问题描述
在初始化 graphql 模式时,我遇到了错误堆栈跟踪。
调用init方法失败;嵌套异常是 SchemaProblem{errors=[解析类型 'Query' [@6:1] 时不存在字段输入类型 'Long',解析类型 'ObjectRef' 时不存在字段类型 'Long' [@17: 1]]} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397) 在
其中 ObjectRef 是我的对象。
type ObjectRef{
id:Long
qualifier : String
}
我正在使用以下一组 jar 来编译我的项目。
compile files('libs/latest/graphql-java-servlet-6.1.3.jar')
compile files('libs/latest/graphql-java-16.2.jar')
compile files('libs/latest/graphql-java-tools-5.2.4.jar')
compile files('libs/latest/graphql-spring-boot-autoconfigure-5.0.2.jar')
compile files('libs/latest/graphql-spring-boot-starter-5.0.2.jar')
compile files('libs/latest/antlr4-runtime-4.9.2.jar')
compile files('libs/lombok-1.16.18.jar')
compile files('libs/latest/graphql-java-extended-scalars-16.0.1.jar')
这是我的架构加载器。
@PostConstruct
private void loadSchema() throws IOException {
// get the schema
File schemaFile = schemaResource.getFile();
System.out.println(schemaFile.getAbsolutePath());
// parse schema
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(schemaFile);
System.out.println("type Registry..."+typeRegistry);
RuntimeWiring wiring = buildRuntimeWiring();
System.out.println("wiring..."+wiring);
GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(typeRegistry,wiring);
System.out.println("schema..."+schema);
graphQL = GraphQL.newGraphQL(schema).build();
}
private RuntimeWiring buildRuntimeWiring() {
System.out.println("buildRuntimeWiring...");
return RuntimeWiring.newRuntimeWiring().type("Query",typeWiring -> typeWiring
.dataFetcher("a",customDataFetcher)
).build();
}
问候 库沙格拉
解决方法
我能够使用标量解决上述问题。我创建了一个名为 Long 的新标量,并在 .graphqls 中使用以下方式使用相同的标量。标量 Long return RuntimeWiring.newRuntimeWiring().type("Query",typeWiring -> typeWiring .dataFetcher("a",customDataFetcher).scalar(ExtendedScalars.GraphQLLong)).build();它还需要创建一个 ScalarType 实现以按预期方式运行。如果有人需要完整代码,请随时 ping 这张票。