Esempio n. 1
0
    public static async Task <ExecutionResult> ExecuteMutationAsync(
        QueryContext context)
    {
        var(schema, _, operation, initialValue, coercedVariableValues) = context;
        var executionContext = context.BuildExecutorContext(new SerialExecutionStrategy());
        var path             = new NodePath();

        var mutationType = schema.Mutation;

        if (mutationType == null)
        {
            throw new QueryExecutionException(
                      "Schema does not support mutations. Mutation type is null.",
                      path);
        }

        var selectionSet = operation.SelectionSet;
        var data         = await SelectionSets.ExecuteSelectionSetAsync(
            executionContext,
            selectionSet,
            mutationType,
            initialValue,
            path).ConfigureAwait(false);


        return(new ExecutionResult
        {
            Errors = executionContext
                     .FieldErrors
                     .Select(context.FormatError).ToList(),
            Data = data?.ToDictionary(kv => kv.Key, kv => kv.Value)
        });
    }
Esempio n. 2
0
    public static async Task <SubscriptionResult> SubscribeAsync(
        QueryContext context,
        CancellationToken cancellationToken)
    {
        cancellationToken.ThrowIfCancellationRequested();

        var(schema, _, operation, initialValue, coercedVariableValues) = context;

        if (schema.Subscription == null)
        {
            throw new QueryExecutionException(
                      "Schema does not support subscriptions. Subscription type is null",
                      new NodePath());
        }

        var executionContext = context.BuildExecutorContext(new ParallelExecutionStrategy());

        try
        {
            var sourceStream = await CreateSourceEventStreamAsync(
                executionContext,
                operation,
                coercedVariableValues,
                initialValue,
                cancellationToken).ConfigureAwait(false);

            var responseStream = MapSourceToResponseEventAsync(
                executionContext,
                sourceStream,
                operation,
                coercedVariableValues,
                context.FormatError,
                cancellationToken);

            return(responseStream);
        }
        catch (QueryExecutionException e)
        {
            executionContext.AddError(e);
        }

        return(new SubscriptionResult(null)
        {
            Errors = executionContext
                     .FieldErrors
                     .Select(context.FormatError)
                     .ToList()
        });
    }
Esempio n. 3
0
    public static async Task <ExecutionResult> ExecuteQueryAsync(
        QueryContext context)
    {
        var(schema, _, operation, initialValue, coercedVariableValues) = context;
        var queryType = schema.Query;
        var path      = new NodePath();

        if (queryType == null)
        {
            throw new QueryExecutionException(
                      "Schema does not support queries. Query type is null.",
                      path);
        }

        var selectionSet     = operation.SelectionSet;
        var executionContext = context.BuildExecutorContext(new ParallelExecutionStrategy());

        IDictionary <string, object?>?data;

        try
        {
            data = await SelectionSets.ExecuteSelectionSetAsync(
                executionContext,
                selectionSet,
                queryType,
                initialValue,
                path).ConfigureAwait(false);
        }
        catch (QueryExecutionException e)
        {
            executionContext.AddError(e);
            data = null;
        }

        return(new ExecutionResult
        {
            Errors = executionContext
                     .FieldErrors
                     .Select(context.FormatError)
                     .ToList(),
            Data = data?.ToDictionary(kv => kv.Key, kv => kv.Value)
        });
    }