Search code examples
graphqlapollo-android

Mutation sending null parameters on Apollo-Android


I'm trying to integrate Apollo-Android with a server mounted locally, the query to show all of the entries is working fine, but when I try to create a new entry, it sends a query without the parameters.

At first I thought it was a server-side problem, but it doesn't seems like it, since it's creating the entry but with null values.

The server it's made on Spring Boot using SPQR for the schema generation, and SpringData-JPA with Hibernate for the connection to a PostGreSQL database.

This is what the server says:

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}}}

And this is the response I get on 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}

In Android Studio this is the builder:

private void createPost(String firstname, String lastname) { application.apolloClient().mutate( CreateAuthorMutation.builder() .firstName(firstname) .lastName(lastname) .build()) .enqueue(createPostMutationCallback); }

That enqueue this:

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()); } };

If you need it, here is my mutation:

mutation CreateAuthor($firstName: String, $lastName: String){ createAuthor(firstName: $firstName, lastName: $lastName){ firstName lastName } }

This project is based on this: https://github.com/graphcool-examples/android-graphql/tree/master/quickstart-with-apollo

Uploading my schema too.

Copia de schema.txt

Thanks beforehand for your help.


Solution

  • The problem was from SPQR, you need to add a mapper for the variables

    @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(); }