Ejemplo n.º 1
0
        public async Task ExecuteMutationSerially()
        {
            // arrange
            var state = 0;

            var schema = Schema.Create(
                FileResource.Open("MutationExecutionSchema.graphql"),
                cnf =>
            {
                cnf.BindResolver(() => state)
                .To("Query", "state");
                cnf.BindResolver(() => state)
                .To("CurrentState", "theNumber");
                cnf.BindResolver(ctx => state = ctx.Argument <int>("newNumber"))
                .To("Mutation", "changeTheNumber");
            });

            DocumentNode query = Parser.Default.Parse(
                FileResource.Open("MutationExecutionQuery.graphql"));

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().FirstOrDefault();

            // act
            var operationExecuter =
                new OperationExecuter(schema, query, operation);

            IExecutionResult result = await operationExecuter.ExecuteAsync(
                new OperationRequest(schema.Services, CreateSession()),
                CancellationToken.None);

            // assert
            Assert.Null(result.Errors);
            result.Snapshot();
        }
Ejemplo n.º 2
0
        private async Task <IExecutionResult> ExecuteInternalAsync(
            QueryRequest queryRequest,
            QueryInfo queryInfo,
            CancellationToken cancellationToken)
        {
            OperationRequest operationRequest = null;

            try
            {
                OperationExecuter operationExecuter =
                    GetOrCreateOperationExecuter(
                        queryRequest, queryInfo.QueryDocument);

                operationRequest =
                    CreateOperationRequest(queryRequest);

                return(await operationExecuter.ExecuteAsync(
                           operationRequest, cancellationToken));
            }
            catch (QueryException ex)
            {
                return(new QueryResult(ex.Errors));
            }
            catch (Exception ex)
            {
                return(new QueryResult(CreateErrorFromException(ex)));
            }
            finally
            {
                operationRequest?.Session.Dispose();
            }
        }
Ejemplo n.º 3
0
        public async Task ResolveSimpleOneLevelQuery()
        {
            // arrange
            Schema schema  = CreateSchema();
            var    session = new Mock <ISession>(MockBehavior.Strict);

            session.Setup(t => t.DataLoaders).Returns((IDataLoaderProvider)null);
            session.Setup(t => t.CustomContexts).Returns((ICustomContextProvider)null);
            DocumentNode            query     = Parser.Default.Parse(@"
                {
                    a
                }");
            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().FirstOrDefault();

            // act
            OperationExecuter operationExecuter =
                new OperationExecuter(schema, query, operation);
            IExecutionResult result = await operationExecuter.ExecuteAsync(
                new OperationRequest(schema.Services, session.Object),
                CancellationToken.None);

            // assert
            Assert.NotNull(result);
            Assert.Null(result.Errors);
            Assert.Collection(((QueryResult)result).Data,
                              item =>
            {
                Assert.Equal("a", item.Key);
                Assert.Equal("hello world", item.Value);
            });
        }
        public async Task ResolveSimpleOneLevelQuery()
        {
            // arrange
            Schema schema = CreateSchema();
            var    dataLoaderDescriptors =
                new DataLoaderDescriptorCollection(schema.DataLoaders);
            var dataLoaderState = new DataLoaderState(
                schema.Services, dataLoaderDescriptors,
                Enumerable.Empty <StateObjectCollection <string> >());
            DocumentNode            query     = Parser.Default.Parse(@"
                {
                    a
                }");
            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().FirstOrDefault();

            // act
            OperationExecuter operationExecuter =
                new OperationExecuter(schema, query, operation);
            IExecutionResult result = await operationExecuter.ExecuteAsync(
                new OperationRequest(schema.Services, dataLoaderState),
                CancellationToken.None);

            // assert
            Assert.NotNull(result);
            Assert.Null(result.Errors);
            Assert.Collection(((QueryResult)result).Data,
                              item =>
            {
                Assert.Equal("a", item.Key);
                Assert.Equal("hello world", item.Value);
            });
        }
Ejemplo n.º 5
0
        public async Task ResolveSimpleOneLevelQuery()
        {
            // arrange
            Schema schema = CreateSchema();

            DocumentNode query = Parser.Default.Parse(@"
                {
                    a
                }");

            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().FirstOrDefault();

            // act
            var operationExecuter =
                new OperationExecuter(schema, query, operation);

            IExecutionResult result = await operationExecuter.ExecuteAsync(
                new OperationRequest(schema.Services, CreateSession()),
                CancellationToken.None);

            // assert
            Assert.NotNull(result);
            Assert.Null(result.Errors);
            Assert.Collection(((QueryResult)result).Data,
                              item =>
            {
                Assert.Equal("a", item.Key);
                Assert.Equal("hello world", item.Value);
            });
        }
Ejemplo n.º 6
0
        public async Task ExecuteMutationSerially()
        {
            // arrange
            int state = 0;

            Schema schema = Schema.Create(
                FileResource.Open("MutationExecutionSchema.graphql"),
                cnf =>
            {
                cnf.BindResolver(() => state).To("Query", "state");
                cnf.BindResolver(() => state).To("CurrentState", "theNumber");
                cnf.BindResolver(ctx => state = ctx.Argument <int>("newNumber"))
                .To("Mutation", "changeTheNumber");
            });

            DocumentNode query = Parser.Default.Parse(
                FileResource.Open("MutationExecutionQuery.graphql"));

            // act
            OperationExecuter operationExecuter = new OperationExecuter();
            QueryResult       result            = await operationExecuter.ExecuteRequestAsync(
                schema, query, null, null, null, CancellationToken.None);

            // assert
            Assert.Null(result.Errors);
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
Ejemplo n.º 7
0
        public async Task ResolveSimpleOneLevelQuery()
        {
            // arrange
            Schema       schema = CreateSchema();
            DocumentNode query  = Parser.Default.Parse(@"
                {
                    a
                }");

            // act
            OperationExecuter operationExecuter = new OperationExecuter();
            QueryResult       result            = await operationExecuter.ExecuteRequestAsync(
                schema, query, null, new Dictionary <string, IValueNode>(),
                null, CancellationToken.None);

            // assert
            Assert.NotNull(result);
            Assert.Null(result.Errors);
            Assert.Collection(result.Data,
                              item =>
            {
                Assert.Equal("a", item.Key);
                Assert.Equal("hello world", item.Value);
            });
        }
Ejemplo n.º 8
0
        public async Task <IExecutionResult> ExecuteAsync(
            QueryRequest queryRequest,
            CancellationToken cancellationToken = default)
        {
            if (queryRequest == null)
            {
                throw new ArgumentNullException(nameof(queryRequest));
            }

            QueryInfo queryInfo = GetOrCreateQuery(queryRequest.Query);

            if (queryInfo.ValidationResult.HasErrors)
            {
                return(new QueryResult(queryInfo.ValidationResult.Errors));
            }

            try
            {
                OperationExecuter operationRequest =
                    GetOrCreateOperationRequest(
                        queryRequest, queryInfo.QueryDocument);

                return(await operationRequest.ExecuteAsync(
                           queryRequest.VariableValues, queryRequest.InitialValue,
                           cancellationToken));
            }
            catch (QueryException ex)
            {
                return(new QueryResult(ex.Errors));
            }
            catch (Exception ex)
            {
                return(new QueryResult(CreateErrorFromException(ex)));
            }
        }
Ejemplo n.º 9
0
        public async Task ExecuteMutationSerially()
        {
            // arrange
            int state = 0;

            Schema schema = Schema.Create(
                FileResource.Open("MutationExecutionSchema.graphql"),
                cnf =>
            {
                cnf.BindResolver(() => state).To("Query", "state");
                cnf.BindResolver(() => state).To("CurrentState", "theNumber");
                cnf.BindResolver(ctx => state = ctx.Argument <int>("newNumber"))
                .To("Mutation", "changeTheNumber");
            });

            DocumentNode query = Parser.Default.Parse(
                FileResource.Open("MutationExecutionQuery.graphql"));
            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().FirstOrDefault();

            // act
            OperationExecuter operationRequest =
                new OperationExecuter(schema, query, operation);
            IExecutionResult result = await operationRequest.ExecuteAsync();

            // assert
            Assert.Null(result.Errors);
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
Ejemplo n.º 10
0
        public async Task ExecuteMutationSerially()
        {
            // arrange
            int state = 0;

            Schema schema = Schema.Create(
                FileResource.Open("MutationExecutionSchema.graphql"),
                cnf =>
            {
                cnf.BindResolver(() => state).To("Query", "state");
                cnf.BindResolver(() => state).To("CurrentState", "theNumber");
                cnf.BindResolver(ctx => state = ctx.Argument <int>("newNumber"))
                .To("Mutation", "changeTheNumber");
            });

            var session = new Mock <ISession>(MockBehavior.Strict);

            session.Setup(t => t.DataLoaders).Returns((IDataLoaderProvider)null);
            session.Setup(t => t.CustomContexts).Returns((ICustomContextProvider)null);

            DocumentNode query = Parser.Default.Parse(
                FileResource.Open("MutationExecutionQuery.graphql"));
            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().FirstOrDefault();

            // act
            OperationExecuter operationExecuter =
                new OperationExecuter(schema, query, operation);
            IExecutionResult result = await operationExecuter.ExecuteAsync(
                new OperationRequest(schema.Services, session.Object),
                CancellationToken.None);

            // assert
            Assert.Null(result.Errors);
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
        public async Task ExecuteMutationSerially()
        {
            // arrange
            int state = 0;

            Schema schema = Schema.Create(
                FileResource.Open("MutationExecutionSchema.graphql"),
                cnf =>
            {
                cnf.BindResolver(() => state).To("Query", "state");
                cnf.BindResolver(() => state).To("CurrentState", "theNumber");
                cnf.BindResolver(ctx => state = ctx.Argument <int>("newNumber"))
                .To("Mutation", "changeTheNumber");
            });
            var dataLoaderDescriptors =
                new DataLoaderDescriptorCollection(schema.DataLoaders);
            var dataLoaderState = new DataLoaderState(
                schema.Services, dataLoaderDescriptors,
                Enumerable.Empty <StateObjectCollection <string> >());

            DocumentNode query = Parser.Default.Parse(
                FileResource.Open("MutationExecutionQuery.graphql"));
            OperationDefinitionNode operation = query.Definitions
                                                .OfType <OperationDefinitionNode>().FirstOrDefault();

            // act
            OperationExecuter operationExecuter =
                new OperationExecuter(schema, query, operation);
            IExecutionResult result = await operationExecuter.ExecuteAsync(
                new OperationRequest(schema.Services, dataLoaderState),
                CancellationToken.None);

            // assert
            Assert.Null(result.Errors);
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }