question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

spring-graphql compatible code generation

See original GitHub issue

Now that spring-graphql is 1.0 and reasonably stable, it would be good to provide first class support for their structures.

Currently @QueryMapping, @MutationMapping and @SubscriptionMapping can be shoe-horned in via customAnnotationsMapping with something like:

  "customAnnotationsMapping": {
    "^Query\\.\\w+$": ["org.springframework.graphql.data.method.annotation.QueryMapping"],
    "^Mutation\\.\\w+$": ["org.springframework.graphql.data.method.annotation.MutationMapping"],
    "^Subscription\\.\\w+$": ["org.springframework.graphql.data.method.annotation.SubscriptionMapping"]
  }

But that falls short on parameterised resolvers, or resolvers for extensions which are generated separately which rely on spring’s @SchemaMapping(typeName = "MyGraphQLType") (and so need to be manually added)

Likewise use of @Argument for all input parameters can be manually added to the implementing classes, but would be convenient to have on the generated interfaces.

There are other parameter types that can be provided (see https://docs.spring.io/spring-graphql/docs/current/reference/html/#controllers). Adding these with similar configuration as exists today for DataFetchingEnvironment would also be useful and would provide reasonably complete with what spring-graphql offers.

Also note that there is a feature request in spring-graphql for code generation: https://github.com/spring-projects/spring-graphql/issues/159 which would effectively be solved with this functionality.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
kobylynskyicommented, Jul 31, 2022

Added support of 2 configs: resolverArgumentAnnotations and parametrizedResolverAnnotations which should solve abovementioned problems:

resolverArgumentAnnotations

  "resolverArgumentAnnotations": ["org.springframework.graphql.data.method.annotation.Argument"]

This will add the specified annotation to every argument in every method in each Query/Mutation/Subscription resolver:

public interface QueryResolver {
    String version(graphql.schema.DataFetchingEnvironment env) throws Exception;

    java.util.List<Event> eventsByCategoryAndStatus(@javax.validation.constraints.NotNull @org.springframework.graphql.data.method.annotation.Argument String categoryId, @org.springframework.graphql.data.method.annotation.Argument EventStatus status, graphql.schema.DataFetchingEnvironment env) throws Exception;

    Event eventById(@javax.validation.constraints.NotNull @org.springframework.graphql.data.method.annotation.Argument String id, graphql.schema.DataFetchingEnvironment env) throws Exception;
}

parametrizedResolverAnnotations

  "parametrizedResolverAnnotations": ["org.springframework.graphql.data.method.annotation.SchemaMapping(typeName="{{TYPE_NAME}}")"]

This will add the specified annotation to every field resolver method:

public interface EventPropertyResolver {
    @org.springframework.graphql.data.method.annotation.SchemaMapping(typeName="EventProperty")
    int intVal(EventPropertyTO eventProperty) throws Exception;

    @org.springframework.graphql.data.method.annotation.SchemaMapping(typeName="EventProperty")
    java.util.List<EventPropertyTO> child(EventPropertyTO eventProperty, Integer first, Integer last) throws Exception;
}

Let me know what you think.

2reactions
JamesPeters98commented, Jul 5, 2022

The custom annotations mapping you provided works well for me too.

The main issue for me is not being able to annotate the arguments with @Argument.

Maybe a customResolverArgumentAnnotations option would be good? It could just take a list of annotations to be applied to all arguments. But not the DataFetchingEnvironment etc

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring for GraphQL Documentation
Spring for GraphQL provides support for Spring applications built on GraphQL Java. It is a joint collaboration between the GraphQL Java team ...
Read more >
Code Generation - DGS Framework - Netflix Open Source
The DGS Code Generation plugin generates code during your project's build process based on your Domain Graph Service's GraphQL schema file.
Read more >
graphql-java version not compatible with graphql-spring-boot ...
Would it be possible to upgrade the version of graphql-java in graphql-java-codegen to 16.2? code-generation project: <dependency> <groupId>io.
Read more >
GraphQL Code Libraries, Tools and Services
Currently supports React, React Native, Preact, Svelte, and Vue, and is supported by GraphQL Code Generator. Logical yet simple default behaviour and ...
Read more >
Spring GraphQL - YouTube
GraphQL is a relatively new but well-established alternative to REST for exposing web APIs that is rapidly becoming a popular choice for web ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found