use of graphql.execution.ResultPath in project graphql-java by graphql-java.
the class FieldLevelTrackingApproach method beginFieldFetch.
public InstrumentationContext<Object> beginFieldFetch(InstrumentationFieldFetchParameters parameters) {
CallStack callStack = parameters.getInstrumentationState();
ResultPath path = parameters.getEnvironment().getExecutionStepInfo().getPath();
int level = path.getLevel();
return new InstrumentationContext<Object>() {
@Override
public void onDispatched(CompletableFuture result) {
boolean dispatchNeeded;
synchronized (callStack) {
callStack.increaseFetchCount(level);
dispatchNeeded = dispatchIfNeeded(callStack, level);
}
if (dispatchNeeded) {
dispatch();
}
}
@Override
public void onCompleted(Object result, Throwable t) {
}
};
}
use of graphql.execution.ResultPath in project graphql-java by graphql-java.
the class FieldValidationSupport method validateFieldsAndArguments.
static List<GraphQLError> validateFieldsAndArguments(FieldValidation fieldValidation, ExecutionContext executionContext) {
Map<ResultPath, List<FieldAndArguments>> fieldArgumentsMap = new LinkedHashMap<>();
QueryTraverser queryTraverser = QueryTraverser.newQueryTraverser().schema(executionContext.getGraphQLSchema()).document(executionContext.getDocument()).operationName(executionContext.getOperationDefinition().getName()).variables(executionContext.getVariables()).build();
queryTraverser.visitPreOrder(new QueryVisitorStub() {
@Override
public void visitField(QueryVisitorFieldEnvironment env) {
Field field = env.getField();
if (field.getArguments() != null && !field.getArguments().isEmpty()) {
//
// only fields that have arguments make any sense to placed in play
// since only they have variable input
FieldAndArguments fieldArguments = new FieldAndArgumentsImpl(env);
ResultPath path = fieldArguments.getPath();
List<FieldAndArguments> list = fieldArgumentsMap.getOrDefault(path, new ArrayList<>());
list.add(fieldArguments);
fieldArgumentsMap.put(path, list);
}
}
});
FieldValidationEnvironment environment = new FieldValidationEnvironmentImpl(executionContext, fieldArgumentsMap);
// this will allow a consumer to plugin their own validation of fields and arguments
return fieldValidation.validateFields(environment);
}
use of graphql.execution.ResultPath in project graphql-java by graphql-java.
the class InstrumentationExamples method fieldValidation.
private void fieldValidation() {
ResultPath fieldPath = ResultPath.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.getArgumentValue("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();
}
use of graphql.execution.ResultPath in project solarthing by wildmountainfarms.
the class SolarThingExceptionHandler method onException.
/*
It's worth nothing that SimpleDataFetcherExceptionHandler appends a "notprivacysafe." in front of the full class name for the logger.
We aren't going to do that here, as there's no private information, but it's just worth noting the difference here.
*/
@Override
public DataFetcherExceptionHandlerResult onException(DataFetcherExceptionHandlerParameters handlerParameters) {
Throwable exception = handlerParameters.getException();
SourceLocation sourceLocation = handlerParameters.getSourceLocation();
ResultPath path = handlerParameters.getPath();
if (exception instanceof DatabaseException) {
// this is the most common exception, usually caused by a timeout
LOGGER.info("Got database exception", exception);
} else {
LOGGER.warn("Got uncommon exception", exception);
}
ExceptionWhileDataFetching error = new ExceptionWhileDataFetching(path, exception, sourceLocation);
return DataFetcherExceptionHandlerResult.newResult().error(error).build();
}
use of graphql.execution.ResultPath in project molgenis-emx2 by molgenis.
the class GraphqlCustomExceptionHandler method onException.
@Override
public DataFetcherExceptionHandlerResult onException(DataFetcherExceptionHandlerParameters handlerParameters) {
final Throwable exception = handlerParameters.getException();
final SourceLocation sourceLocation = handlerParameters.getSourceLocation();
final ResultPath path = handlerParameters.getPath();
GraphQLError error = new ExceptionWhileDataFetching(path, exception, sourceLocation);
final ErrorClassification errorType = error.getErrorType();
if (exception instanceof MolgenisException) {
error = new GraphQLError() {
@Override
public String getMessage() {
return exception.toString();
}
@Override
public List<SourceLocation> getLocations() {
return List.of(sourceLocation);
}
@Override
public ErrorClassification getErrorType() {
return errorType;
}
};
}
return DataFetcherExceptionHandlerResult.newResult().error(error).build();
}
Aggregations