Add fragments support in GraphQlTester
This commit adds support for GraphQL fragments with `GraphQlTester`.
Fragments allow to avoid repetition in GraphQL requests by reusing
field selection sets.
For example, the "releases" fragment can be reused in multiple queries
and make the overall document shorter:
```
query frameworkReleases {
project(slug: "spring-framework") {
name
...releases
}
}
query graphqlReleases {
project(slug: "spring-graphql") {
name
...releases
}
}
fragment releases on Project {
releases {
version
}
}
```
With this change, `GraphQlTester` accepts fragments as `String` or can
load them by their name using the configured `DocumentSource`, similarly
to the document support.
Closes gh-964
This commit is contained in:
@@ -476,6 +476,55 @@ You can then:
|
||||
<1> Load the document from "projectReleases.graphql"
|
||||
<2> Provide variable values.
|
||||
|
||||
|
||||
This approach also works for loading fragments for your queries.
|
||||
Fragments are reusable field selection sets that avoid repetition in a request document.
|
||||
For example, we can use a `...releases` fragment in multiple queries:
|
||||
|
||||
[source,graphql,indent=0,subs="verbatim,quotes"]
|
||||
.src/main/resources/graphql-documents/projectReleases.graphql
|
||||
----
|
||||
query frameworkReleases {
|
||||
project(slug: "spring-framework") {
|
||||
name
|
||||
...releases
|
||||
}
|
||||
}
|
||||
query graphqlReleases {
|
||||
project(slug: "spring-graphql") {
|
||||
name
|
||||
...releases
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
This fragment can be defined in a separate file for reuse:
|
||||
|
||||
[source,graphql,indent=0,subs="verbatim,quotes"]
|
||||
.src/main/resources/graphql-documents/releases.graphql
|
||||
----
|
||||
fragment releases on Project {
|
||||
releases {
|
||||
version
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
You can then send this fragment along the query document:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
Project project = graphQlClient.documentName("projectReleases") <1>
|
||||
.fragmentName("releases") <2>
|
||||
.retrieveSync()
|
||||
.toEntity(Project.class);
|
||||
----
|
||||
<1> Load the document from "projectReleases.graphql"
|
||||
<2> Load the fragment from "releases.graphql" and append it to the document
|
||||
|
||||
|
||||
|
||||
The "JS GraphQL" plugin for IntelliJ supports GraphQL query files with code completion.
|
||||
|
||||
You can use the `GraphQlClient` xref:client.adoc#client.graphqlclient.builder[Builder] to customize the
|
||||
|
||||
Reference in New Issue
Block a user