Search code examples
javapaginationgraphqlrelaygraphql-java

Relay based Pagination in Java for Java-GraphQL server


I have implemented a java based GraphQL server using the GraphQL-Java-tools. Now I need to implement the Relay based pagination with the Java-GraphQL server that I have.

I couldn't find anything helpful out there. Can anyone please help me in pointing out to the right place to find how to implement Relay based Pagination in Java_GraphQL server?

Thanks in anticipation.


Solution

  • graphql-java-tools added support for Relay in release 5.4.0. As explained in the documentation example you can use the new @connection directive in the schema:

    type Query {
        users(first: Int, after: String): UserConnection @connection(for: "User")
    }
    
    type User {
        id: ID!
        name: String
    }
    

    and return a Connection<T> in the resolver:

    class QueryResolver implements GraphQLQueryResolver {
    
      public Connection<User> users(int first, String after, DataFetchingEnvironment env) {
        return new SimpleListConnection<>(Collections.singletonList(new User(1L, "Luke"))).get(env);
      }
    }
    

    Still, examples beyond a simple list (eg. when edges have to be fetched from a database) are scarce, and SimpleListConnection is the only implementation provided by graphql-java so far.