americanexpress / nodes

A GraphQL JVM Client - Java, Kotlin, Scala, etc.
Apache License 2.0
307 stars 70 forks source link

how to map a list of fields via this library #68

Open bbakerman opened 5 years ago

bbakerman commented 5 years ago

I was testing out this library via the Spotify test API : https://spotify-graphql-server.herokuapp.com/graphql

The query would be

query ($byName: String) {
  queryArtists(byName: $byName) {
    image
    albums {
      image
      name
      id
      tracks {
        preview_url
        name
        id
      }
    }
    name
    id
  }
}

with variables byName : "Red Hot Chili Peppers"

Debugging shows it sending the right query and data back but it fails to Deserliase into a structure

Exception in thread "main" GraphQLException{message='OK', status='200', description='Unrecognized field "queryArtists" (class com.atlassian.model.Query), not marked as ignorable (0 known properties: ])
 at [Source: (String)"{"queryArtists":[{"image":"https://i.scdn.co/image/5b2072e522bf3324019a8c2dc3db20116dff0b87","albums":[{"image":"https://i.scdn.co/image/8deed63f89e0a215e90de1ee5809780921a47747","name":"The Getaway","id":"43otFXrY0bgaq5fB3GrZj6"},{"image":"https://i.scdn.co/image/66ab66e38e40a9202e8417a0b59ad86a210637a7","name":"I'm With You","id":"5wZtSIvijWCMc1vlPFqAyB"},{"image":"https://i.scdn.co/image/fd62b1f4f697284a024784706949bff3a6e1a27e","name":"Stadium Arcadium","id":"7xl50xr9NDkd3i2kBbzsNZ"},{"image"[truncated 3654 chars]; line: 1, column: 18] (through reference chain: io.aexp.nodes.graphql.Wrapper["data"]->com.atlassian.model.Query["queryArtists"])', errors=null}
    at io.aexp.nodes.graphql.Fetch.send(Fetch.java:96)
    at io.aexp.nodes.graphql.GraphQLTemplate.execute(GraphQLTemplate.java:99)
    at io.aexp.nodes.graphql.GraphQLTemplate.query(GraphQLTemplate.java:58)
    at com.atlassian.Main.main(Main.java:23)

My code is this

public static void main(String[] args) throws MalformedURLException {
        GraphQLTemplate graphQLTemplate = new GraphQLTemplate();

        GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
                .url("https://spotify-graphql-server.herokuapp.com/graphql")
                .variables(new Variable("byName", "Red Hot Chili Peppers"))
                .scalars(BigDecimal.class)
                .request(Query.class)
                .build();
        GraphQLResponseEntity<Query> responseEntity = graphQLTemplate.query(requestEntity, Query.class);
        System.out.println(responseEntity);
    }

My model classes are

public class Query {

    @GraphQLVariable(name = "byName", scalar = "String")
    List<Artist> queryArtists;
}

public class Artist {
    String name;
    String id;
    String image;
    List<Album> albums;
}

public class Album {
    String name;
    String id;
    String image;
    //List<Track> tracks;
}

public class Track {
    String name;
    //List<Artist> artists;
    String preview_url;
    String id;
}

I guess I am unsure about where the entry point for queries are. The samples in the library only every enter a single value field (eg User via github /user) and not a list field like above

Am I off track on how this gets set up?

bbakerman commented 5 years ago

ps. I am one of the maintainers of graphql-java so my server side graphql knowlege is ok but I am trying to work out this as a client side consumer library

bbakerman commented 5 years ago

Ahh worked it out just after I typed all this

The model classes MUST be public. So

public class Query {

    @GraphQLVariable(name = "byName", scalar = "String")
    public List<Artist> queryArtists;
}