Skip to content

Commit 696f2cd

Browse files
committed
Refine documentSource locations
Closes gh-338
1 parent 9289f0a commit 696f2cd

File tree

11 files changed

+68
-18
lines changed

11 files changed

+68
-18
lines changed

spring-graphql-docs/src/docs/asciidoc/client.adoc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,11 @@ For example:
295295
The document for a request is a `String` that may be defined in a local variable or
296296
constant, or it may be produced through a code generated request object.
297297

298-
Alternatively, you can also keep documents in files with extensions `.graphql` or `
299-
.gql`, on the classpath or anywhere else, and refer to those by file name. For example,
300-
given:
298+
You can also create document files with extensions `.graphql` or `.gql` under
299+
`"graphql-documents/"` on the classpath and refer to them by file name.
300+
301+
For example, given a file called `projectReleases.graphql` in
302+
`src/main/resources/graphql-documents`, with content:
301303

302304
[source,graphql,indent=0,subs="verbatim,quotes"]
303305
.src/main/resources/graphql/project.graphql
@@ -316,7 +318,7 @@ You can then:
316318

317319
[source,java,indent=0,subs="verbatim,quotes"]
318320
----
319-
Mono<Project> projectMono = graphQlClient.documentName("project") <1>
321+
Mono<Project> projectMono = graphQlClient.documentName("projectReleases") <1>
320322
.variable("slug", "spring-framework") <2>
321323
.retrieve()
322324
.toEntity(Project.class);

spring-graphql-docs/src/docs/asciidoc/testing.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,10 @@ project release versions from the response:
281281
The JsonPath is relative to the "data" section of the response.
282282

283283
You can also create document files with extensions `.graphql` or `.gql` under
284-
`"graphql/"` on the classpath and refer to them by file name. For example, given a file
285-
called `projectReleases.graphql` in `src/main/resources/graphql`, with content:
284+
`"graphql-test/"` on the classpath and refer to them by file name.
285+
286+
For example, given a file called `projectReleases.graphql` in
287+
`src/main/resources/graphql-test`, with content:
286288

287289
[source,graphql,indent=0,subs="verbatim,quotes"]
288290
----

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/AbstractGraphQlTesterBuilder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.graphql.test.tester;
1717

1818
import java.time.Duration;
19+
import java.util.Collections;
1920
import java.util.function.Consumer;
2021
import java.util.function.Function;
2122
import java.util.function.Predicate;
@@ -27,6 +28,7 @@
2728
import reactor.core.publisher.Flux;
2829
import reactor.core.publisher.Mono;
2930

31+
import org.springframework.core.io.ClassPathResource;
3032
import org.springframework.graphql.GraphQlRequest;
3133
import org.springframework.graphql.GraphQlResponse;
3234
import org.springframework.graphql.ResponseError;
@@ -64,13 +66,24 @@ public abstract class AbstractGraphQlTesterBuilder<B extends AbstractGraphQlTest
6466
@Nullable
6567
private Predicate<ResponseError> errorFilter;
6668

67-
private DocumentSource documentSource = new CachingDocumentSource(new ResourceDocumentSource());
69+
private DocumentSource documentSource;
6870

6971
private Configuration jsonPathConfig = Configuration.builder().build();
7072

7173
private Duration responseTimeout = DEFAULT_RESPONSE_DURATION;
7274

7375

76+
public AbstractGraphQlTesterBuilder() {
77+
this.documentSource = initDocumentSource();
78+
}
79+
80+
private static DocumentSource initDocumentSource() {
81+
return new ResourceDocumentSource(
82+
Collections.singletonList(new ClassPathResource("graphql-test/")),
83+
ResourceDocumentSource.FILE_EXTENSIONS);
84+
}
85+
86+
7487
@Override
7588
public B errorFilter(Predicate<ResponseError> predicate) {
7689
this.errorFilter = (this.errorFilter != null ? errorFilter.and(predicate) : predicate);

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public interface GraphQlTester {
6464

6565
/**
6666
* Variant of {@link #document(String)} that uses the given key to resolve
67-
* the GraphQL document from a file, or in another way with the help of the
68-
* {@link DocumentSource} that the client is configured with.
67+
* the GraphQL document from a file with the help of the configured
68+
* {@link Builder#documentSource(DocumentSource) DocumentSource}.
6969
* @return spec for response assertions
7070
* @throws IllegalArgumentException if the documentName cannot be resolved
7171
* @throws AssertionError if the response status is not 200 (OK)
@@ -108,7 +108,9 @@ interface Builder<B extends Builder<B>> {
108108
/**
109109
* Configure a {@link DocumentSource} for use with
110110
* {@link #documentName(String)} for resolving a document by name.
111-
* <p>By default, {@link ResourceDocumentSource} is used.
111+
* <p>By default, this is set to {@link ResourceDocumentSource} with
112+
* classpath location {@code "graphql-test/"} and
113+
* {@link ResourceDocumentSource#FILE_EXTENSIONS} as extensions.
112114
*/
113115
B documentSource(DocumentSource contentLoader);
114116

spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterBuilderTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public class GraphQlTesterBuilderTests extends GraphQlTesterTestSupport {
3535
private static final String DOCUMENT = "{ Query }";
3636

3737

38+
@Test
39+
void defaultDocumentSource() {
40+
String document = "{ greeting }";
41+
getGraphQlService().setDataAsJson(document, "{}");
42+
graphQlTester().documentName("greeting").execute();
43+
assertThat(getActualRequestDocument()).isEqualTo(document);
44+
}
45+
3846
@Test
3947
void mutateDocumentSource() {
4048

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ greeting }

spring-graphql/src/main/java/org/springframework/graphql/client/AbstractGraphQlClientBuilder.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.function.Consumer;
2324

2425
import org.springframework.core.codec.Decoder;
2526
import org.springframework.core.codec.Encoder;
27+
import org.springframework.core.io.ClassPathResource;
2628
import org.springframework.graphql.client.GraphQlClientInterceptor.Chain;
2729
import org.springframework.graphql.client.GraphQlClientInterceptor.SubscriptionChain;
2830
import org.springframework.graphql.support.CachingDocumentSource;
@@ -56,7 +58,7 @@ public abstract class AbstractGraphQlClientBuilder<B extends AbstractGraphQlClie
5658

5759
private final List<GraphQlClientInterceptor> interceptors = new ArrayList<>();
5860

59-
private DocumentSource documentSource = new CachingDocumentSource(new ResourceDocumentSource());
61+
private DocumentSource documentSource;
6062

6163
@Nullable
6264
private Encoder<?> jsonEncoder;
@@ -71,6 +73,13 @@ public abstract class AbstractGraphQlClientBuilder<B extends AbstractGraphQlClie
7173
* during, by overriding {@link #build()}.
7274
*/
7375
protected AbstractGraphQlClientBuilder() {
76+
this.documentSource = initDocumentSource();
77+
}
78+
79+
private static DocumentSource initDocumentSource() {
80+
return new CachingDocumentSource(new ResourceDocumentSource(
81+
Collections.singletonList(new ClassPathResource("graphql-documents/")),
82+
ResourceDocumentSource.FILE_EXTENSIONS));
7483
}
7584

7685

spring-graphql/src/main/java/org/springframework/graphql/client/GraphQlClient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public interface GraphQlClient {
5959

6060
/**
6161
* Variant of {@link #document(String)} that uses the given key to resolve
62-
* the GraphQL document from a file, or in another way with the help of the
63-
* {@link DocumentSource} that the client is configured with.
62+
* the GraphQL document from a file with the help of the configured
63+
* {@link Builder#documentSource(DocumentSource) DocumentSource}.
6464
* @throws IllegalArgumentException if the content could not be loaded
6565
*/
6666
RequestSpec documentName(String name);
@@ -109,7 +109,9 @@ interface Builder<B extends Builder<B>> {
109109
/**
110110
* Configure a {@link DocumentSource} for use with
111111
* {@link #documentName(String)} for resolving a document by name.
112-
* <p>By default, {@link ResourceDocumentSource} is used.
112+
* <p>By default, this is set to {@link ResourceDocumentSource} with
113+
* classpath location {@code "graphql-documents/"} and
114+
* {@link ResourceDocumentSource#FILE_EXTENSIONS} as extensions.
113115
*/
114116
B documentSource(DocumentSource contentLoader);
115117

spring-graphql/src/main/java/org/springframework/graphql/support/ResourceDocumentSource.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,22 @@ public ResourceDocumentSource() {
6565
* Constructor with given locations and extensions.
6666
*/
6767
public ResourceDocumentSource(List<Resource> locations, List<String> extensions) {
68-
this.locations = new ArrayList<>(locations);
69-
this.extensions = new ArrayList<>(extensions);
68+
this.locations = Collections.unmodifiableList(new ArrayList<>(locations));
69+
this.extensions = Collections.unmodifiableList(new ArrayList<>(extensions));
7070
}
7171

7272

7373
/**
74-
* Return the configured locations where to check for documents.
74+
* Return a read-only list with the configured locations where to check for
75+
* documents.
7576
*/
7677
public List<Resource> getLocations() {
7778
return this.locations;
7879
}
7980

8081
/**
81-
* Return the file extensions to try when checking for documents by name.
82+
* Return a read-only list with the file extensions to try when checking
83+
* for documents by name.
8284
*/
8385
public List<String> getExtensions() {
8486
return this.extensions;

spring-graphql/src/test/java/org/springframework/graphql/client/GraphQlClientBuilderTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ public class GraphQlClientBuilderTests extends GraphQlClientTestSupport {
3838
private static final String DOCUMENT = "{ Query }";
3939

4040

41+
@Test
42+
void defaultDocumentSource() {
43+
String document = "{ greeting }";
44+
getGraphQlService().setDataAsJson(document, "{}");
45+
graphQlClient().documentName("greeting").execute().block();
46+
assertThat(getGraphQlService().getGraphQlRequest().getDocument()).isEqualTo(document);
47+
}
48+
4149
@Test
4250
void mutateDocumentSource() {
4351

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ greeting }

0 commit comments

Comments
 (0)