question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapping and accessing "partial" responses

See original GitHub issue

I’m having trouble wrapping my head around the mapping between whatever GraphQLResult returns after a successful query and the generated classes.

I’m executing a mutation and fetch only a single field “access_token” from the response by setting the appropriate projection. The autogenerated response object “LoginUserResponse” has another field “user”, which I’m not interested in.

public void login() throws IOException, GraphQLException {
    LoginUserInput input = new LoginUserInput();
    input.setUsername(this.username);
    input.setPassword(this.password);

    LoginUserMutationRequest query = new LoginUserMutationRequest();
    query.setUser_data(input);

    // Only fetch field access_token, ignore all other fields
    LoginUserResponseResponseProjection projection = new LoginUserResponseResponseProjection().access_token();

    GraphQLResult<Map<String, LoginUserResponse>> result = this.queryEntity(query, projection);
    System.out.println(result.getData().get(query.getOperationName()).getAccess_token());
}

public <Q extends GraphQLOperationRequest, P extends GraphQLResponseProjection, R> GraphQLResult<Map<String, R>> queryEntity(Q query, P projection)  throws GraphQLException {
    GraphQLRequest request = new GraphQLRequest(query, projection);
    GraphQLResult<Map<String, R>> result = restTemplate.exchange(URI.create(this.endpoint + "/***/api/graphql"),
        HttpMethod.POST,
        new HttpEntity<String>(request.toHttpJsonBody(), createHeaders(request)),
        new ParameterizedTypeReference<GraphQLResult<Map<String, R>>>() {})
        .getBody();
    if (result.hasErrors()) {
        throw new GraphQLException(result.getErrors());
    }
    return result;
}

This leads to this error:

java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.***.***.model.LoginUserResponse (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.***.***.model.LoginUserResponse is in unnamed module of loader 'app')

Which makes sense, as the class LoginResponse would expect the field ‘user’ to be not null, but I’m not fetching this field, so constructing an instance of LoginResponse is impossible.

Could anyone give me a hint on how to properly access the fields I fetched? I can see that there are Mapper definitions in the examples, but I’m a little confused regarding the differences between ProductTO and Product, as I can not find two different classes for an entity in my autogenerated code.

Any help very much appreciated!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
philipp-schmidtcommented, May 29, 2020

Looks good to me. Having the operation name being resolved automatically is a nice plus! Will this work with Lists?

1reaction
kobylynskyicommented, May 29, 2020

@philipp-schmidt I’ve already started working on this. Here’s the additional class that will be generated:

import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLResult;

import java.util.Map;

public class LoginUserResponseGraphQLResult extends GraphQLResult<Map<String, LoginUserResponse>> {

    public LoginUserResponse loginUser() {
        Map<String, LoginUserResponse> data = getData();
        return data != null ? data.get(LoginUserMutationRequest.OPERATION_NAME) : null;
    }
}

So that in the client code it will look a bit simplified:

    LoginUserResponseGraphQLResult result = queryEntity(query, projection, LoginUserResponseGraphQLResult.class);
    result.loginUser().getAccess_token();

Please let me know what you think

Read more comments on GitHub >

github_iconTop Results From Across the Web

Incomplete Responses/Partially Filled Responses
Yes, We are using response mapping workflow, I have given the same condition which you mentioned but the responses are not syncing in ......
Read more >
Partial Answers: Journal of Literature and the History of Ideas
Partial Answers is an international, peer-reviewed, interdisciplinary journal that focuses on the study of literature and the history of ...
Read more >
Download partial survey responses? - Typeform Community
Typeform only collect responses when the respondents submit the form, so we can't download (or even view) partial responses.
Read more >
Python: map() with partial arguments - Stack Overflow
from functools import partial _mapfunc = partial(_dist, X=cur.x(), ... Edit (in response to @chepner's comment) - if you want to iterate ...
Read more >
What are partial survey responses? - SurveyLab
Partial responses allow you to access unfinished surveys. Each time the respondent press "Next page" button the survey is saved in our database....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found