public async Task <IReadOnlyQueryResult> ReadAsync(
            CancellationToken cancellationToken)
        {
            try
            {
                IReadOnlyQueryRequest request = _batch[_index++];

                DocumentNode document = request.Query is QueryDocument d
                    ? d.Document
                    : Utf8GraphQLParser.Parse(request.Query.ToSpan());

                OperationDefinitionNode operation =
                    document.GetOperation(request.OperationName);

                if (document != _previous)
                {
                    _fragments = document.GetFragments();
                    _visitationMap.Initialize(_fragments);
                }

                operation.Accept(
                    _visitor,
                    _visitationMap,
                    n => VisitorAction.Continue);

                _previous = document;
                document  = RewriteDocument(operation);
                operation = (OperationDefinitionNode)document.Definitions[0];
                IReadOnlyDictionary <string, object> variableValues =
                    MergeVariables(request.VariableValues, operation);

                request = QueryRequestBuilder.From(request)
                          .SetQuery(document)
                          .SetVariableValues(variableValues)
                          .AddExportedVariables(_exportedVariables)
                          .SetQueryName(null) // TODO ... should we create a name here?
                          .SetQueryHash(null)
                          .Create();

                var result =
                    (IReadOnlyQueryResult)await _executor.ExecuteAsync(
                        request, cancellationToken)
                    .ConfigureAwait(false);

                IsCompleted = _index >= _batch.Count;
                return(result);
            }
            catch (QueryException ex)
            {
                IsCompleted = true;
                return(QueryResult.CreateError(ex.Errors));
            }
            catch (Exception ex)
            {
                IsCompleted = true;
                return(QueryResult.CreateError(
                           _errorHandler.Handle(
                               _errorHandler.CreateUnexpectedError(ex).Build())));
            }
        }
예제 #2
0
            private async Task <IQueryResult> ExecuteNextAsync(
                IReadOnlyQueryRequest request,
                CancellationToken cancellationToken)
            {
                try
                {
                    DocumentNode document = request.Query is QueryDocument d
                        ? d.Document
                        : Utf8GraphQLParser.Parse(request.Query !.AsSpan());

                    OperationDefinitionNode operation =
                        document.GetOperation(request.OperationName);

                    if (document != _previous)
                    {
                        _fragments = document.GetFragments();
                        _visitationMap.Initialize(_fragments);
                    }

                    operation.Accept(
                        _visitor,
                        _visitationMap,
                        n => VisitorAction.Continue);

                    _previous = document;
                    document  = RewriteDocument(operation);
                    operation = (OperationDefinitionNode)document.Definitions[0];
                    IReadOnlyDictionary <string, object?>?variableValues =
                        MergeVariables(request.VariableValues, operation);

                    request = QueryRequestBuilder.From(request)
                              .SetQuery(document)
                              .SetVariableValues(variableValues)
                              .AddExportedVariables(_exportedVariables)
                              .SetQueryId(null) // TODO ... should we create a name here?
                              .SetQueryHash(null)
                              .Create();

                    return((IReadOnlyQueryResult)await _requestExecutor.ExecuteAsync(
                               request, cancellationToken)
                           .ConfigureAwait(false));
                }
                catch (GraphQLException ex)
                {
                    return(QueryResultBuilder.CreateError(ex.Errors));
                }
                catch (Exception ex)
                {
                    return(QueryResultBuilder.CreateError(
                               _errorHandler.Handle(
                                   _errorHandler.CreateUnexpectedError(ex).Build())));
                }
            }
        public void FindUndeclaredVariablesFragmentDefinition()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"
                query getHero {
                    ... q
                }

                fragment q on Query {
                    hero(episode: $ep) {
                        name
                    }
                }
                ");

            OperationDefinitionNode operation = document.Definitions
                                                .OfType <OperationDefinitionNode>()
                                                .First();

            var visitor       = new CollectVariablesVisitor(schema);
            var visitationMap = new CollectVariablesVisitationMap();

            visitationMap.Initialize(
                document.Definitions.OfType <FragmentDefinitionNode>()
                .ToDictionary(t => t.Name.Value));

            // act
            operation.Accept(
                visitor,
                visitationMap,
                node => VisitorAction.Continue);

            // assert
            var variables = operation.VariableDefinitions.ToList();

            variables.AddRange(visitor.VariableDeclarations);
            operation = operation.WithVariableDefinitions(variables);

            var definitions = new List <IDefinitionNode>();

            definitions.Add(operation);
            definitions.AddRange(
                document.Definitions.OfType <FragmentDefinitionNode>());

            QuerySyntaxSerializer.Serialize(
                new DocumentNode(
                    definitions)).MatchSnapshot();
        }
        public void FindUndeclaredVariablesInlineFragment()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"
                query getHero {
                    ... on Query {
                        hero(episode: $ep) {
                            name
                        }
                    }
                }");

            OperationDefinitionNode operation = document.Definitions
                                                .OfType <OperationDefinitionNode>()
                                                .First();

            var visitor       = new CollectVariablesVisitor(schema);
            var visitationMap = new CollectVariablesVisitationMap();

            // act
            operation.Accept(
                visitor,
                visitationMap,
                node => VisitorAction.Continue);

            // assert
            var variables = operation.VariableDefinitions.ToList();

            variables.AddRange(visitor.VariableDeclarations);
            operation = operation.WithVariableDefinitions(variables);

            new DocumentNode(
                new IDefinitionNode[]
            {
                operation
            }).Print().MatchSnapshot();
        }