I have a requirement to transform the source JSON to a target JSON structure; for this, I am experimenting with GraphQL.
Below is my Java Code
String schema = "type Query{AuthToken: String}";
SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
DataFetcher<LinkedHashMap<Object, Object>> dataFetcher = environment -> {
String json = "{\"AuthToken\":\"eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJjdXJyZW50QmF0Y2hOdW1iZXIiOiIxODk3MTczIiwidGVybWluYWxJZCI6InhucFByb2R1Y3QtT25saW5lIiwidXNlck5hbWUiOiJ4bnBVc2VyMSIsInBhc3N3b3JkIjoiQ24zdDNXaDlUZjlHYXBkT2RCamdNQTgrM1NZPSIsImVuYyI6InRydWUiLCJhdXRoVHlwZSI6IkJBU0lDIiwidG9rZW5UeXBlIjoieG5wX2F1dGhfdG9rZW4iLCJuYmYiOjE2NjI2NTIzMDMsImV4cCI6MTY2MjY1MjkwMywiaWF0IjoxNjYyNjUyMzAzLCJpc3MiOiJodHRwczovL3F3aWtjaWx2ZXIuY29tLyJ9.tiYe88waCpiWKAmYCkFyxxBCmuyFgLas_XsobFWxDrqfI9jzbrqQ1ZNiPdsovULd6xk1_0553aVOPJap15GE6Q\",\"ResponseMessage\":\"Transactionsuccessful.\"}";
TypeReference<LinkedHashMap<Object, Object>> typeReference = new TypeReference<>() {
};
return mapper.readValue(json, typeReference);
};
RuntimeWiring runtimeWiring = newRuntimeWiring()
.type("Query", builder -> builder.defaultDataFetcher(dataFetcher))
.build();
SchemaGenerator schemaGenerator = new SchemaGenerator();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
ExecutionResult executionResult = build.execute("{token: AuthToken}");
log.info("GraphQL response {}", executionResult.getData().toString());
However, when this code is executed, GraphQL returns the complete JSON and not just the property AuthToken.
Also, the expected response is of structure
{token: 'the token from the input json' }
however, below response is received
{token={AuthToken=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJjdXJyZW50QmF0Y2hOdW1iZXIiOiIxODk3MTczIiwidGVybWluYWxJZCI6InhucFByb2R1Y3QtT25saW5lIiwidXNlck5hbWUiOiJ4bnBVc2VyMSIsInBhc3N3b3JkIjoiQ24zdDNXaDlUZjlHYXBkT2RCamdNQTgrM1NZPSIsImVuYyI6InRydWUiLCJhdXRoVHlwZSI6IkJBU0lDIiwidG9rZW5UeXBlIjoieG5wX2F1dGhfdG9rZW4iLCJuYmYiOjE2NjI2NTIzMDMsImV4cCI6MTY2MjY1MjkwMywiaWF0IjoxNjYyNjUyMzAzLCJpc3MiOiJodHRwczovL3F3aWtjaWx2ZXIuY29tLyJ9.tiYe88waCpiWKAmYCkFyxxBCmuyFgLas_XsobFWxDrqfI9jzbrqQ1ZNiPdsovULd6xk1_0553aVOPJap15GE6Q, ResponseMessage=Transactionsuccessful.}}