spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.96k stars 40.65k forks source link

Configure specific GraphQL schema files #42792

Open bjoernmayer opened 2 weeks ago

bjoernmayer commented 2 weeks ago

Heyho,

it seems like there is currently no way to specify schema files by their filename. Instead it is only possible to configure directories.

From GraphqlProperties.java:

private String[] appendSlashIfNecessary(String[] locations) {
            return Arrays.stream(locations)
                .map((location) -> location.endsWith("/") ? location : location + "/")
                .toArray(String[]::new);
        }

Hence this is impossible:

spring.graphql.schema:
    locations:
        # Subgraph's schema
        - classpath:schema/schema.graphqls
        # Federation-specific directives (shared across subgraphs)
        - classpath:schema/federation.graphqls
        # Common types (shared across subgraphs)
        - classpath:schema/Accommodation.graphqls
        - classpath:schema/AccommodationDeal.graphqls

I encountered this obstacle when trying to migrate from DGS to Spring GQL

bclozel commented 1 week ago

Hello @bjoernmayer

Yes that's correct, those are meant to be locations, not fully resolved resources. I'm not sure I understand the use case though. Why point directly to resources and not locations? Are there schema resources in the same location that are not meant to be used? Why?

bjoernmayer commented 1 week ago

Hi @bclozel, thank you for looking into this.

The main reason is Federation.

We are working with subgraphs in a Mono Repository. We have all of our shared types in a shared library. Each subgraph then picks only the types, that it needs and should expose in its schema.

bclozel commented 3 days ago

Spring for GraphQL provides a org.springframework.graphql.execution.GraphQlSource.SchemaResourceBuilder#schemaResources that lets you configure specific schema files.

You can already declare specific resources in your application by contributing a GraphQlSourceBuilderCustomizer bean to your application and add resources manually.

I'll move this issue to Spring Boot to consider a separate configuration property that would let you set additional schema resources on top of scanned ones.