我正在尝试读取通过后端变量传递的一些参数,让我们看看:(这个方法在里面AuthenticationService
,注入到我的 graphql 控制器中,见下文)
@GraphQLMutation(name = "getSessionToken")
public AuthReturn getSessionToken(@GraphQLArgument(name = "user") String u, @GraphQLArgument(name = "password") String p) {...}
这是我的graphQL请求:
mutation ($user: String!, $password: String!) {
getSessionToken(user: $user, password: $password) {
status
payload
}
}
和我的变量:
{ "user": "myuser", "password": "mypass"}
但是当我尝试运行此示例代码时,会显示以下错误:
{"timestamp":"2019-07-29T17:18:32.753+0000","status":400,"error":"Bad Request","message":"JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 162] (through reference chain: java.util.LinkedHashMap[\"variables\"])","path":"/graphql"}
[编辑] 这是我的控制器:
@RestController
public class GraphQLController {
private final GraphQL graphQL;
public GraphQLController(AgendamentoService agendamentos, ConfiguracaoService config, ProcessoService processos, ParametroService parametros, AuthenticationService autenticacao) {
GraphQLSchema schema = new GraphQLSchemaGenerator()
.withResolverBuilders(
//Resolve by annotations
new AnnotatedResolverBuilder())
.withOperationsFromSingletons(agendamentos, config, processos, parametros, autenticacao)
.withValueMapperFactory(new JacksonValueMapperFactory())
.generate();
graphQL = GraphQL.newGraphQL(schema).build();
}
@CrossOrigin
@PostMapping(value = "/graphql", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Map<String, Object> graphql(@RequestBody Map<String, String> request, HttpServletRequest raw) {
// em context estamos passando o Request, usamos para fazer as verificacoes de autenticacao com GraphQl
ExecutionResult executionResult = graphQL.execute(ExecutionInput.newExecutionInput()
.query(request.get("query"))
.operationName(request.get("operationName"))
.context(raw)
.build());
return executionResult.toSpecification();
}
}
但是如果我在没有按要求传递参数的情况下运行这个突变variables
,那么一切都会正常工作。如何将变量传递给我的 graphQl 请求?提前致谢。