AlikElzin-kilaka
AlikElzin-kilaka

Reputation: 36041

In spring-graphql, how to auto-generate schema from annotations (e.g. @QueryMapping) without defining `.graphql` files?

I have a project, written in Kotlin/Java that uses spring-graphql annotation like @QueryMapping, @MutationMapping, etc.

The whole schema is defined in code using the above annotations.

But, in all the tutorials I've found, a .graphql file is needed, which is a huge duplication and a burden - especially if the schema is a bit complicated.

If I don't specify the .graphql file, the playground/graphiql doesn't load.

How to auto-generate the schema from code to be used by graphiql/playground without writing .graphql files? Thanks

Example tutorials:

  1. techdozo
  2. eh3rrera

Upvotes: 2

Views: 4365

Answers (2)

Fradantim
Fradantim

Reputation: 43

A little late, but this repo has an example on schema auto-generation based on @QueryMapping annotated methods and @Argument annotated parameters.

This is done with reflection on application startup. @MutationMapping is not present in the example, but could be added by reading carefully GraphQLConfig.java

Upvotes: 1

Nils Hartmann
Nils Hartmann

Reputation: 556

Spring for GraphQL has a "schema-first" approach, that means you have to declare the schema yourself (common approach is to use SDL in .graphls files). Spring for GraphQL cannot generate the schema from annotations.

If you want to use "code-first" approach, where you write code with annotations and then let the tool generate the schema, you could try MicroProfile GraphQL, but I'm not sure, if that works in Spring.

Upvotes: 5

Related Questions