private PreparsedDocumentEntry parseAndValidate(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { log.debug("Parsing query: '{}'...", executionInput.getQuery()); ParseResult parseResult = parse(executionInput, graphQLSchema, instrumentationState); if (parseResult.isFailure()) { log.warn("Query failed to parse : '{}'", executionInput.getQuery()); return new PreparsedDocumentEntry(parseResult.getException().toInvalidSyntaxError()); } else { final Document document = parseResult.getDocument(); log.debug("Validating query: '{}'", executionInput.getQuery()); final List<ValidationError> errors = validate(executionInput, document, graphQLSchema, instrumentationState); if (!errors.isEmpty()) { log.warn("Query failed to validate : '{}'", executionInput.getQuery()); return new PreparsedDocumentEntry(errors); } return new PreparsedDocumentEntry(document); } }
private CompletableFuture<ExecutionResult> execute(ExecutionInput executionInput, Document document, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { String query = executionInput.getQuery(); String operationName = executionInput.getOperationName(); Object context = executionInput.getContext(); Execution execution = new Execution(); ExecutionId executionId = idProvider.provide(query, operationName, context); log.debug("Executing '{}'. operation name: '{}'. query: '{}'. variables '{}'", executionId, executionInput.getOperationName(), executionInput.getQuery(), executionInput.getVariables()); CompletableFuture<ExecutionResult> future = execution.execute(executionStrategy, document, graphQLSchema, executionId, executionInput, instrumentationState); future = future.whenComplete((result, throwable) -> { if (throwable != null) { log.error(String.format("Execution '%s' threw exception when executing : query : '%s'. variables '%s'", executionId, executionInput.getQuery(), executionInput.getVariables()), throwable); } else { int errorCount = result.getErrors().size(); if (errorCount > 0) { log.debug("Execution '{}' completed with '{}' errors", executionId, errorCount); } else { log.debug("Execution '{}' completed with zero errors", executionId); } } }); return future; }
private CompletableFuture<ExecutionResult> execute(ExecutionInput executionInput, Document document, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { String query = executionInput.getQuery(); String operationName = executionInput.getOperationName(); Object context = executionInput.getContext(); Execution execution = new Execution(queryStrategy, mutationStrategy, subscriptionStrategy, instrumentation); ExecutionId executionId = idProvider.provide(query, operationName, context); log.debug("Executing '{}'. operation name: '{}'. query: '{}'. variables '{}'", executionId, executionInput.getOperationName(), executionInput.getQuery(), executionInput.getVariables()); CompletableFuture<ExecutionResult> future = execution.execute(document, graphQLSchema, executionId, executionInput, instrumentationState); future = future.whenComplete((result, throwable) -> { if (throwable != null) { log.error(String.format("Execution '%s' threw exception when executing : query : '%s'. variables '%s'", executionId, executionInput.getQuery(), executionInput.getVariables()), throwable); } else { int errorCount = result.getErrors().size(); if (errorCount > 0) { log.debug("Execution '{}' completed with '{}' errors", executionId, errorCount); } else { log.debug("Execution '{}' completed with zero errors", executionId); } } }); return future; }
public InstrumentationExecutionParameters(ExecutionInput executionInput, GraphQLSchema schema, InstrumentationState instrumentationState) { this.executionInput = executionInput; this.query = executionInput.getQuery(); this.operation = executionInput.getOperationName(); this.context = executionInput.getContext(); this.variables = executionInput.getVariables() != null ? executionInput.getVariables() : Collections.emptyMap(); this.instrumentationState = instrumentationState; this.schema = schema; }
public InstrumentationExecutionParameters(ExecutionInput executionInput, GraphQLSchema schema, InstrumentationState instrumentationState) { this.executionInput = executionInput; this.query = executionInput.getQuery(); this.operation = executionInput.getOperationName(); this.context = executionInput.getContext(); this.variables = executionInput.getVariables() != null ? executionInput.getVariables() : Collections.emptyMap(); this.instrumentationState = instrumentationState; this.schema = schema; }
private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { AtomicReference<ExecutionInput> executionInputRef = new AtomicReference<>(executionInput); PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.get(executionInput.getQuery(), transformedQuery -> { // if they change the original query in the pre-parser, then we want to see it downstream from then on executionInputRef.set(executionInput.transform(bldr -> bldr.query(transformedQuery))); return parseAndValidate(executionInputRef.get(), graphQLSchema, instrumentationState); }); if (preparsedDoc.hasErrors()) { return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors())); } return execute(executionInputRef.get(), preparsedDoc.getDocument(), graphQLSchema, instrumentationState); }
private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { AtomicReference<ExecutionInput> executionInputRef = new AtomicReference<>(executionInput); PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.get(executionInput.getQuery(), transformedQuery -> { // if they change the original query in the pre-parser, then we want to see it downstream from then on executionInputRef.set(executionInput.transform(bldr -> bldr.query(transformedQuery))); return parseAndValidate(executionInputRef, graphQLSchema, instrumentationState); }); if (preparsedDoc.hasErrors()) { return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors())); } return execute(executionInputRef.get(), preparsedDoc.getDocument(), graphQLSchema, instrumentationState); }
private PreparsedDocumentEntry parseAndValidate(AtomicReference<ExecutionInput> executionInputRef, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { ExecutionInput executionInput = executionInputRef.get(); String query = executionInput.getQuery(); log.debug("Parsing query: '{}'...", query); ParseResult parseResult = parse(executionInput, graphQLSchema, instrumentationState); if (parseResult.isFailure()) { log.warn("Query failed to parse : '{}'", executionInput.getQuery()); return new PreparsedDocumentEntry(parseResult.getException().toInvalidSyntaxError()); } else { final Document document = parseResult.getDocument(); // they may have changed the document and the variables via instrumentation so update the reference to it executionInput = executionInput.transform(builder -> builder.variables(parseResult.getVariables())); executionInputRef.set(executionInput); log.debug("Validating query: '{}'", query); final List<ValidationError> errors = validate(executionInput, document, graphQLSchema, instrumentationState); if (!errors.isEmpty()) { log.warn("Query failed to validate : '{}'", query); return new PreparsedDocumentEntry(errors); } return new PreparsedDocumentEntry(document); } }
log.debug("Executing request. operation name: '{}'. query: '{}'. variables '{}'", executionInput.getOperationName(), executionInput.getQuery(), executionInput.getVariables());
log.debug("Executing request. operation name: '{}'. query: '{}'. variables '{}'", executionInput.getOperationName(), executionInput.getQuery(), executionInput.getVariables());
private ParseResult parse(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { InstrumentationExecutionParameters parameters = new InstrumentationExecutionParameters(executionInput, graphQLSchema, instrumentationState); InstrumentationContext<Document> parseInstrumentation = instrumentation.beginParse(parameters); Parser parser = new Parser(); Document document; DocumentAndVariables documentAndVariables; try { document = parser.parseDocument(executionInput.getQuery()); documentAndVariables = newDocumentAndVariables() .document(document).variables(executionInput.getVariables()).build(); documentAndVariables = instrumentation.instrumentDocumentAndVariables(documentAndVariables, parameters); } catch (InvalidSyntaxException e) { parseInstrumentation.onCompleted(null, e); return ParseResult.ofError(e); } parseInstrumentation.onCompleted(documentAndVariables.getDocument(), null); return ParseResult.of(documentAndVariables); }
private ParseResult parse(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { InstrumentationExecutionParameters parameters = new InstrumentationExecutionParameters(executionInput, graphQLSchema, instrumentationState); InstrumentationContext<Document> parseInstrumentation = instrumentation.beginParse(parameters); Parser parser = new Parser(); Document document; DocumentAndVariables documentAndVariables; try { document = parser.parseDocument(executionInput.getQuery()); documentAndVariables = newDocumentAndVariables() .document(document).variables(executionInput.getVariables()).build(); documentAndVariables = instrumentation.instrumentDocumentAndVariables(documentAndVariables, parameters); } catch (InvalidSyntaxException e) { parseInstrumentation.onCompleted(null, e); return ParseResult.ofError(e); } parseInstrumentation.onCompleted(documentAndVariables.getDocument(), null); return ParseResult.of(documentAndVariables); }
public InstrumentationExecutionParameters(ExecutionInput executionInput, GraphQLSchema schema, InstrumentationState instrumentationState) { this.executionInput = executionInput; this.query = executionInput.getQuery(); this.operation = executionInput.getOperationName(); this.context = executionInput.getContext(); this.variables = executionInput.getVariables() != null ? executionInput.getVariables() : Collections.emptyMap(); this.instrumentationState = instrumentationState; this.schema = schema; }
public InstrumentationExecutionParameters(ExecutionInput executionInput, GraphQLSchema schema, InstrumentationState instrumentationState) { this.executionInput = executionInput; this.query = executionInput.getQuery(); this.operation = executionInput.getOperationName(); this.context = executionInput.getContext(); this.variables = executionInput.getVariables() != null ? executionInput.getVariables() : Collections.emptyMap(); this.instrumentationState = instrumentationState; this.schema = schema; }
private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { AtomicReference<ExecutionInput> executionInputRef = new AtomicReference<>(executionInput); PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.get(executionInput.getQuery(), transformedQuery -> { // if they change the original query in the pre-parser, then we want to see it downstream from then on executionInputRef.set(executionInput.transform(bldr -> bldr.query(transformedQuery))); return parseAndValidate(executionInputRef.get(), graphQLSchema, instrumentationState); }); if (preparsedDoc.hasErrors()) { return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors())); } return execute(executionInputRef.get(), preparsedDoc.getDocument(), graphQLSchema, instrumentationState); }
private CompletableFuture<ExecutionResult> parseValidateAndExecute(ExecutionInput executionInput, GraphQLSchema graphQLSchema, InstrumentationState instrumentationState) { AtomicReference<ExecutionInput> executionInputRef = new AtomicReference<>(executionInput); PreparsedDocumentEntry preparsedDoc = preparsedDocumentProvider.get(executionInput.getQuery(), transformedQuery -> { // if they change the original query in the pre-parser, then we want to see it downstream from then on executionInputRef.set(executionInput.transform(bldr -> bldr.query(transformedQuery))); return parseAndValidate(executionInputRef, graphQLSchema, instrumentationState); }); if (preparsedDoc.hasErrors()) { return CompletableFuture.completedFuture(new ExecutionResultImpl(preparsedDoc.getErrors())); } return execute(executionInputRef.get(), preparsedDoc.getDocument(), graphQLSchema, instrumentationState); }
@Test public void endpointIsAvailableWithDifferentUrl() { String query = "{foo}"; ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult() .data("bar") .build(); CompletableFuture cf = CompletableFuture.completedFuture(executionResult); ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class); Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf); String body = this.restTemplate.getForObject("/otherUrl/?query={query}", String.class, query); assertThat(body, is("{\"data\":\"bar\"}")); assertThat(captor.getValue().getQuery(), is(query)); }
@Test public void endpointIsAvailable() { String query = "{foo}"; ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult() .data("bar") .build(); CompletableFuture cf = CompletableFuture.completedFuture(executionResult); ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class); Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf); String body = this.restTemplate.getForObject("/graphql?query={query}", String.class, query); assertThat(body, is("{\"data\":\"bar\"}")); assertThat(captor.getValue().getQuery(), is(query)); }
@Test public void endpointIsAvailableWithDifferentUrl() { String query = "{foo}"; ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult() .data("bar") .build(); CompletableFuture cf = CompletableFuture.completedFuture(executionResult); ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class); Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf); this.webClient.get().uri("/otherUrl?query={query}", query).exchange().expectStatus().isOk() .expectBody(String.class).isEqualTo("{\"data\":\"bar\"}"); assertThat(captor.getValue().getQuery(), is(query)); }
@Test public void endpointIsAvailable() { String query = "{foo}"; ExecutionResultImpl executionResult = ExecutionResultImpl.newExecutionResult() .data("bar") .build(); CompletableFuture cf = CompletableFuture.completedFuture(executionResult); ArgumentCaptor<ExecutionInput> captor = ArgumentCaptor.forClass(ExecutionInput.class); Mockito.when(graphql.executeAsync(captor.capture())).thenReturn(cf); this.webClient.get().uri("/graphql?query={query}", query).exchange().expectStatus().isOk() .expectBody(String.class).isEqualTo("{\"data\":\"bar\"}"); assertThat(captor.getValue().getQuery(), is(query)); }