use of graphql.language.OperationDefinition in project graphql-java by graphql-java.
the class GraphqlAntlrToLanguage method visitOperationDefinition.
@Override
public Void visitOperationDefinition(GraphqlParser.OperationDefinitionContext ctx) {
OperationDefinition operationDefinition;
operationDefinition = new OperationDefinition();
newNode(operationDefinition, ctx);
if (ctx.operationType() == null) {
operationDefinition.setOperation(OperationDefinition.Operation.QUERY);
} else {
operationDefinition.setOperation(parseOperation(ctx.operationType()));
}
if (ctx.name() != null) {
operationDefinition.setName(ctx.name().getText());
}
result.getDefinitions().add(operationDefinition);
addContextProperty(ContextProperty.OperationDefinition, operationDefinition);
super.visitOperationDefinition(ctx);
popContext();
return null;
}
use of graphql.language.OperationDefinition in project graphql-java by graphql-java.
the class GraphqlAntlrToLanguage method visitVariableDefinition.
@Override
public Void visitVariableDefinition(GraphqlParser.VariableDefinitionContext ctx) {
VariableDefinition variableDefinition = new VariableDefinition();
newNode(variableDefinition, ctx);
variableDefinition.setName(ctx.variable().name().getText());
if (ctx.defaultValue() != null) {
Value value = getValue(ctx.defaultValue().value());
variableDefinition.setDefaultValue(value);
}
OperationDefinition operationDefinition = (OperationDefinition) getFromContextStack(ContextProperty.OperationDefinition);
operationDefinition.getVariableDefinitions().add(variableDefinition);
addContextProperty(ContextProperty.VariableDefinition, variableDefinition);
super.visitVariableDefinition(ctx);
popContext();
return null;
}
use of graphql.language.OperationDefinition in project graphql-java by graphql-java.
the class Execution method execute.
public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput, InstrumentationState instrumentationState) {
NodeUtil.GetOperationResult getOperationResult = NodeUtil.getOperation(document, executionInput.getOperationName());
Map<String, FragmentDefinition> fragmentsByName = getOperationResult.fragmentsByName;
OperationDefinition operationDefinition = getOperationResult.operationDefinition;
ValuesResolver valuesResolver = new ValuesResolver();
Map<String, Object> inputVariables = executionInput.getVariables();
List<VariableDefinition> variableDefinitions = operationDefinition.getVariableDefinitions();
Map<String, Object> coercedVariables;
try {
coercedVariables = valuesResolver.coerceArgumentValues(graphQLSchema, variableDefinitions, inputVariables);
} catch (RuntimeException rte) {
if (rte instanceof GraphQLError) {
return completedFuture(new ExecutionResultImpl((GraphQLError) rte));
}
throw rte;
}
ExecutionContext executionContext = newExecutionContextBuilder().instrumentation(instrumentation).instrumentationState(instrumentationState).executionId(executionId).graphQLSchema(graphQLSchema).queryStrategy(queryStrategy).mutationStrategy(mutationStrategy).subscriptionStrategy(subscriptionStrategy).context(executionInput.getContext()).root(executionInput.getRoot()).fragmentsByName(fragmentsByName).variables(coercedVariables).document(document).operationDefinition(operationDefinition).build();
InstrumentationExecutionParameters parameters = new InstrumentationExecutionParameters(executionInput, graphQLSchema, instrumentationState);
executionContext = instrumentation.instrumentExecutionContext(executionContext, parameters);
return executeOperation(executionContext, parameters, executionInput.getRoot(), executionContext.getOperationDefinition());
}
use of graphql.language.OperationDefinition in project graphql-java by graphql-java.
the class Execution method executeOperation.
private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext executionContext, InstrumentationExecutionParameters instrumentationExecutionParameters, Object root, OperationDefinition operationDefinition) {
InstrumentationExecuteOperationParameters instrumentationParams = new InstrumentationExecuteOperationParameters(executionContext);
InstrumentationContext<ExecutionResult> executeOperationCtx = instrumentation.beginExecuteOperation(instrumentationParams);
OperationDefinition.Operation operation = operationDefinition.getOperation();
GraphQLObjectType operationRootType;
try {
operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operationDefinition);
} catch (RuntimeException rte) {
if (rte instanceof GraphQLError) {
ExecutionResult executionResult = new ExecutionResultImpl(Collections.singletonList((GraphQLError) rte));
CompletableFuture<ExecutionResult> resultCompletableFuture = completedFuture(executionResult);
executeOperationCtx.onDispatched(resultCompletableFuture);
executeOperationCtx.onCompleted(executionResult, rte);
return resultCompletableFuture;
}
throw rte;
}
FieldCollectorParameters collectorParameters = FieldCollectorParameters.newParameters().schema(executionContext.getGraphQLSchema()).objectType(operationRootType).fragments(executionContext.getFragmentsByName()).variables(executionContext.getVariables()).build();
Map<String, List<Field>> fields = fieldCollector.collectFields(collectorParameters, operationDefinition.getSelectionSet());
ExecutionPath path = ExecutionPath.rootPath();
ExecutionTypeInfo typeInfo = newTypeInfo().type(operationRootType).path(path).build();
NonNullableFieldValidator nonNullableFieldValidator = new NonNullableFieldValidator(executionContext, typeInfo);
ExecutionStrategyParameters parameters = newParameters().typeInfo(typeInfo).source(root).fields(fields).nonNullFieldValidator(nonNullableFieldValidator).path(path).build();
CompletableFuture<ExecutionResult> result;
try {
ExecutionStrategy executionStrategy;
if (operation == OperationDefinition.Operation.MUTATION) {
executionStrategy = mutationStrategy;
} else if (operation == SUBSCRIPTION) {
executionStrategy = subscriptionStrategy;
} else {
executionStrategy = queryStrategy;
}
log.debug("Executing '{}' query operation: '{}' using '{}' execution strategy", executionContext.getExecutionId(), operation, executionStrategy.getClass().getName());
result = executionStrategy.execute(executionContext, parameters);
} catch (NonNullableFieldWasNullException e) {
// this means it was non null types all the way from an offending non null type
// up to the root object type and there was a a null value some where.
//
// The spec says we should return null for the data in this case
//
// http://facebook.github.io/graphql/#sec-Errors-and-Non-Nullability
//
result = completedFuture(new ExecutionResultImpl(null, executionContext.getErrors()));
}
// note this happens NOW - not when the result completes
executeOperationCtx.onDispatched(result);
result = result.whenComplete(executeOperationCtx::onCompleted);
return deferSupport(executionContext, result);
}
use of graphql.language.OperationDefinition in project structr by structr.
the class GraphQLRequest method initialize.
// ----- private methods -----
private void initialize(final SecurityContext securityContext, Document document) throws FrameworkException {
for (final Node child : document.getChildren()) {
if (child instanceof OperationDefinition) {
final OperationDefinition operationDefinition = (OperationDefinition) child;
final SelectionSet selectionSet = operationDefinition.getSelectionSet();
for (final Selection selection : selectionSet.getSelections()) {
if (selection instanceof Field) {
queries.add(new GraphQLQuery(securityContext, (Field) selection));
} else {
logger.warn("Unknown selection set element {} in GraphQL query, ignoring.", selection.getClass());
}
}
} else {
logger.warn("Unknown document element {} in GraphQL query, ignoring.", child.getClass());
}
}
}
Aggregations