GraphQL Java

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();
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

GraphQL Java - HelloWorld