У меня проблема с добавлением распознавателя с использованием этого подхода в 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
. Я думал, что полиморфизм будет работать здесь.