Skip to content

Modify fragment location for GraphQl Tests #1092

@emaarco

Description

@emaarco

📋 Summary

I suggest modifying how file-based GraphQL fragments are handled in tests by allowing fragments to be sourced from a dedicated graphql-fragments folder rather than the current graphql-test folder.

🛑 Problem Statement

Currently, Spring GraphQL requires file-based fragments for tests to be placed in a graphql-test folder, which limits their use as a central resource.

In our platform, we have multiple backend services, each with GraphQL APIs that often return similar types like Task or User. Clients frequently request these entire objects, which leads to duplicated code across many queries and mutations. For example, every time we need a User object, we manually list all attributes, which is cumbersome and error-prone.

GraphQL fragments solve this problem by letting us define shared structures once, reducing repetition. However, since fragments aren't part of the GraphQL Schema Definition Language (SDL) and can't be accessed via introspection, clients must also define them separately, leading to duplication between backend and frontend code.

To address this, we want to create a central fragment registry in a backend service that all clients can use (f.ex. by synching them). This ensures consistency and keeps the backend as the single source of truth.

The current restriction to graphql-test is misleading, as it implies the fragments are test-only, rather than globally reusable.

🔍 Example

Consider the following example of a GraphQL fragment that defines a fragment for a User:

# In file user.fragments.graphqls
fragment UserDetails on UserDto {
    id
    name
    email
    phoneNumber
}

Below is a test that makes use of this UserDetails fragment. The user.fragment file referenced must be placed in a graphql-test folder:

graphQlTester.documentName("user")
        .fragmentName("user.fragment")
        .variable("userId", user.getId().toString())
        .execute()
        .path("user")
        .entity(UserDto.class)
        .isEqualTo(
            new UserDto(
                user.getId().toString(),
                user.getName(),
                user.getEmail(),
                user.getPhoneNumber()
            )
        );

💡 Proposed Solution

Allow file-based fragments to be placed in a graphql-fragments folder instead of graphql-test. This will better support the idea of a central fragment registry that can be used across both backend services and clients.

🚀 Benefits

  • Central Fragment Repository: Enables a central repository where fragments are consistently shared across all services and clients.
  • Improved Developer Experience: Separates fragments from test-specific content, reducing confusion.

🤝 Contribution

I am willing to contribute to this feature and have already designed a first implementation to check if this approach is possible. If you are considering the implementation, I am happy to further refine it based on your feedback.

💬 Feedback Request

I would greatly appreciate your feedback on this suggestion. Do you see any challenges or alternative approaches that could better support this use case? Please let me know your thoughts, and I'd be happy to discuss this further.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions