Skip to content

Commit 12fcd6c

Browse files
emaarcobclozel
authored andcommitted
Document graphQlTester custom document source
Signed-off-by: Marco Schaeck <[email protected]> [[email protected]: polishing docs and tests] Signed-off-by: Brian Clozel <[email protected]> Closes gh-1285
1 parent 8c39294 commit 12fcd6c

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

spring-graphql-docs/modules/ROOT/pages/testing.adoc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ xref:testing.adoc#testing.requests[execute requests] using the same API, indepen
181181
[[testing.graphqltester.builder]]
182182
=== Builder
183183

184-
`GraphQlTester` defines a parent `Builder` with common configuration options for the
185-
builders of all extensions. It lets you configure the following:
184+
`GraphQlTester` defines a parent `Builder` with common configuration options for the builders of all supported transports.
185+
It lets you configure the following:
186186

187187
- `errorFilter` - a predicate to suppress expected errors, so you can inspect the data
188188
of the response.
@@ -191,8 +191,7 @@ the classpath or from anywhere else.
191191
- `responseTimeout` - how long to wait for request execution to complete before timing
192192
out.
193193

194-
195-
194+
include-code::GraphQlTesterBuilder[tag=inlineDocument,indent=0]
196195

197196
[[testing.requests]]
198197
== Requests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2020-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.docs.testing.graphqltester.builder;
18+
19+
import java.time.Duration;
20+
import java.util.function.Predicate;
21+
22+
import org.springframework.core.io.ClassPathResource;
23+
import org.springframework.graphql.ResponseError;
24+
import org.springframework.graphql.client.GraphQlTransport;
25+
import org.springframework.graphql.support.DocumentSource;
26+
import org.springframework.graphql.support.ResourceDocumentSource;
27+
import org.springframework.graphql.test.tester.GraphQlTester;
28+
29+
public class GraphQlTesterBuilder {
30+
31+
void inlineDocument() {
32+
// tag::inlineDocument[]
33+
GraphQlTransport transport = /**/ null;
34+
Predicate<ResponseError> errorFilter = /**/ null;
35+
ClassPathResource resource = new ClassPathResource("custom-folder/");
36+
DocumentSource documentSource = new ResourceDocumentSource(resource);
37+
38+
GraphQlTester tester = GraphQlTester.builder(transport)
39+
.documentSource(documentSource)
40+
.errorFilter(errorFilter)
41+
.responseTimeout(Duration.ofSeconds(5))
42+
.build();
43+
// end::inlineDocument[]
44+
}
45+
46+
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
import org.springframework.util.FileCopyUtils;
3535

3636
/**
37-
* {@link DocumentSource} that looks for a document {@link Resource} under a set
38-
* of locations and trying a number of different file extension.
37+
* {@link DocumentSource} that searches for document {@link Resource}s across
38+
* multiple locations while trying different file extensions.
3939
*
4040
* @author Rossen Stoyanchev
41+
* @author Marco Schäck
4142
* @since 1.0.0
4243
*/
4344
public class ResourceDocumentSource implements DocumentSource {
@@ -61,6 +62,15 @@ public ResourceDocumentSource() {
6162
this(Collections.singletonList(new ClassPathResource("graphql/")), FILE_EXTENSIONS);
6263
}
6364

65+
/**
66+
* Constructor with a single location and the default file extensions.
67+
* @param location the resource location
68+
* @since 2.0.0
69+
*/
70+
public ResourceDocumentSource(Resource location) {
71+
this(Collections.singletonList(location), FILE_EXTENSIONS);
72+
}
73+
6474
/**
6575
* Constructor with given locations and extensions.
6676
* @param locations the resource locations

0 commit comments

Comments
 (0)