spring-projects / spring-graphql

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

GraphQlTester should support fragments #923

Closed thomascaillier closed 3 months ago

thomascaillier commented 4 months ago

Problem

I have several mutations and queries that returns the same type, so I use a fragment to define and reuse it. \ I want to test those mutations and queries using a GraphQlTester (more precisely a HttpGraphQlTester).

Let's assume I have mutation A and mutation B that are both using a fragment in fragment.graphql :

src
└─ main
   └─ resources
      └─ graphql
         └─ definitions.graphqls
└─ test
   └─ resources
      └─ graphql-test
         └─ mutationA.graphql
         └─ mutationB.graphql
         └─ fragment.graphql

definitions.graphqls :

type Foo {
    id: String
    name: String
}

type Mutation {
    mutationA: Foo
    mutationB: Foo
}

mutationA.graphql :

mutation mutationA {
    mutationA {
        ...FooFragment
    }
}

mutationB.graphql :

mutation mutationB {
    mutationB {
        ...FooFragment
    }
}

fragments.graphql :

fragment FooFragment on Foo {
    id
    name
}

When I run a test like this :

@SpringBootTest
@AutoConfigureGraphQl
@AutoConfigureHttpGraphQlTester
class MyTest {

    @Autowired
    private HttpGraphQlTester tester;

    @Test
    void test() {
        tester.documentName("mutationA")
                .execute()
                .path("mutationA.id").entity(String.class).isEqualTo("1234");
    }
}

Solutions

The solutions I see are either support an import syntax (ex: #import "fragments.sql") or taking the whole graphql-test folder as context

bclozel commented 4 months ago

@thomascaillier Interesting. Can you edit your issue to describe an example of the actual content of those .gql files?

Do you have an example of this feature being supported by other tools and libraries? Creating our own templating engine for that asks the question of IDE support...

thomascaillier commented 4 months ago

@bclozel issue updated 👍

I know IntelliJ doesn't need any import, it seems it analyze the whole folder.

The #import syntax is supported by npm @graphql-tools : https://the-guild.dev/graphql/tools/docs/schema-loading#using-import-expression

bclozel commented 4 months ago

Thanks for the update @thomascaillier

We looked into this and came to the conclusion that graphql-tools/graphql-utilities are the only solutions out there for this, and they're not just about concatenating pieces of schema, but really a complete namespace and import system that parses the schema elements and allows to rename types on-the-fly (a bit like TypeScript imports). So this not only applies to testing, but also to schemas in general.

We don't think we can achieve simply this level of support as this is very much tied to the TypeScript/JavaScript ecosystem.

What we could do instead is the following: offer tester.fragmentName("fragment") and tester.fragment("...") that lets you point to fragments that will be prepended to the actual document. This would not parse the schema nor support any namespacing feature. This would also require each test to point to fragments, which would make maintenance a bit harder.

If we're looking into a more complete and general solution for this, I think this should be discussed at the level of the graphql-java project - we're using its SchemaParser#parse and TypeDefinitionRegistry#merge methods to parse several files and merge the definitions. I don't think they support any type of import syntax nor any extension point like @graphql-tools does, but this might be a good discussion to have. Can you maybe start a discussion there to get the team's opinion on this?

spring-projects-issues commented 3 months ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues commented 3 months ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.