Skip to content

Commit 980715a

Browse files
committed
improve test
1 parent b6f9d16 commit 980715a

File tree

1 file changed

+20
-6
lines changed
  • instrumentation/graphql-java/graphql-java-common/testing/src/main/java/io/opentelemetry/instrumentation/graphql

1 file changed

+20
-6
lines changed

instrumentation/graphql-java/graphql-java-common/testing/src/main/java/io/opentelemetry/instrumentation/graphql/AbstractGraphqlTest.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import graphql.ExecutionResult;
1616
import graphql.GraphQL;
17+
import graphql.GraphqlErrorBuilder;
18+
import graphql.execution.DataFetcherResult;
1719
import graphql.schema.DataFetcher;
1820
import graphql.schema.GraphQLSchema;
1921
import graphql.schema.idl.RuntimeWiring;
@@ -114,20 +116,32 @@ private RuntimeWiring buildWiring() {
114116
.build();
115117
}
116118

117-
private DataFetcher<Map<String, String>> getBookByIdDataFetcher() {
119+
private DataFetcher<DataFetcherResult<Map<String, String>>> getBookByIdDataFetcher() {
118120
return dataFetchingEnvironment ->
119121
getTesting()
120122
.runWithSpan(
121123
"fetchBookById",
122124
() -> {
123125
String bookId = dataFetchingEnvironment.getArgument("id");
124-
if ("book-failure".equals(bookId)) {
126+
DataFetcherResult.Builder<Map<String, String>> builder =
127+
DataFetcherResult.newResult();
128+
if ("book-exception".equals(bookId)) {
125129
throw new IllegalStateException("fetching book failed");
130+
} else if ("book-graphql-error".equals(bookId)) {
131+
return builder
132+
.error(
133+
GraphqlErrorBuilder.newError(dataFetchingEnvironment)
134+
.message("failed to fetch book")
135+
.build())
136+
.build();
126137
}
127-
return books.stream()
128-
.filter(book -> book.get("id").equals(bookId))
129-
.findFirst()
130-
.orElse(null);
138+
return builder
139+
.data(
140+
books.stream()
141+
.filter(book -> book.get("id").equals(bookId))
142+
.findFirst()
143+
.orElse(null))
144+
.build();
131145
});
132146
}
133147

0 commit comments

Comments
 (0)