Можем ли мы вызвать GraphQL Query в моем Spring Boot REST api
Я новичок в GraphQL, у меня есть один сценарий, в котором мне нужно добавить новую конечную точку REST в микросервис и внутренне вызвать GraphQLQuery, чтобы использовать полезную нагрузку ответа в службе.
как этого добиться?
Я пробовал это, но получаю исключение:
GraphQLObjectType.Builder queryBuilder = GraphQLObjectType.newObject()
.name("GetAllCatalogCategories")
.description("Get All Catalog Categories");
GraphQLSchema schema = GraphQLSchema.newSchema()
.query(queryBuilder)
.build();
GraphQL graphQL = GraphQL.newGraphQL(schema)
.build();
String variableJson = "{ \"catalogueId\": \"9\", \"searchTerm\": \"\", \"allLevels\": true, \"firstCourses\": 5, \"filter\": { \"deliveryMethods\": [], \"supplierIds\": [] } }";
try {
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<>() {
};
HashMap<String, Object> variableMap = new ObjectMapper().readValue(variableJson, typeRef);
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query("query GetAllCatalogCategories($catalogueId: ID!, $searchTerm: String, $allLevels: Boolean, $firstCourses: Int!, $filter: CatalogueCourseFilterInput!) { catalogue(catalogueId: $catalogueId) { id courses(first: 0, searchTerm: $searchTerm, courseGroup: CATEGORIZED, catalogueCourseFilterInput: $filter) { totalCount } categories(searchTerm: $searchTerm, allLevels: $allLevels, catalogueCourseFilterInput: $filter) { totalCount nodes { id name coursesCount parentCategoryId subCategoriesCount courses: courses(first: $firstCourses, searchTerm: $searchTerm, catalogueCourseFilterInput: $filter) { totalCount nodes { id code title price{ buyPrice{ publicPrice{ baseCost } clientPrice{ baseCost forecast } kpPrice{ forecast{ cost } budgeted{ cost } } supplierPrice{ budgeted{ cost } forecast{ cost } supplier{ id details{ exclFromSellPriceCalc } } } } costType sellPriceOverride{ perDelegateClientPrice perDelegateKpPrice perDelegatePublicPrice } } deliveryMethod supplier { details { name } } } } } } } }")
.variables(variableMap)
.operationName("GetAllCatalogCategories")
.build();
ExecutionResult executionResult = graphQL.execute(executionInput);
Object data = executionResult.getData();
List<GraphQLError> errors = executionResult.getErrors();
} catch (JsonProcessingException e) {
}
Это дало мне исключение ниже:
Blockquote ExecutionResultImpl {errors = [ValidationError {validationErrorType = UnknownType, queryPath = null, message = Ошибка проверки типа UnknownType: неизвестный идентификатор типа, location = [SourceLocation{line = 1, column = 45}], description = 'Unknown type ID'}, ValidationError{validationErrorType = UnknownType, queryPath = null, message = Ошибка проверки типа UnknownType: Неизвестный тип Int, location = [SourceLocation{line = 1, column = 107}], description = 'Unknown type Int'}, ValidationError{validationErrorType = UnknownType, queryPath = null, message = Ошибка валидации типа UnknownType: Неизвестный тип CatalogueCourseFilterInput, location = [SourceLocation{line = 1, column = 122}], description = 'Неизвестный тип CatalogueCourseFilterInput'}, ValidationErrorTyperror {validationError {validationError {validationError} queryPath = [каталог], message = Ошибка проверки типа FieldUndefined:Поле 'catalog' в типе 'GetAllCatalogCategories' не определено @ 'catalog', locations = [SourceLocation{line = 1, column = 153}], description = 'Field' catalog 'в типе' GetAllCatalogCategories 'не определено'}], data = null, dataPresent = false, extensions = null}