Skip to content

Add Spring GraphQL support #29140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
12 tasks done
bclozel opened this issue Dec 20, 2021 · 0 comments
Closed
12 tasks done

Add Spring GraphQL support #29140

bclozel opened this issue Dec 20, 2021 · 0 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@bclozel
Copy link
Member

bclozel commented Dec 20, 2021

Now that the Spring GraphQL project has made significant project in its Milestone phase, we should move the experimental auto-configuration and starter in this project.

We should address the following tasks:

  • Add dependency management for Spring GraphQL and GraphQL Java
  • Auto-configure Spring GraphQL base infrastructure
  • Add a FailureAnalyzer for InvalidSchemaLocationsException
  • Auto-configure HTTP transport for MVC and WebFlux
  • Auto-configure schema printer and graphiql endpoints
  • Auto-configure CORS options for GraphQL web endpoints
  • Auto-configure GraphQL WebSocket endpoint
  • Auto-configure Query* support for GraphQL
  • Auto-configure security for Spring GraphQL
  • Add testing support with GraphQlTester and @GraphQlTest
  • Add Metrics support
  • Create Spring Boot starter

See spring-projects/spring-graphql#207

@bclozel bclozel added the type: enhancement A general enhancement label Dec 20, 2021
@bclozel bclozel added this to the 2.7.0-M1 milestone Dec 20, 2021
@bclozel bclozel self-assigned this Dec 20, 2021
bclozel added a commit that referenced this issue Dec 21, 2021
This commit adds dependency management in Spring Boot for Spring GraphQL
and GraphQL Java.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit adds the auto-configuration for setting up the base Spring
GraphQL infrastructure. Because GraphQL doesn't depend on any particular
transport, we must have a separate configuration for creating:

* the `GraphQlSource`, which holds the schema and the `GraphQL` instance
* the `GraphQlService` for executing incoming requests
* the `BatchLoaderRegistry` for batch loading support
* the `AnnotatedControllerConfigurer` for supporting the annotated
  controllers programming model

This comes with a starting point for the `"spring.graphql.*"`
configuration properties; we can now configure the locations and file
extensions of GraphQL schema files we should load and configure at
startup.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit adds a new `FailureAnalyzer` for exceptions thrown when no
GraphQL schema file could be found.
This lists configured locations (with `"classpath:..."` notation) and
also resolved locations - this helps developers figure out how the
pattern are resolved and why schema files cannot be found.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit adds two new auto-configuration classes for Spring GraphQL
support. Once the base GraphQL infrastructure is in place, we can now
expose the `GraphQlService` over an HTTP transport.

Spring GraphQL supports both MVC and WebFlux, so this commit ships with
one auto-configuration for each.
Developers can configure the HTTP path where the GraphQL resource is
exposed using the `spring.graphql.path` configuration property (this
defaults to `"/graphql"`).

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit configuresa new endpoint for printing in text format the
resolved GraphQL schema.
This endpoint is exposed by default under "/graphql/schema" and must be
enabled with "spring.graphql.schema.printer=true".

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
Spring GraphQL ships with a static version of the graphiql IDE for
exploring and querying GraphQL endpoints.
See https://github.com/graphql/graphiql for more information.

This commit auto-configures the GraphiQL handler for both MVC and
WebFlux and points GraphiQL to the GraphQL HTTP endpoint exposed by the
application. This feature is disabled by default and can be switched on
with "spring.graphql.graphiql.enabled=true".

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit adds `"spring.graphql.cors.*"` configuration properties to
customize the CORS configuration for GraphQL web endpoints.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit auto-configures a GraphQL WebSocket endpoint for both Spring
MVC and Spring WebFlux. This is only enabled if the required libraries
are on the classpath and if the `"spring.graphql.websocket.path"`
property is defined.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
GraphiQL is useful when working on a GraphQL API - it allows
developers to craft queries and test newly developed feature. This
is not enabled by default, as this should not be exposed in production
without the developer's knowledge.

This commit flips this value when Spring Boot devtools is active for a
better developer experience.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit auto-configures Spring Data Querydsl and QueryByExample
support for Spring GraphQL.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit configures security features for Spring GraphQL.
In the case of both MVC and WebFlux, this contributes
`DataFetcherExceptionResolver` instances to resolve security exceptions
and expose them as proper errors in the GraphQL response.

For MVC only, this also configures a
`SecurityContextThreadLocalAccessor`. This component ensures that the
security context is propagated between `ThreadLocal` and the Reactor
asynchronous execution.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit adds the `@AutoConfigureGraphQl` test annotation. It can be
used to import the relevant auto-configurations when testing a GraphQL
application.
Currently, it will get the main `GraphQlAutoConfiguration`, but also
what's required for configuring codecs and validation support.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit adds the required infrastructure to auto-configure a
`GraphQlTester` or `WebGraphQlTester` in Spring Boot tests.
Specific annotations like `AutoConfigureGraphQlTester` and
`AutoConfigureWebGraphQlTester` will contribute pre-configured beans for
testing a GraphQL with the tester.

This also ships a `ContextCustomize` for contributing a `GraphQlTester`
in the case of a full `@SpringBootTest` integration test against a live
server.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit adds the `@GraphQlTest` annotation which brings a new type
of sliced test for GraphQL applications. This considers all the required
infrastructure brought by `@AutoConfigureGraphQl`, but also brings
application components like `@Controller` beans and
`RuntimeWiringConfigurer`.

With this type of test, we'll only initialize a minimal setup for
testing a set of Controllers, without involving any transport-related
component.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit adds the required infrastructure for instrumenting the
GraphQL engine and datafetchers in order to collect metrics.

With this infrastructure, we can collect metrics such as:

* "graphql.request", a timer for GraphQL query
* "graphql.datafetcher", a timer for GraphQL datafetcher calls
* "graphql.request.datafetch.count", a distribution summary of
  datafetcher count per query
* "graphql.error", an error counter

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This commit auto-configures the metrics infrastructure for Spring
GraphQL.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
This new starter brings the following dependencies:

* the base starter
* the JSON starter
* the Spring GraphQL dependency

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
bclozel added a commit that referenced this issue Dec 21, 2021
This commit documents all the features added in the previous commits:
from the main infrastructure support, to testing and metrics.

See gh-29140
bclozel added a commit that referenced this issue Dec 21, 2021
@bclozel bclozel closed this as completed Dec 21, 2021
izeye added a commit to izeye/spring-boot that referenced this issue Dec 31, 2021
bclozel pushed a commit that referenced this issue Jan 3, 2022
bclozel added a commit that referenced this issue Oct 19, 2022
This commit migrates the existing metrics support (added in #29140) to
the new `Observation` instrumentation contributed in
spring-projects/spring-graphql#501.

We cannot have a smoother migration path here as the instrumentation
does not use the same context information for metadata extraction.

Closes gh-32794
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant