我正在尝试将Apollo-Android与本地安装的服务器集成在一起,以显示所有条目的查询工作正常,但是当我尝试创建新条目时,它将发送不带参数的查询。
起初,我认为这是服务器端的问题,但似乎并非如此,因为它创建的是条目,但具有空值。
它是在Spring Boot上使用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
也上传我的架构。
在此先感谢您的帮助。
答案 0 :(得分:0)
问题出在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(); }