Netflix DGS client with code generation - how to properly desserialize json response?

686 Views Asked by At

I am generating code to consume a GraphQL Service with graphqlcodegen.

Maven plugin setup looks like this:

        <plugin>
            <groupId>io.github.deweyjose</groupId>
            <artifactId>graphqlcodegen-maven-plugin</artifactId>
            <version>${graphqlcodegen.version}</version>
            <configuration>
                <packageName>br.com.funcionalcorp.generated</packageName>
                <schemaPaths>
                    <path>src/main/resources/schema</path>
                </schemaPaths>
                <generateBoxedTypes>true</generateBoxedTypes>
                <generateClient>true</generateClient>
                <writeToFiles>true</writeToFiles>
                <addGeneratedAnnotation>true</addGeneratedAnnotation>
                <outputDir>${project.build.directory}/generated-sources/generated</outputDir>
                <typeMapping>
                    <Datetime>java.time.LocalDateTime</Datetime>
                    <Prescription_Upload>com.netflix.graphql.dgs.scalars.UploadScalar</Prescription_Upload>
                    <JSON>graphql.scalars.object.JsonScalar</JSON>
                    <URL>graphql.scalars.url.UrlScalar</URL>
                </typeMapping>

            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

It generates beatifully. But the reponse types do no match the actual response.

This is the response:

{
  "data": {
    "Pharma_assessEligibility": {
      "eligibilityAssessment": {
        "belongsToIndustryProgram": false,
        "productDescription": null,
        "programCode": "0"
      },
      "registrationPolicy": {
        "beneficiaryFields": []
      }
    }
  }
}

But the class generated looks like this:

public class Pharma_AssessEligibilityResponse {}

It does not match the case or name and fails to desserialize from json. It's done like this:

public <T> T query(final String apiKey, final GraphQLQuery query, final BaseProjectionNode projection, final Class<T> type) {
    GraphQLQueryRequest req = new GraphQLQueryRequest(query, projection);
    GraphQLResponse response = getClient(apiKey).executeQuery(req.serialize());

    return response.dataAsObject(type);
}

Is there a way to annotate it or configure it?

0

There are 0 best solutions below