Manav Bhanot
Manav Bhanot

Reputation: 113

GraphQL schema generation

I am generating graphQL schema using java annotations. Thanks to graphql-spqr library. I am calling the code to

GraphQLSchemaGenerator schemaGenerator = new GraphQLSchemaGenerator()
            .withResolverBuilders(new AnnotatedResolverBuilder())
            .withValueMapperFactory(new JacksonValueMapperFactory())
            .withOperationsFromSingleton(<annotatedClassObject>);
GraphQLSchema schema = schemaGenerator.generate();

The above code is executed for every call the graphQL API. I know that the GraphQL Java library has the below line documented.

/** Building this object is very cheap and can be done on each 
  * execution * if necessary.  Building the schema is often not
  * as cheap, especially if its parsed from graphql IDL schema 
  * format via {@link graphql.schema.idl.SchemaParser}.
 */

Is there any way I can collect metrics on how long each call to generate this schema takes ?

Upvotes: 0

Views: 996

Answers (1)

kaqqao
kaqqao

Reputation: 15459

I'm the author of graphql-spqr. What I can tell you is that you definitely should not be calling this for every request. What graphql-java documentation refers to is the GraphQL object, which is indeed cheap to construct, but the schema is not. SPQR scans your entire class hierarchy when generating the schema, and that is rather expensive in the general case.

What is your use-case exactly? Why would you be constructing a new schema instance on each call? Whatever you're trying to achieve - there's almost certainly a better way.

As for measuring the time, you can use JMH (Java Microbenchmark Harness).

Upvotes: 1

Related Questions