File tree Expand file tree Collapse file tree 1 file changed +20
-6
lines changed
instrumentation/graphql-java/graphql-java-common/testing/src/main/java/io/opentelemetry/instrumentation/graphql Expand file tree Collapse file tree 1 file changed +20
-6
lines changed Original file line number Diff line number Diff line change 1414
1515import graphql .ExecutionResult ;
1616import graphql .GraphQL ;
17+ import graphql .GraphqlErrorBuilder ;
18+ import graphql .execution .DataFetcherResult ;
1719import graphql .schema .DataFetcher ;
1820import graphql .schema .GraphQLSchema ;
1921import 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
You can’t perform that action at this time.
0 commit comments