Skip to content

fix: docs for graphQlTester & constructor for documentSource #1285

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions spring-graphql-docs/modules/ROOT/pages/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ xref:testing.adoc#testing.requests[execute requests] using the same API, indepen
=== Builder

`GraphQlTester` defines a parent `Builder` with common configuration options for the
builders of all extensions. It lets you configure the following:
builders of all extensions. These include builders like the one of the ExecutionGraphQlServiceTester, listed above. It lets you configure at least the following:

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



include-code::GraphQlTesterBuilder[tag=inlineDocument,indent=0]

[[testing.requests]]
== Requests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2020-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.graphql.docs.testing.graphqltester.builder;

import java.time.Duration;
import java.util.function.Predicate;

import org.springframework.core.io.ClassPathResource;
import org.springframework.graphql.ResponseError;
import org.springframework.graphql.client.GraphQlTransport;
import org.springframework.graphql.support.DocumentSource;
import org.springframework.graphql.support.ResourceDocumentSource;
import org.springframework.graphql.test.tester.GraphQlTester;

public class GraphQlTesterBuilder {

void inlineDocument() {
// tag::inlineDocument[]
GraphQlTransport transport = /**/ null;
Predicate<ResponseError> errorFilter = /**/ null;
ClassPathResource resource = new ClassPathResource("custom-folder/");
DocumentSource documentSource = new ResourceDocumentSource(resource);

GraphQlTester.builder(transport)
.documentSource(documentSource)
.errorFilter(errorFilter)
.responseTimeout(Duration.ofSeconds(5))
.build();
// end::inlineDocument[]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
import org.springframework.util.FileCopyUtils;

/**
* {@link DocumentSource} that looks for a document {@link Resource} under a set
* of locations and trying a number of different file extension.
* {@link DocumentSource} that searches for document {@link Resource}s across
* multiple locations while trying different file extensions.
*
* @author Rossen Stoyanchev
* @author Marco Schäck
* @since 1.0.0
*/
public class ResourceDocumentSource implements DocumentSource {
Expand All @@ -61,6 +62,14 @@ public ResourceDocumentSource() {
this(Collections.singletonList(new ClassPathResource("graphql/")), FILE_EXTENSIONS);
}

/**
* Constructor with a single location and the default file extensions.
* @param location the resource location
*/
public ResourceDocumentSource(Resource location) {
this(Collections.singletonList(location), FILE_EXTENSIONS);
}

/**
* Constructor with given locations and extensions.
* @param locations the resource locations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.time.Duration;
import java.util.Collections;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import reactor.test.StepVerifier;

Expand All @@ -30,33 +30,75 @@
/**
* Unit tests for {@link ResourceDocumentSource}.
* @author Rossen Stoyanchev
* @author Marco Schäck
*/
class ResourceDocumentSourceTests {

private ResourceDocumentSource source;
@Nested
class DefaultConstructorTests {

@Test
void getDocument() {
ResourceDocumentSource source = new ResourceDocumentSource();
String content = source.getDocument("test-document").block(Duration.ofSeconds(5));
assertThat(content).startsWith("query GetUser($id: ID!)");
}

@BeforeEach
void setUp() {
this.source = new ResourceDocumentSource(
Collections.singletonList(new ClassPathResource("books/")),
ResourceDocumentSource.FILE_EXTENSIONS);
@Test
void getDocumentNotFound() {
ResourceDocumentSource source = new ResourceDocumentSource();
StepVerifier.create(source.getDocument("invalid"))
.expectErrorMessage(
"Failed to find document, name='invalid', " +
"under location(s)=[class path resource [graphql/]]")
.verify(Duration.ofSeconds(5));
}
}

@Nested
class SingleResourceConstructorTests {

@Test
void getDocument() {
String content = this.source.getDocument("book-document").block(Duration.ofSeconds(5));
assertThat(content).startsWith("bookById(id:\"1\"");
@Test
void getDocument() {
ResourceDocumentSource source = new ResourceDocumentSource(new ClassPathResource("books/"));
String content = source.getDocument("book-document").block(Duration.ofSeconds(5));
assertThat(content).startsWith("bookById(id:\"1\"");
}

@Test
void getDocumentNotFound() {
ResourceDocumentSource source = new ResourceDocumentSource(new ClassPathResource("books/"));
StepVerifier.create(source.getDocument("invalid"))
.expectErrorMessage(
"Failed to find document, name='invalid', " +
"under location(s)=[class path resource [books/]]")
.verify(Duration.ofSeconds(5));
}
}

@Test
void getDocumentNotFound() {
StepVerifier.create(this.source.getDocument("invalid"))
.expectErrorMessage(
"Failed to find document, name='invalid', " +
"under location(s)=[class path resource [books/]]")
.verify(Duration.ofSeconds(5));
@Nested
class ListResourceConstructorTests {

@Test
void getDocument() {
ResourceDocumentSource source = new ResourceDocumentSource(
Collections.singletonList(new ClassPathResource("books/")),
ResourceDocumentSource.FILE_EXTENSIONS);
String content = source.getDocument("book-document").block(Duration.ofSeconds(5));
assertThat(content).startsWith("bookById(id:\"1\"");
}

@Test
void getDocumentNotFound() {
ResourceDocumentSource source = new ResourceDocumentSource(
Collections.singletonList(new ClassPathResource("books/")),
ResourceDocumentSource.FILE_EXTENSIONS);
StepVerifier.create(source.getDocument("invalid"))
.expectErrorMessage(
"Failed to find document, name='invalid', " +
"under location(s)=[class path resource [books/]]")
.verify(Duration.ofSeconds(5));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query GetUser($id: ID!) {
user(id: $id) {
id
name
email
}
}