Skip to content

Commit

Permalink
Merge pull request #197 from Netflix/graphql-java-22
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbakker authored May 29, 2024
2 parents 18ddaf2 + 184c9e8 commit 02d93ba
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ dependencies {
implementation("com.netflix.graphql.dgs:graphql-dgs-spring-graphql-starter")
implementation("com.netflix.graphql.dgs:graphql-dgs-extended-scalars")
implementation("org.springframework.boot:spring-boot-starter-websocket")
implementation("name.nkonev.multipart-spring-graphql:multipart-spring-graphql:1.1.4")
implementation("name.nkonev.multipart-spring-graphql:multipart-spring-graphql:1.+")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("net.datafaker:datafaker:2.+")
implementation("com.github.ben-manes.caffeine:caffeine")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public class UppercaseDirective implements SchemaDirectiveWiring {

@Override
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> env) {
GraphQLFieldsContainer fieldsContainer = env.getFieldsContainer();
GraphQLFieldDefinition fieldDefinition = env.getFieldDefinition();

DataFetcher<?> originalDataFetcher = env.getCodeRegistry().getDataFetcher(fieldsContainer, fieldDefinition);
DataFetcher<?> originalDataFetcher = env.getFieldDataFetcher();
DataFetcher<?> dataFetcher = DataFetcherFactories.wrapDataFetcher(
originalDataFetcher,
(dataFetchingEnvironment, value) -> {
Expand All @@ -27,7 +24,7 @@ public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFi
}
);

env.getCodeRegistry().dataFetcher(fieldsContainer, fieldDefinition, dataFetcher);
return fieldDefinition;
env.setFieldDataFetcher(dataFetcher);
return env.getElement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,40 @@
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.execution.instrumentation.SimpleInstrumentation;
import graphql.execution.instrumentation.SimplePerformantInstrumentation;
import graphql.execution.instrumentation.parameters.InstrumentationCreateStateParameters;
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters;
import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

@Component
public class ExampleTracingInstrumentation extends SimpleInstrumentation {
public class ExampleTracingInstrumentation extends SimplePerformantInstrumentation {
private final static Logger LOGGER = LoggerFactory.getLogger(ExampleTracingInstrumentation.class);

@Override
public InstrumentationState createState() {
public @Nullable InstrumentationState createState(InstrumentationCreateStateParameters parameters) {
return new TracingState();
}

@Override
public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) {
TracingState tracingState = parameters.getInstrumentationState();
public @Nullable InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters, InstrumentationState state) {
TracingState tracingState = (TracingState) state;
tracingState.startTime = System.currentTimeMillis();
return super.beginExecution(parameters);
return super.beginExecution(parameters, state);
}

@Override
public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters) {
public @NotNull DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state) {
// We only care about user code
if(parameters.isTrivialDataFetcher()) {
return dataFetcher;
Expand All @@ -56,12 +61,12 @@ public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, Instrume
}

@Override
public CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters) {
TracingState tracingState = parameters.getInstrumentationState();
public @NotNull CompletableFuture<ExecutionResult> instrumentExecutionResult(ExecutionResult executionResult, InstrumentationExecutionParameters parameters, InstrumentationState state) {
TracingState tracingState = (TracingState) state;
long totalTime = System.currentTimeMillis() - tracingState.startTime;
LOGGER.info("Total execution time: {}ms", totalTime);

return super.instrumentExecutionResult(executionResult, parameters);
return super.instrumentExecutionResult(executionResult, parameters, state);
}

private String findDatafetcherTag(InstrumentationFieldFetchParameters parameters) {
Expand Down

0 comments on commit 02d93ba

Please sign in to comment.