graphql.execution.ExecutionStepInfo Java Examples

The following examples show how to use graphql.execution.ExecutionStepInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FederatedTracingInstrumentation.java    From federation-jvm with MIT License 6 votes vote down vote up
@Override
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
    FederatedTracingState state = parameters.getInstrumentationState();
    if (state == null) {
        return super.beginFieldFetch(parameters);
    }

    SourceLocation fieldLocation = parameters.getEnvironment().getField().getSourceLocation();

    long startNanos = System.nanoTime();
    return whenCompleted((result, throwable) -> {
        long endNanos = System.nanoTime();

        ExecutionStepInfo executionStepInfo = parameters.getEnvironment().getExecutionStepInfo();
        state.addFieldFetchData(
                executionStepInfo,
                // relative to the trace's start_time, in ns
                startNanos - state.getStartRequestNanos(),
                // relative to the trace's start_time, in ns
                endNanos - state.getStartRequestNanos(),
                convertErrors(throwable, result),
                fieldLocation
        );
    });
}
 
Example #2
Source File: MockDataFetchEnvironment.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
public static DataFetchingEnvironment myFastQueryDfe(String typeName, String fieldName, String operationName,
        String executionId) {
    GraphQLNamedType query = mock(GraphQLNamedType.class);
    when(query.getName()).thenReturn(typeName);

    Field field = mock(Field.class);
    when(field.getName()).thenReturn(fieldName);

    OperationDefinition operationDefinition = mock(OperationDefinition.class);
    when(operationDefinition.getName()).thenReturn(operationName);

    ExecutionPath executionPath = mock(ExecutionPath.class);
    when(executionPath.toString()).thenReturn("/" + typeName + "/" + fieldName);
    ExecutionStepInfo executionStepInfo = mock(ExecutionStepInfo.class);
    when(executionStepInfo.getPath()).thenReturn(executionPath);

    DataFetchingEnvironment dfe = mock(DataFetchingEnvironment.class);
    when(dfe.getParentType()).thenReturn(query);
    when(dfe.getField()).thenReturn(field);
    when(dfe.getOperationDefinition()).thenReturn(operationDefinition);
    when(dfe.getExecutionStepInfo()).thenReturn(executionStepInfo);
    when(dfe.getExecutionId()).thenReturn(ExecutionId.from(executionId));

    return dfe;
}
 
Example #3
Source File: Directives.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
public static FragmentDirectiveCollector collect(DataFetchingEnvironment env, ExecutionStepInfo step) {
    FragmentDirectiveCollector fragmentDirectiveCollector = new FragmentDirectiveCollector(env);
    // This is safe because top-level fields don't get to here and all deeper fields at least have a parent (source object) and a grand-parent (query root)
    ExecutionStepInfo rootStep = step.getParent().getParent();
    if (rootStep == null) { //Should never be possible, see above
        return fragmentDirectiveCollector;
    }
    GraphQLType rootParentType = GraphQLUtils.unwrapNonNull(rootStep.getType());
    while(!(rootParentType instanceof GraphQLObjectType)) {
        rootStep = rootStep.getParent();
        rootParentType = GraphQLUtils.unwrapNonNull(rootStep.getType());
    }
    QueryTraverser traversal = QueryTraverser.newQueryTraverser()
            .fragmentsByName(env.getFragmentsByName())
            .schema(env.getGraphQLSchema())
            .variables(env.getVariables())
            .root(env.getExecutionStepInfo().getParent().getField().getSingleField())
            .rootParentType((GraphQLObjectType) rootParentType)
            .build();
    traversal.visitPostOrder(fragmentDirectiveCollector);
    return fragmentDirectiveCollector;
}
 
Example #4
Source File: FederatedTracingInstrumentation.java    From federation-jvm with MIT License 5 votes vote down vote up
/**
 * Adds stats data collected from a field fetch.
 */
void addFieldFetchData(ExecutionStepInfo stepInfo, long startFieldNanos, long endFieldNanos, List<GraphQLError> errors, SourceLocation fieldLocation) {
    ExecutionPath path = stepInfo.getPath();
    protoBuilderTree.editBuilder(path, (builder) -> {
        builder.setStartTime(startFieldNanos)
                .setEndTime(endFieldNanos)
                .setParentType(simplePrint(stepInfo.getParent().getUnwrappedNonNullType()))
                .setType(stepInfo.simplePrint())
                .setResponseName(stepInfo.getResultKey());

        // set originalFieldName only when a field alias was used
        String originalFieldName = stepInfo.getField().getName();
        if (!originalFieldName.equals(stepInfo.getResultKey())) {
            builder.setOriginalFieldName(originalFieldName);
        }

        errors.forEach(error -> {
            Reports.Trace.Error.Builder errorBuilder = builder.addErrorBuilder()
                    .setMessage(error.getMessage());
            if (error.getLocations().isEmpty() && fieldLocation != null) {
                errorBuilder.addLocationBuilder()
                        .setColumn(fieldLocation.getColumn())
                        .setLine(fieldLocation.getLine());
            } else {
                error.getLocations().forEach(location -> errorBuilder.addLocationBuilder()
                        .setColumn(location.getColumn())
                        .setLine(location.getLine()));
            }
        });
    });
}
 
Example #5
Source File: PublisherAdapter.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private <R> CompletableFuture<DataFetcherResult<List<R>>> collect(Publisher<R> publisher, ExecutionStepInfo step) {
    CompletableFuture<DataFetcherResult<List<R>>> promise = new CompletableFuture<>();

    executor.execute(() -> publisher.subscribe(new Subscriber<R>() {

        private List<R> buffer = new ArrayList<>();

        @Override
        public void onSubscribe(Subscription subscription) {
            subscription.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(R result) {
            buffer.add(result);
        }

        @Override
        public void onError(Throwable error) {
            ExceptionWhileDataFetching wrapped = new ExceptionWhileDataFetching(step.getPath(), error, step.getField().getSingleField().getSourceLocation());
            promise.complete(DataFetcherResult.<List<R>>newResult()
                    .data(buffer)
                    .error(wrapped)
                    .build());
        }

        @Override
        public void onComplete() {
            promise.complete(DataFetcherResult.<List<R>>newResult().data(buffer).build());
        }
    }));
    return promise;
}
 
Example #6
Source File: RelayDataFetchingEnvironmentDecorator.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public ExecutionStepInfo getExecutionStepInfo() {
    return delegate.getExecutionStepInfo();
}
 
Example #7
Source File: Directives.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
Directives(DataFetchingEnvironment env, ExecutionStepInfo step) {
    List<Field> fields = env.getMergedField().getFields();
    if (step != null) {
        fields = step.getField() != null ? step.getField().getFields() : Collections.emptyList();
    }
    step = step != null ? step : env.getExecutionStepInfo();
    // Field directives
    fields.forEach(field ->
            directives.merge(Introspection.DirectiveLocation.FIELD, parseDirectives(field.getDirectives(), env), (directiveMap1, directiveMap2) -> {
                directiveMap2.forEach((directiveName, directiveValues) -> directiveMap1.merge(directiveName, directiveValues,
                        (valueList1, valueList2) -> Stream.concat(valueList1.stream(), valueList2.stream()).collect(Collectors.toList()))
                );
                return directiveMap1;
            }));

    // Operation directives
    Map<String, List<Map<String, Object>>> operationDirectives = parseDirectives(env.getOperationDefinition().getDirectives(), env);

    switch (env.getOperationDefinition().getOperation()) {
        case QUERY:
            directives.put(Introspection.DirectiveLocation.QUERY, operationDirectives);
            directives.put(Introspection.DirectiveLocation.MUTATION, Collections.emptyMap());
            directives.put(Introspection.DirectiveLocation.SUBSCRIPTION, Collections.emptyMap());
            break;
        case MUTATION:
            directives.put(Introspection.DirectiveLocation.MUTATION, operationDirectives);
            directives.put(Introspection.DirectiveLocation.QUERY, Collections.emptyMap());
            directives.put(Introspection.DirectiveLocation.SUBSCRIPTION, Collections.emptyMap());
            break;
        case SUBSCRIPTION:
            directives.put(Introspection.DirectiveLocation.SUBSCRIPTION, operationDirectives);
            directives.put(Introspection.DirectiveLocation.QUERY, Collections.emptyMap());
            directives.put(Introspection.DirectiveLocation.MUTATION, Collections.emptyMap());
            break;
    }
    if (OperationDefinition.Operation.MUTATION.equals(env.getOperationDefinition().getOperation())) {
        directives.put(Introspection.DirectiveLocation.MUTATION, operationDirectives);
        directives.put(Introspection.DirectiveLocation.QUERY, Collections.emptyMap());
    } else {
        directives.put(Introspection.DirectiveLocation.QUERY, operationDirectives);
        directives.put(Introspection.DirectiveLocation.MUTATION, Collections.emptyMap());
    }

    // Fragment directives
    if (step.hasParent() && step.getParent().getField() != null) {
        FragmentDirectiveCollector fragmentDirectiveCollector = FragmentDirectiveCollector.collect(env, step);
        directives.put(Introspection.DirectiveLocation.INLINE_FRAGMENT, parseDirectives(fragmentDirectiveCollector.getInlineFragmentDirs(), env));
        directives.put(Introspection.DirectiveLocation.FRAGMENT_SPREAD, parseDirectives(fragmentDirectiveCollector.getFragmentDirs(), env));
        directives.put(Introspection.DirectiveLocation.FRAGMENT_DEFINITION, parseDirectives(fragmentDirectiveCollector.getFragmentDefDirs(), env));
    } else {
        directives.put(Introspection.DirectiveLocation.INLINE_FRAGMENT, Collections.emptyMap());
        directives.put(Introspection.DirectiveLocation.FRAGMENT_SPREAD, Collections.emptyMap());
        directives.put(Introspection.DirectiveLocation.FRAGMENT_DEFINITION, Collections.emptyMap());
    }
}
 
Example #8
Source File: ResolutionEnvironment.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
public Directives getDirectives(ExecutionStepInfo step) {
    return new Directives(dataFetchingEnvironment, step);
}
 
Example #9
Source File: LookUpSubjectByUriFetcherWrapperTest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ExecutionStepInfo getExecutionStepInfo() {
  throw new UnsupportedOperationException("Not yet implemented");//FIXME: implement
}