1
votes

I'm trying to use GraphQL like client of a REST API. My backend return JSON but in my application client i write graphQL and in client i translate graphQL queries to HTTP call's.

My schema:

type Query {
   students: [Student]
}
type Student {
   name: String
}

POJO example:

public class Student {
    private Integer id;
    private String name;
}

My resolver:

public class Query implements GraphQLQueryResolver {
  public List<Post> students() {
    // HTTP Request
   }
}

In all library's implementations i need create a POJO for Student and write a resolver for request in my API.

A way exist to don't need create a POJO and a create global execute resolver?

1

1 Answers

0
votes

If you're using libraries like graphql-java-tools (which seems to be the case), you need POJOs, as this is where the library gets its type mappings from. But if you're just using graphql-java itself, you can wire it any way you like - including having a single global resolver (i.e. DataFetcher).

For an idea how to do this, see http://graphql-java.readthedocs.io/en/latest/schema.html#idl

You want something like:

SchemaParser schemaParser = new SchemaParser();
SchemaGenerator schemaGenerator = new SchemaGenerator();

File schemaFile = loadSchema("yourStudentSchema.graphqls");

TypeDefinitionRegistry typeRegistry = schemaParser.parse(schemaFile);
RuntimeWiring wiring = buildRuntimeWiring();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, wiring);

RuntimeWiring is where you hook the resolvers, e.g:

RuntimeWiring buildRuntimeWiring() {
    return RuntimeWiring.newRuntimeWiring()
            // this uses builder function lambda syntax
            .type("Query", typeWiring -> typeWiring
                    .dataFetcher("students", env -> fetchDataSomeHow(env)))             
            .build();
}

So you can provide the same DataFetcher implementation to each dataFetcher call f that's what you're after. graphql-java itself makes no assumptions about way it's wired and implemented, e.g. it does not enforce POJOs or anything else.