突变在 Apollo-Android 上发送空参数
Mutation sending null parameters on Apollo-Android
我正在尝试将 Apollo-Android 与本地安装的服务器集成,显示所有条目的查询工作正常,但是当我尝试创建一个新条目时,它会发送一个查询没有参数。
起初我以为这是服务器端的问题,但看起来不像,因为它正在创建条目,但具有空值。
它在 Spring 上创建的服务器使用 SPQR 启动模式生成,并使用 SpringData-JPA 和 Hibernate 连接到 PostGreSQL 数据库。
服务器是这样说的:
Hibernate: insert into author (first_name, last_name) values (?, ?)
2018-06-25 14:48:25.479 INFO 6670 --- [nio-8080-exec-3] s.testing.controllers.GraphQLController : {data={createAuthor={__typename=Author, firstName=null, lastName=null}}}
这是我在 Android Studio 上得到的回复:
06-25 14:48:24.974 16068-16132/com.example.nburk.apollodemo D/graphcool: Executed mutation: CreateAuthor{__typename=Author, firstName=null, lastName=null}
在 Android Studio 中,这是生成器:
private void createPost(String firstname, String lastname) {
application.apolloClient().mutate(
CreateAuthorMutation.builder()
.firstName(firstname)
.lastName(lastname)
.build())
.enqueue(createPostMutationCallback);
}
排队这个:
private ApolloCall.Callback<CreateAuthorMutation.Data> createPostMutationCallback = new ApolloCall.Callback<CreateAuthorMutation.Data>() {
@Override
public void onResponse(@Nonnull final Response<CreateAuthorMutation.Data> dataResponse) {
Log.d(ApolloDemoApplication.TAG, "Executed mutation: " + dataResponse.data().createAuthor().toString());
fetchPosts();
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Log.d(ApolloDemoApplication.TAG, "Error:" + e.toString());
}
};
如果你需要它,这是我的突变:
mutation CreateAuthor($firstName: String, $lastName: String){
createAuthor(firstName: $firstName, lastName: $lastName){
firstName
lastName
}
}
本项目基于此:https://github.com/graphcool-examples/android-graphql/tree/master/quickstart-with-apollo
也在上传我的架构。
预先感谢您的帮助。
问题来自 SPQR,您需要为变量添加一个映射器
@PostMapping(value = "/graphql", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public Map<String, Object> indexFromAnnotated(@RequestBody Map<String, Object> request, HttpServletRequest raw) {
ExecutionResult executionResult = graphQL.execute(ExecutionInput.newExecutionInput()
.query((String) request.get("query"))
.operationName((String) request.get("operationName"))
// -------- THIS RESOLVES THE VARIABLES--------
.variables((Map<String, Object>) request.get("variables"))
.context(raw)
.build());
LOGGER.info(executionResult.toSpecification().toString());
return executionResult.toSpecification();
}
我正在尝试将 Apollo-Android 与本地安装的服务器集成,显示所有条目的查询工作正常,但是当我尝试创建一个新条目时,它会发送一个查询没有参数。
起初我以为这是服务器端的问题,但看起来不像,因为它正在创建条目,但具有空值。
它在 Spring 上创建的服务器使用 SPQR 启动模式生成,并使用 SpringData-JPA 和 Hibernate 连接到 PostGreSQL 数据库。
服务器是这样说的:
Hibernate: insert into author (first_name, last_name) values (?, ?) 2018-06-25 14:48:25.479 INFO 6670 --- [nio-8080-exec-3] s.testing.controllers.GraphQLController : {data={createAuthor={__typename=Author, firstName=null, lastName=null}}}
这是我在 Android Studio 上得到的回复:
06-25 14:48:24.974 16068-16132/com.example.nburk.apollodemo D/graphcool: Executed mutation: CreateAuthor{__typename=Author, firstName=null, lastName=null}
在 Android Studio 中,这是生成器:
private void createPost(String firstname, String lastname) { application.apolloClient().mutate( CreateAuthorMutation.builder() .firstName(firstname) .lastName(lastname) .build()) .enqueue(createPostMutationCallback); }
排队这个:
private ApolloCall.Callback<CreateAuthorMutation.Data> createPostMutationCallback = new ApolloCall.Callback<CreateAuthorMutation.Data>() { @Override public void onResponse(@Nonnull final Response<CreateAuthorMutation.Data> dataResponse) { Log.d(ApolloDemoApplication.TAG, "Executed mutation: " + dataResponse.data().createAuthor().toString()); fetchPosts(); } @Override public void onFailure(@Nonnull ApolloException e) { Log.d(ApolloDemoApplication.TAG, "Error:" + e.toString()); } };
如果你需要它,这是我的突变:
mutation CreateAuthor($firstName: String, $lastName: String){ createAuthor(firstName: $firstName, lastName: $lastName){ firstName lastName } }
本项目基于此:https://github.com/graphcool-examples/android-graphql/tree/master/quickstart-with-apollo
也在上传我的架构。
预先感谢您的帮助。
问题来自 SPQR,您需要为变量添加一个映射器
@PostMapping(value = "/graphql", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public Map<String, Object> indexFromAnnotated(@RequestBody Map<String, Object> request, HttpServletRequest raw) { ExecutionResult executionResult = graphQL.execute(ExecutionInput.newExecutionInput() .query((String) request.get("query")) .operationName((String) request.get("operationName")) // -------- THIS RESOLVES THE VARIABLES-------- .variables((Map<String, Object>) request.get("variables")) .context(raw) .build()); LOGGER.info(executionResult.toSpecification().toString()); return executionResult.toSpecification(); }