Skip to content

Commit b7a2df6

Browse files
committed
Problem: no way to intercept GraphQL operations
For example, if one wants to log GraphQL operations and/or failures associated with them, there isn't really a good way to do this. Solution: introduce GraphQLOperationListener interface
1 parent e7bde7f commit b7a2df6

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
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+
* http://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+
*/
15+
package graphql.servlet;
16+
17+
import graphql.GraphQLError;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
public interface GraphQLOperationListener {
23+
void onGraphQLOperation(GraphQLContext context, String operationName, String query, Map<String, Object> variables,
24+
Object data);
25+
void onFailedGraphQLOperation(GraphQLContext context, String operationName, String query,
26+
Map<String, Object> variables, List<GraphQLError> errors);
27+
}

src/main/java/graphql/servlet/GraphQLServlet.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ protected GraphQLContext createContext(Optional<HttpServletRequest> req, Optiona
160160
return contextBuilder.build(req, resp);
161161
}
162162

163+
private List<GraphQLOperationListener> operationListeners = new ArrayList<>();
164+
165+
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policyOption = ReferencePolicyOption.GREEDY)
166+
public void bindOperationListener(GraphQLOperationListener listener) {
167+
operationListeners.add(listener);
168+
}
169+
170+
public void unbindOperationListener(GraphQLOperationListener listener) {
171+
operationListeners.remove(listener);
172+
}
173+
163174
@Override @SneakyThrows
164175
public String executeQuery(String query) {
165176
try {
@@ -274,6 +285,8 @@ public Void run() {
274285
Map<String, Object> dict = new HashMap<>();
275286
dict.put("data", result.getData());
276287
resp.getWriter().write(new ObjectMapper().writeValueAsString(dict));
288+
operationListeners.forEach(l -> l.onGraphQLOperation(context, operationName, query, variables,
289+
result.getData()));
277290
} else {
278291
result.getErrors().stream().
279292
filter(error -> (error instanceof ExceptionWhileDataFetching)).
@@ -285,6 +298,8 @@ public Void run() {
285298
dict.put("errors", errors);
286299

287300
resp.getWriter().write(new ObjectMapper().writeValueAsString(dict));
301+
operationListeners.forEach(l -> l.onFailedGraphQLOperation(context, operationName, query, variables,
302+
result.getErrors()));
288303
}
289304
}
290305
}

0 commit comments

Comments
 (0)