spring-projects / spring-graphql

Spring Integration for GraphQL
https://spring.io/projects/spring-graphql
Apache License 2.0
1.53k stars 305 forks source link

Check for and eliminate duplicate schema resource registrations #230

Closed rstoyanchev closed 2 years ago

rstoyanchev commented 2 years ago

A GraphQlSourceBuilderCustomizer might need to replace the default schema resources, e.g. https://github.com/spring-projects/spring-graphql/pull/218#issuecomment-997296408.

bclozel commented 2 years ago

I've isolated the PathMatchingResourcePatternResolver in a native application to better understand the issue. This is exactly what Josh hinted at: the resolver is resolving resources from the classpath and then trying to consider them as files. Resources in a native image are not files, folders up in the hierarchy are not added to the image. Even if we add them manually, they're not considered as files so the resolver cannot list resources under them.

I don't think the issue is about replacing resources - as this happens only in JVM mode since both scanned schema resources and resources manually set with a customizer are present, leading to duplicates. In native mode, this doesn't happen since the first set of resources are not discovered.

The code snippet listed in https://github.com/spring-projects/spring-graphql/pull/218#issuecomment-997296408 looks fine to me - and this should actually be quite close to what we should do in the future: an AOT processor should discover those resources at build time, register them as resources against the native image, and then generate code that does such manual registration.

In summary, I don't think we need to change the current behavior.

bclozel commented 2 years ago

I've created spring-projects/spring-boot#29291 to follow up on this. Note that Spring GraphQL doesn't offer a Jakarta variant for now, so support for this project is not yet available in the Spring Boot 3.x branch.

rstoyanchev commented 2 years ago

After a team discussion, I'm reopening this issue to also improve the situation for Boot 2.7.

@joshlong pointed out in https://github.com/spring-projects/spring-graphql/pull/218#issuecomment-997296408 that using GraphqlSourcebuilderCustomizer almost works, except that on the JVM you end up with duplicate resources. We can explicitly protect against this by checking for and ignoring duplicate registrations. This should help for this case, but also shouldn't hurt generally since there should be no reason for the same resource to be added twice.