使用此方法添加冲突解决程序时遇到问题
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
. 当我添加到架构时,此行:
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]
如何使用我的方法处理这些错误?还有其他方法来处理吗?
编辑
添加这些代码行可以部分解决问题,我想:
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
生成器:
.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
我得到一个错误
Field 'wingspan' in type 'Vehicle' is undefined @ 'getVehicle/wingspan'",
我补充说
getVehicle(maxSpeed: Int): Vehicle
到
graphqls
文件。我认为多态性在这里可以工作。