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

Support fragments in GraphQlTester #964

Closed
wickstopher opened this issue May 9, 2024 · 4 comments
Closed

Support fragments in GraphQlTester #964

wickstopher opened this issue May 9, 2024 · 4 comments
Assignees
Labels
in: test Issues related to the test module type: enhancement A general enhancement
Milestone

Comments

@wickstopher
Copy link

wickstopher commented May 9, 2024

I am in the process of converting a codebase from the old GraphQL Java Kickstart library over to spring-graphql.

The test template in the testing framework for that library included a parameter that allowed you to optionally pass in fragment resource paths.

It would be helpful in the maintenance of test requests to support the loading of GraphQL fragments from disk. Currently, as far as I can tell fragments are only supported in the document method, which requires you to juxtapose your fragment with your query in a single string.

Being able to write something along the following lines:

graphQlTester
  .documentName("graphQLDocument")
  .withFragmentNames("fragmentOne", "fragmentTwo")
  .execute()

would be much nicer than having to repeat boilerplate in your test requests.

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

bclozel commented May 10, 2024

Duplicates #923
Please comment on that other issue to provide more feedback and ideas. Thanks!

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@bclozel bclozel added status: duplicate A duplicate of another issue in: test Issues related to the test module and removed status: waiting-for-triage An issue we've not yet triaged labels May 10, 2024
@rstoyanchev
Copy link
Contributor

The #import syntax suggested in #923 is a much broader feature as it tends to be used in schema files too. We don't necessarily need to go there to support reusable fragments, which is a much more specific goal that makes sense to support.

As mentioned in #923 (comment), we could also expose fragmentName and fragment methods on GraphQlTester, and is also what is suggested here, so let's use this issue to track the request.

It makes sense to have equivalent methods on GraphQlClient too.

@rstoyanchev rstoyanchev reopened this May 10, 2024
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: duplicate A duplicate of another issue labels May 10, 2024
@rstoyanchev rstoyanchev added this to the 1.x Backlog milestone May 10, 2024
@rstoyanchev rstoyanchev changed the title GraphQlTester proposal: support reading GraphQL fragments from disk Support fragments in GraphQlTester May 10, 2024
@rstoyanchev
Copy link
Contributor

In the meantime, since "documentName" handling can be customized, you could support a name like "myDocument::myFragment1::myFragment2". I haven't tested this, but a custom DocumentSource to support that could look like this:

public class ConcatenatingDocumentSource implements DocumentSource {

	private final DocumentSource delegate = new ResourceDocumentSource(
			List.of(new ClassPathResource("graphql-test/")), List.of(".graphql"));

	@Override
	public Mono<String> getDocument(String name) {
		return name.contains("::") ?
				Flux.fromArray(name.split("::")).flatMap(this.delegate::getDocument).reduce((s, s2) -> s + s2) :
				this.delegate.getDocument(name);
	}

}

You plug that in via GraphQlTester.Builder#documentSource . The delegate is what is used by default. In the custom implementation we are simply using it to load multiple documents and join them.

@wickstopher
Copy link
Author

Thanks, this workaround is exactly what I needed to move forward without having to abandon fragment utilization in my test cases. Much appreciated!

@rstoyanchev rstoyanchev modified the milestones: 1.x Backlog, 1.3 Backlog May 23, 2024
@bclozel bclozel self-assigned this Jun 3, 2024
@bclozel bclozel closed this as completed in a9a8d0b Jun 3, 2024
@bclozel bclozel modified the milestones: 1.3 Backlog, 1.3.1 Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues related to the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants