Instrumentation拦截器
通过实现Instrumentation接口,可以观察一个query的执行,或修改运行期的行为。
最常见的用途是进行性能监控,和自定义日志记录,但它也可以用于完成其他任务。
创建GraphQL对象时,可以绑定相关的Instrumentation实现。
1 GraphQL.newGraphQL(schema) 2 .instrumentation(new TracingInstrumentation()) 3 .build();
自定义Instrumentation拦截器
Instrumentation实现类需要实现"begin"开头的方法。这个方法会在查询执行的过程中,在每个步骤开始之前被调用。
每个回调方法都必须返回一个非空的InstrumentationContext对象,这个对象在本执行步骤完成时会被回调,并告知是否调用成功或是出错(可以获取Throwable对象)。
下面的示例中,给出了一个自定义的Instrumentation拦截器。可以用来测量执行过程的整体执行时间,并将结果存入一个有状态的对象当中。
1 class CustomInstrumentationState implements InstrumentationState { 2 private Map<String, Object> anyStateYouLike = new HashMap<>(); 3 4 void recordTiming(String key, long time) { 5 anyStateYouLike.put(key, time); 6 } 7 } 8 9 class CustomInstrumentation extends SimpleInstrumentation { 10 @Override 11 public InstrumentationState createState() { 12 // 13 // instrumentation state is passed during each invocation of an Instrumentation method 14 // and allows you to put stateful data away and reference it during the query execution 15 // 16 return new CustomInstrumentationState(); 17 } 18 19 @Override 20 public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) { 21 long startNanos = System.nanoTime(); 22 return new SimpleInstrumentationContext<ExecutionResult>() { 23 @Override 24 public void onCompleted(ExecutionResult result, Throwable t) { 25 CustomInstrumentationState state = parameters.getInstrumentationState(); 26 state.recordTiming(parameters.getQuery(), System.nanoTime() - startNanos); 27 } 28 }; 29 } 30 31 @Override 32 public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters) { 33 // 34 // this allows you to intercept the data fetcher used to fetch a field and provide another one, perhaps 35 // that enforces certain behaviours or has certain side effects on the data 36 // 37 return dataFetcher; 38 } 39 40 @Override 41 public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) { 42 // 43 // this allows you to instrument the execution result some how. For example the Tracing support uses this to put 44 // the `extensions` map of data in place 45 // 46 return CompletableFuture.completedFuture(executionResult); 47 } 48 }
链式Instrumentation拦截器
可以使用ChainedInstrumentation类,将多个Instrumentation对象进行聚合。ChainedInstrumentation类接收一个Instrumentation对象列表参数,然后按照它们定义的顺序依次调用。
1 List<Instrumentation> chainedList = new ArrayList<>(); 2 chainedList.add(new FooInstrumentation()); 3 chainedList.add(new BarInstrumentation()); 4 ChainedInstrumentation chainedInstrumentation = new ChainedInstrumentation(chainedList); 5 6 GraphQL.newGraphQL(schema) 7 .instrumentation(chainedInstrumentation) 8 .build();
字段验证Instrumentation
FieldValidationInstrumentation拦截器,可以在执行查询前,校验字段和字段参数。如果校验失败,那么执行过程将终止,error信息添加到查询的result当中。
可以自定义FieldValidation的实现,或直接使用SimpleFieldValidation类来为每个字段增加校验规则。
1 ExecutionPath fieldPath = ExecutionPath.parse("/user"); 2 FieldValidation fieldValidation = new SimpleFieldValidation() 3 .addRule(fieldPath, new BiFunction<FieldAndArguments, FieldValidationEnvironment, Optional<GraphQLError>>() { 4 @Override 5 public Optional<GraphQLError> apply(FieldAndArguments fieldAndArguments, FieldValidationEnvironment environment) { 6 String nameArg = fieldAndArguments.getFieldArgument("name"); 7 if (nameArg.length() > 255) { 8 return Optional.of(environment.mkError("Invalid user name", fieldAndArguments)); 9 } 10 return Optional.empty(); 11 } 12 }); 13 14 FieldValidationInstrumentation instrumentation = new FieldValidationInstrumentation( 15 fieldValidation 16 ); 17 18 GraphQL.newGraphQL(schema) 19 .instrumentation(instrumentation) 20 .build();