Graphql 没有为接口/联合定义解析器 - java [英] Graphql no resolver definied for interface/union - java
问题描述
我在 graphql
中使用这种方法添加解析器时遇到问题:
@RestController@RequestMapping("/api/dictionary/")@RequiredArgsConstructor(onConstructor = @__(@Autowired))公共类字典控制器{@Value("classpath:items.graphqls")私有资源架构资源;私有 GraphQL graphQL;私人最终字典服务字典服务;@PostConstructpublic void loadSchema() 抛出 IOException {文件 schemaFile = schemaResource.getFile();TypeDefinitionRegistry registry = new SchemaParser().parse(schemaFile);RuntimeWiring 接线 = buildWiring();GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(registry,wiring);graphQL = GraphQL.newGraphQL(schema).build();}私有 RuntimeWiring buildWiring() {DataFetcher<List<DictionaryItemWithParentDto>>fetcher6 = dataFetchingEnvironment ->dictionaryService.getClaimSubType();返回 RuntimeWiring.newRuntimeWiring().type("查询", typeWriting ->打字.dataFetcher("getClaimSubType", fetcher6)).建造();}公共列表getClaimSubType() {返回 dictionaryService.getClaimSubType();}}
items.graphqls
文件内容:
类型查询{getClaimSubType: [DictionaryItemWithParentDto]}输入 DictionaryItemWithParentDto {代码:字符串!名称:字符串父母:[DictionaryItemDto]}输入 DictionaryItemDto {代码:字符串!名称:字符串描述:字符串}
在 Java 中,我有 Vehicle
接口和两个实现它的类:Airplane
和 Car
.当我将此行添加到架构时:
union SearchResult = 飞机 |车
我收到以下错误:
没有为 interface/union 'Vehicle' 类型定义类型解析器,没有为 interface/union 'SearchResult' 类型定义类型解析器]}
我不知道如何处理.
如果我添加:
interface Vehicle {最大速度:整数}类型飞机执行车辆{最大速度:整数翼展:Int}类型 Car 执行 Vehicle {最大速度:整数牌照:字符串}
我收到以下错误:
errors=[没有为接口/联合车辆"类型定义类型解析器]
如何使用我的方法处理这些错误?有没有其他方法来处理它?</p>
编辑
添加这些代码行我猜可以解决这个问题:
TypeResolver t = new TypeResolver() {@覆盖公共 GraphQLObjectType getType(TypeResolutionEnvironment env) {对象 javaObject = env.getObject();if (javaObject instanceof Car) {返回 env.getSchema().getObjectType("Car");} else if (javaObject instanceof Airplane) {返回 env.getSchema().getObjectType("飞机");} 别的 {返回 env.getSchema().getObjectType("Car");}}};
并添加到 RuntimeWiring
构建器:
.type("Vehicle", typeWriting ->打字.typeResolver(t))@PostMapping("getVehicle")公共响应实体<对象>getVehicleMaxSpeed(@RequestBody String 查询){ExecutionResult 结果 = graphQL.execute(query);返回新的 ResponseEntity
要求时:
查询{getVehicle(最大速度:30){最大速度}}
我得到了 maxSpeed
但是当我添加 wingspan
时我得到一个错误
'Vehicle' 类型中的字段 'wingspan' 未定义 @'getVehicle/wingspan'",
我加了
getVehicle(maxSpeed: Int):车辆
到 graphqls
文件.我认为多态在这里会起作用.
添加以下代码行解决问题:
TypeResolver t = new TypeResolver() {@覆盖公共 GraphQLObjectType getType(TypeResolutionEnvironment env) {对象 javaObject = env.getObject();if (javaObject instanceof Car) {返回 env.getSchema().getObjectType("Car");} else if (javaObject instanceof Airplane) {返回 env.getSchema().getObjectType("飞机");} 别的 {返回 env.getSchema().getObjectType("Car");}}};
并添加到 RuntimeWiring
构建器:
.type("Vehicle", typeWriting ->打字.typeResolver(t))@PostMapping("getVehicle")公共响应实体<对象>getVehicleMaxSpeed(@RequestBody String 查询){ExecutionResult 结果 = graphQL.execute(query);返回新的 ResponseEntity
要求时:
查询{getVehicle(最大速度:30){最大速度}}
我得到了 maxSpeed
但是当我添加 wingspan
时我得到一个错误
'Vehicle' 类型中的字段 'wingspan' 未定义 @'getVehicle/wingspan'",
我加了
getVehicle(maxSpeed: Int):车辆
到 graphqls
文件.我认为多态在这里会起作用.
如果我想要子类的字段,我可以这样要求:
查询{getVehicle(最大速度:10){最大速度...在飞机上{翼展}...在车上{牌照}}}
I have problem adding resolver using this approach in graphql
:
@RestController
@RequestMapping("/api/dictionary/")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DictionaryController {
@Value("classpath:items.graphqls")
private Resource schemaResource;
private GraphQL graphQL;
private final DictionaryService dictionaryService;
@PostConstruct
public void loadSchema() throws IOException {
File schemaFile = schemaResource.getFile();
TypeDefinitionRegistry registry = new SchemaParser().parse(schemaFile);
RuntimeWiring wiring = buildWiring();
GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(registry, wiring);
graphQL = GraphQL.newGraphQL(schema).build();
}
private RuntimeWiring buildWiring() {
DataFetcher<List<DictionaryItemWithParentDto>> fetcher6 = dataFetchingEnvironment -> dictionaryService.getClaimSubType();
return RuntimeWiring.newRuntimeWiring()
.type("Query", typeWriting ->
typeWriting
.dataFetcher("getClaimSubType", fetcher6)
)
.build();
}
public List<DictionaryItemWithParentDto> getClaimSubType() {
return dictionaryService.getClaimSubType();
}
}
items.graphqls
file content:
type Query {
getClaimSubType: [DictionaryItemWithParentDto]
}
type DictionaryItemWithParentDto {
code: String!
name: String
parents: [DictionaryItemDto]
}
type DictionaryItemDto {
code: String!
name: String
description: String
}
In java I have Vehicle
interface and two classes that implement it: Airplane
and Car
. When i add to schema this line:
union SearchResult = Airplane | Car
I get following error:
There is no type resolver defined for interface / union 'Vehicle' type, There is no type resolver defined for interface / union 'SearchResult' type]}
I am not sure how to handle it.
If instead i add:
interface Vehicle {
maxSpeed: Int
}
type Airplane implements Vehicle {
maxSpeed: Int
wingspan: Int
}
type Car implements Vehicle {
maxSpeed: Int
licensePlate: String
}
I get following error:
errors=[There is no type resolver defined for interface / union 'Vehicle' type]
How can i handle these errors using my approach ? Is there another approach to handle it?
Edit
Adding these lines of code fix the issue partway i guess:
TypeResolver t = new TypeResolver() {
@Override
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
Object javaObject = env.getObject();
if (javaObject instanceof Car) {
return env.getSchema().getObjectType("Car");
} else if (javaObject instanceof Airplane) {
return env.getSchema().getObjectType("Airplane");
} else {
return env.getSchema().getObjectType("Car");
}
}
};
And adding to RuntimeWiring
builder this:
.type("Vehicle", typeWriting ->
typeWriting
.typeResolver(t)
)
@PostMapping("getVehicle")
public ResponseEntity<Object> getVehicleMaxSpeed(@RequestBody String query)
{
ExecutionResult result = graphQL.execute(query);
return new ResponseEntity<Object>(result, HttpStatus.OK);
}
When asking for:
query {
getVehicle(maxSpeed: 30) {
maxSpeed
}
}
I get the maxSpeed
but when i add wingspan
i get an error
Field 'wingspan' in type 'Vehicle' is undefined @ 'getVehicle/wingspan'",
I added
getVehicle(maxSpeed: Int): Vehicle
To the graphqls
file. I thought that polymorphism would work here.
Adding these lines of code fix the issue:
TypeResolver t = new TypeResolver() {
@Override
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
Object javaObject = env.getObject();
if (javaObject instanceof Car) {
return env.getSchema().getObjectType("Car");
} else if (javaObject instanceof Airplane) {
return env.getSchema().getObjectType("Airplane");
} else {
return env.getSchema().getObjectType("Car");
}
}
};
And adding to RuntimeWiring
builder this:
.type("Vehicle", typeWriting ->
typeWriting
.typeResolver(t)
)
@PostMapping("getVehicle")
public ResponseEntity<Object> getVehicleMaxSpeed(@RequestBody String query)
{
ExecutionResult result = graphQL.execute(query);
return new ResponseEntity<Object>(result, HttpStatus.OK);
}
When asking for:
query {
getVehicle(maxSpeed: 30) {
maxSpeed
}
}
I get the maxSpeed
but when i add wingspan
i get an error
Field 'wingspan' in type 'Vehicle' is undefined @ 'getVehicle/wingspan'",
I added
getVehicle(maxSpeed: Int): Vehicle
To the graphqls
file. I thought that polymorphism would work here.
If i want fields from subclasses i can ask for them like that:
query {
getVehicle(maxSpeed: 10) {
maxSpeed
... on Airplane {
wingspan
}
... on Car {
licensePlate
}
}
}
这篇关于Graphql 没有为接口/联合定义解析器 - java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!