问题描述
我在使用 graphql
的这种方法添加解析器时遇到问题:
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
文件内容:
type Query {
getClaimSubType: [DictionaryItemWithParentDto]
}
type DictionaryItemWithParentDto {
code: String!
name: String
parents: [DictionaryItemDto]
}
type DictionaryItemDto {
code: String!
name: String
description: String
}
在Java中,我有 Vehicle
接口和两个实现该接口的类: Airplane
和 Car
.当我添加到架构时,此行:
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
我收到以下错误:
There is no type resolver defined for interface / union 'Vehicle' type, There is no type resolver defined for interface / union 'SearchResult' type]}
我不确定如何处理.
如果相反,我添加:
interface Vehicle {
maxSpeed: Int
}
type Airplane implements Vehicle {
maxSpeed: Int
wingspan: Int
}
type Car implements Vehicle {
maxSpeed: Int
licensePlate: String
}
我收到以下错误:
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?
修改
添加这些代码行可以部分解决此问题:
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");
}
}
};
并在 RuntimeWiring
构建器中添加以下内容:
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);
}
询问时:
query {
getVehicle(maxSpeed: 30) {
maxSpeed
}
}
我得到了 maxSpeed
,但是当我添加 wingspan
时,我得到了一个错误
I get the maxSpeed
but when i add wingspan
i get an error
Field 'wingspan' in type 'Vehicle' is undefined @ 'getVehicle/wingspan'",
我添加了
getVehicle(maxSpeed: Int): Vehicle
到 graphqls
文件.我以为多态性可以在这里工作.
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");
}
}
};
并在 RuntimeWiring
构建器中添加以下内容:
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);
}
询问时:
query {
getVehicle(maxSpeed: 30) {
maxSpeed
}
}
我得到了 maxSpeed
,但是当我添加 wingspan
时,我得到了一个错误
I get the maxSpeed
but when i add wingspan
i get an error
Field 'wingspan' in type 'Vehicle' is undefined @ 'getVehicle/wingspan'",
我添加了
getVehicle(maxSpeed: Int): Vehicle
到 graphqls
文件.我以为多态性可以在这里工作.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!