americanexpress / nodes

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

Unrecognized field error when attempting to capture References (`refs`) from GitHub GraphQL API v4 #28

Closed chirangaalwis closed 6 years ago

chirangaalwis commented 6 years ago

Requirement: I am attempting to capture the branches of a GitHub repository using the GitHub GraphQL API v4. The following is an example GraphQL query for capturing the branches of repository wso2/docker-ei.

{
  organization(login: "wso2") {
    name
    url
    repository(name: "docker-ei") {
      refs(first: 10, refPrefix: "refs/heads/") {
        edges {
          node {
            name
          }
        }
      }
    }
  }
}

Current Status: For this purpose, we need to capture the Git References (refs). I have managed to build up the Java implementation for building the following query, to capture the Git References (en route to creating the final query).

{
  organization(login: "wso2") {
    name
    repository(name: "docker-ei") {
      issue(number: 32) {
        title
      }
      refs(first: 6, refPrefix: "refs/heads/") {
        totalCount
      }
      name
    }
    url
  }
}

I have created three relevant Java classes (DTOs) Organization.java, Repository.java and Refs.java in order to build up the above query. The content of these classes are as follows:

Organization.java

@GraphQLProperty(name = "organization",
        arguments = {@GraphQLArgument(name = "login", type = "String")}
)
public class Organization {
    private String name;
    private String url;
    @GraphQLArguments({
            @GraphQLArgument(name = "name", type = "String")
    })
    private Repository repository;

    public String getName() {
        return name;
    }

    public String getUrl() {
        return url;
    }

    public Repository getRepository() {
        return repository;
    }
}

Repository.java

@GraphQLProperty(name = "repository")
public class Repository {
    private String name;

    @GraphQLArguments({
            @GraphQLArgument(name = "number", type = "Int")
    })
    private Issue issue;

    @GraphQLArguments({
            @GraphQLArgument(name = "first", type = "Int"),
            @GraphQLArgument(name = "refPrefix", type = "String")
    })
    private Refs refs;

    public String getName() {
        return name;
    }

    public Issue getIssue() {
        return issue;
    }

    public Refs getReferences() {
        return refs;
    }
}

Refs.java

@GraphQLProperty(name = "refs")
public class Refs {
    private int totalCount;

    public int getTotalCount() {
        return totalCount;
    }
}

The following code snippet depicts how I am executing the query:

        GraphQLRequestEntity requestEntity = null;

        try {
            requestEntity = GraphQLRequestEntity.Builder()
                    .headers(headers)
                    .url("https://api.github.com/graphql")
                    .request(Organization.class)
                    .arguments(
                            new Arguments("organization", new Argument("login", "wso2")),
                            new Arguments("organization.repository", new Argument("name", "docker-ei")),
                            new Arguments("organization.repository.issue", new Argument("number", 32)),
                            new Arguments("organization.repository.refs", new Argument("first", 6), new Argument("refPrefix", "refs/heads/"))
                    )
                    .build();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        GraphQLTemplate graphQLTemplate = new GraphQLTemplate();

        GraphQLResponseEntity<Organization> responseEntity = graphQLTemplate.query(requestEntity, Organization.class);

Issue: But I am experiencing the following exception when executing the query (although refs is a valid field under repository):

Exception in thread "main" GraphQLException{message='OK', status='200', description='Unrecognized field "refs" (class org.wso2.carbon.wum.git.Repository), not marked as ignorable (2 known properties: "issue", "name"])
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: io.aexp.nodes.graphql.Wrapper["data"]->org.wso2.carbon.wum.git.Organization["repository"]->org.wso2.carbon.wum.git.Repository["refs"])', errors=null}
    at io.aexp.nodes.graphql.Fetch.send(Fetch.java:84)
    at io.aexp.nodes.graphql.GraphQLTemplate.execute(GraphQLTemplate.java:83)
    at io.aexp.nodes.graphql.GraphQLTemplate.query(GraphQLTemplate.java:42)
    at org.wso2.carbon.wum.git.GraphQLRequestSender.main(GraphQLRequestSender.java:44)

During debugging it was identified that the GraphQL query is accurately created and when executed using the GitHub GraphQL v4 explorer, the desired results can be obtained.

Any help or suggestions in relation with fixing this issue, are highly appreciated.

chemdrew commented 6 years ago

The issue you are facing is related to the serialization. I haven't had the time to check thoroughly but I think the issue is getReferences does not match the naming convention for that field. Right now we are using jackson so you can either update the method names to match or use the jackson annotations to make it work as it stands