Skip to content
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

GraphQlTester should support fragments #923

Closed
thomascaillier opened this issue Mar 13, 2024 · 5 comments
Closed

GraphQlTester should support fragments #923

thomascaillier opened this issue Mar 13, 2024 · 5 comments

Comments

@thomascaillier
Copy link

thomascaillier commented Mar 13, 2024

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 13, 2024
@bclozel
Copy link
Member

bclozel commented Mar 13, 2024

@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...

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Mar 13, 2024
@thomascaillier
Copy link
Author

@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

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 14, 2024
@bclozel
Copy link
Member

bclozel commented Mar 22, 2024

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?

@bclozel bclozel added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Mar 22, 2024
@spring-projects-issues
Copy link
Collaborator

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 spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Mar 29, 2024
@spring-projects-issues
Copy link
Collaborator

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.

@spring-projects-issues spring-projects-issues added status: waiting-for-triage An issue we've not yet triaged and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Apr 5, 2024
@rstoyanchev rstoyanchev closed this as not planned Won't fix, can't repro, duplicate, stale Apr 10, 2024
@rstoyanchev rstoyanchev removed the status: waiting-for-triage An issue we've not yet triaged label Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants