示例#1
0
        /// <summary>
        ///     For each subscription operation definition subscription in the document
        ///     Let subscriptionType be the root Subscription type in schema.
        ///     Let selectionSet be the top level selection set on subscription.
        ///     Let variableValues be the empty set.
        ///     Let groupedFieldSet be the result of CollectFields(subscriptionType, selectionSet, variableValues).
        ///     groupedFieldSet must have exactly one entry.
        /// </summary>
        public static CombineRule R5231SingleRootField()
        {
            return((context, rule) =>
            {
                rule.EnterDocument += document =>
                {
                    var subscriptions = document.Definitions
                                        .OfType <GraphQLOperationDefinition>()
                                        .Where(op => op.Operation == OperationType.Subscription)
                                        .ToList();

                    if (!subscriptions.Any())
                    {
                        return;
                    }

                    var schema = context.Schema;
                    //todo(pekka): should this report error?
                    if (schema.Subscription == null)
                    {
                        return;
                    }

                    var subscriptionType = schema.Subscription;
                    foreach (var subscription in subscriptions)
                    {
                        var selectionSet = subscription.SelectionSet;
                        var variableValues = new Dictionary <string, object>();

                        var groupedFieldSet = SelectionSets.CollectFields(
                            schema,
                            context.Document,
                            subscriptionType,
                            selectionSet,
                            variableValues);

                        if (groupedFieldSet.Count != 1)
                        {
                            context.Error(
                                ValidationErrorCodes.R5231SingleRootField,
                                "Subscription operations must have exactly one root field.",
                                subscription);
                        }
                    }
                };
            });
        }
示例#2
0
    public static async Task <ISubscriberResult> CreateSourceEventStreamAsync(
        IExecutorContext context,
        OperationDefinition subscription,
        IReadOnlyDictionary <string, object?> coercedVariableValues,
        object initialValue,
        CancellationToken cancellationToken)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        if (subscription == null)
        {
            throw new ArgumentNullException(nameof(subscription));
        }
        if (coercedVariableValues == null)
        {
            throw new ArgumentNullException(nameof(coercedVariableValues));
        }

        cancellationToken.ThrowIfCancellationRequested();

        var schema           = context.Schema;
        var subscriptionType = schema.Subscription;
        var groupedFieldSet  = SelectionSets.CollectFields(
            context.Schema,
            context.Document,
            subscriptionType,
            subscription.SelectionSet,
            coercedVariableValues
            );

        var fields         = groupedFieldSet.Values.First();
        var fieldName      = fields.First().Name;
        var fieldSelection = fields.First();

        var coercedArgumentValues = ArgumentCoercion.CoerceArgumentValues(
            schema,
            subscriptionType,
            fieldSelection,
            coercedVariableValues);

        var field          = schema.GetField(subscriptionType.Name, fieldName);
        var path           = new NodePath();
        var resolveContext = new ResolverContext(
            subscriptionType,
            initialValue,
            field,
            fieldSelection,
            fields,
            coercedArgumentValues,
            path,
            context);

        var subscriber = schema.GetSubscriber(subscriptionType.Name, fieldName);

        if (subscriber == null)
        {
            throw new QueryExecutionException(
                      $"Could not subscribe. Field '{subscriptionType}:{fieldName}' does not have subscriber",
                      path);
        }

        var subscribeResult = await subscriber(resolveContext, cancellationToken)
                              .ConfigureAwait(false);

        return(subscribeResult);
    }