GraphQLのJava - 計装

計装インターセプター

計装インタフェースを実装することで、クエリの実行を観察する、または実行時の動作を変更することができます。

最も一般的な用途は、パフォーマンスの監視、およびカスタムロギングのためであるが、それはまた、他のタスクに使用することができます。

あなたはGraphQLオブジェクトを作成するときは、達成関連する計装をバインドすることができます。

        GraphQL.newGraphQL(schema)
                .instrumentation(new TracingInstrumentation())
                .build();

カスタムインストルメンテーションインターセプター

計装実装クラスは、「開始」の冒頭でメソッドを実装する必要があります。クエリの実行中にこの方法は、各ステップの開始前に呼び出されました。

各コールバックメソッドは、オブジェクトInstrumentationContext null以外を返す必要があり、オブジェクトは実行がこのステップで完了すると、バックと呼ばれ、呼び出しが成功したかエラー(あなたがThrowableオブジェクトを取得することができます)かどうかを教えてくれます。

次の例では、計装のカスタムインターセプタを与えられました。実行の全体的な実行時間を測定するために使用することができ、その結果、状態内のオブジェクトに格納されています。

    class CustomInstrumentationState implements InstrumentationState {
        private Map<String, Object> anyStateYouLike = new HashMap<>();

        void recordTiming(String key, long time) {
            anyStateYouLike.put(key, time);
        }
    }

    class CustomInstrumentation extends SimpleInstrumentation {
        @Override
        public InstrumentationState createState() {
            //
            // instrumentation state is passed during each invocation of an Instrumentation method
            // and allows you to put stateful data away and reference it during the query execution
            //
            return new CustomInstrumentationState();
        }

        @Override
        public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
            long startNanos = System.nanoTime();
            return new SimpleInstrumentationContext<ExecutionResult>() {
                @Override
                public void onCompleted(ExecutionResult result, Throwable t) {
                    CustomInstrumentationState state = parameters.getInstrumentationState();
                    state.recordTiming(parameters.getQuery(), System.nanoTime() - startNanos);
                }
            };
        }

        @Override
        public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters) {
            //
            // this allows you to intercept the data fetcher used to fetch a field and provide another one, perhaps
            // that enforces certain behaviours or has certain side effects on the data
            //
            return dataFetcher;
        }

        @Override
        public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
            //
            // this allows you to instrument the execution result some how.  For example the Tracing support uses this to put
            // the `extensions` map of data in place
            //
            return CompletableFuture.completedFuture(executionResult);
        }
    }

計装インターセプタチェーン

ChainedInstrumentationクラスは、オブジェクト計装重合を複数使用してもよいです。それらはコールで定義されているために、オブジェクトパラメータ、および順次のリストを受信計装クラスChainedInstrumentation。

        List<Instrumentation> chainedList = new ArrayList<>();
        chainedList.add(new FooInstrumentation());
        chainedList.add(new BarInstrumentation());
        ChainedInstrumentation chainedInstrumentation = new ChainedInstrumentation(chainedList);

        GraphQL.newGraphQL(schema)
                .instrumentation(chainedInstrumentation)
                .build();

フィールド検証計装

クエリを実行する前に、FieldValidationInstrumentationインターセプタは、フィールドとフィールドのパラメータをチェックしてください。チェックが失敗した場合、実行はエラー情報を問い合わせ、それらの結果に加算され、終了します。

各フィールドのバリデーションルールを高めるために、カスタム実装FieldValidationまたは直接SimpleFieldValidationクラスすることができます。

        ExecutionPath fieldPath = ExecutionPath.parse("/user");
        FieldValidation fieldValidation = new SimpleFieldValidation()
                .addRule(fieldPath, new BiFunction<FieldAndArguments, FieldValidationEnvironment, Optional<GraphQLError>>() {
                    @Override
                    public Optional<GraphQLError> apply(FieldAndArguments fieldAndArguments, FieldValidationEnvironment environment) {
                        String nameArg = fieldAndArguments.getFieldArgument("name");
                        if (nameArg.length() > 255) {
                            return Optional.of(environment.mkError("Invalid user name", fieldAndArguments));
                        }
                        return Optional.empty();
                    }
                });

        FieldValidationInstrumentation instrumentation = new FieldValidationInstrumentation(
                fieldValidation
        );

        GraphQL.newGraphQL(schema)
                .instrumentation(instrumentation)
                .build();

おすすめ

転載: www.cnblogs.com/pku-liuqiang/p/11529115.html