Example #1
0
        public void BuildRequest_Empty_QueryRequestBuilderException()
        {
            // arrange
            // act
            Action action = () =>
                            RemoteQueryRequestBuilder.New()
                            .Create();

            // assert
            Assert.Throws <QueryRequestBuilderException>(action)
            .Message.MatchSnapshot();
        }
Example #2
0
        public void SetQuery_Null_ArgumentException()
        {
            // arrange
            // act
            Action action = () =>
                            RemoteQueryRequestBuilder.New()
                            .SetQuery(null)
                            .Create();

            // assert
            Assert.Equal("query",
                         Assert.Throws <ArgumentNullException>(action).ParamName);
        }
Example #3
0
        public void BuildRequest_OnlyQueryIsSet_RequestHasOnlyQuery()
        {
            // arrange
            DocumentNode query = Parser.Default.Parse("{ foo }");

            // act
            IRemoteQueryRequest request =
                RemoteQueryRequestBuilder.New()
                .SetQuery(query)
                .Create();

            // assert
            request.MatchSnapshot();
        }
Example #4
0
        public void BuildRequest_QueryAndOperation_RequestIsCreated()
        {
            // arrange
            DocumentNode query = Parser.Default.Parse("query bar { foo }");

            // act
            IReadOnlyQueryRequest request =
                RemoteQueryRequestBuilder.New()
                .SetQuery(query)
                .SetOperation("bar")
                .Create();

            // assert
            request.MatchSnapshot();
        }
Example #5
0
        public void BuildRequest_QueryAndInitialValue_RequestIsCreated()
        {
            // arrange
            DocumentNode query = Parser.Default.Parse("{ foo }");

            // act
            IReadOnlyQueryRequest request =
                RemoteQueryRequestBuilder.New()
                .SetQuery(query)
                .SetInitialValue(new { a = "123" })
                .Create();

            // assert
            request.MatchSnapshot();
        }
Example #6
0
        public void BuildRequest_QueryAndAddVariables_RequestIsCreated()
        {
            // arrange
            DocumentNode query = Parser.Default.Parse("{ foo }");

            // act
            IReadOnlyQueryRequest request =
                RemoteQueryRequestBuilder.New()
                .SetQuery(query)
                .AddVariableValue("one", "foo")
                .AddVariableValue("two", "bar")
                .Create();

            // assert
            request.MatchSnapshot();
        }
Example #7
0
        private static IRemoteQueryRequest CreateQuery(
            IMiddlewareContext context,
            NameString schemaName,
            IImmutableStack <SelectionPathComponent> path)
        {
            var fieldRewriter = new ExtractFieldQuerySyntaxRewriter(
                context.Schema,
                context.Service <IEnumerable <IQueryDelegationRewriter> >());

            OperationType operationType =
                context.Schema.IsRootType(context.ObjectType)
                    ? context.Operation.Operation
                    : OperationType.Query;

            ExtractedField extractedField = fieldRewriter.ExtractField(
                schemaName, context.Document, context.Operation,
                context.FieldSelection, context.ObjectType);

            IEnumerable <VariableValue> scopedVariables =
                ResolveScopedVariables(
                    context, schemaName, operationType, path);

            IReadOnlyCollection <VariableValue> variableValues =
                CreateVariableValues(
                    context, scopedVariables, extractedField);

            DocumentNode query = RemoteQueryBuilder.New()
                                 .SetOperation(operationType)
                                 .SetSelectionPath(path)
                                 .SetRequestField(extractedField.Field)
                                 .AddVariables(CreateVariableDefs(variableValues))
                                 .AddFragmentDefinitions(extractedField.Fragments)
                                 .Build();

            var requestBuilder = new RemoteQueryRequestBuilder();

            AddVariables(context, schemaName,
                         requestBuilder, query, variableValues);

            requestBuilder.SetQuery(query);
            requestBuilder.AddProperty(
                WellKnownProperties.IsAutoGenerated,
                true);

            return(requestBuilder.Create());
        }
Example #8
0
        public void BuildRequest_QueryAndServices_RequestIsCreated()
        {
            // arrange
            DocumentNode query   = Parser.Default.Parse("{ foo }");
            var          service = new { a = "123" };

            // act
            IReadOnlyQueryRequest request =
                RemoteQueryRequestBuilder.New()
                .SetQuery(query)
                .SetServices(new DictionaryServiceProvider(
                                 service.GetType(), service))
                .Create();

            // assert
            request.MatchSnapshot();
        }
Example #9
0
        public void BuildRequest_NoOperation_QueryRequestBuilderException()
        {
            // arrange
            DocumentNode query = Parser.Default.Parse(
                "query baz { foo } query qux { foo }");

            // act
            Action action = () =>
                            RemoteQueryRequestBuilder.New()
                            .SetQuery(query)
                            .Create();

            // assert
            Assert.Equal(
                "Specify an operation name in order to create " +
                "a query request that contains multiple operations.",
                Assert.Throws <QueryRequestBuilderException>(action).Message);
        }
Example #10
0
        public void BuildRequest_QueryAndResetProperties_RequestIsCreated()
        {
            // arrange
            DocumentNode query = Parser.Default.Parse("{ foo }");

            // act
            IReadOnlyQueryRequest request =
                RemoteQueryRequestBuilder.New()
                .SetQuery(query)
                .AddProperty("one", "foo")
                .AddProperty("two", "bar")
                .SetProperties(null)
                .Create();

            // assert
            // no property should be in the request
            request.MatchSnapshot();
        }
Example #11
0
        public void BuildRequest_QueryAndSetVariables_RequestIsCreated()
        {
            // arrange
            DocumentNode query = Parser.Default.Parse("{ foo }");

            // act
            IReadOnlyQueryRequest request =
                RemoteQueryRequestBuilder.New()
                .SetQuery(query)
                .AddVariableValue("one", "foo")
                .AddVariableValue("two", "bar")
                .SetVariableValues(new Dictionary <string, object>
            {
                { "three", "baz" }
            })
                .Create();

            // assert
            // only three should be in the request
            request.MatchSnapshot();
        }
Example #12
0
        public void BuildRequest_SetAll_RequestIsCreated()
        {
            // arrange
            DocumentNode query   = Parser.Default.Parse("query bar { foo }");
            var          service = new { a = "123" };

            // act
            IReadOnlyQueryRequest request =
                RemoteQueryRequestBuilder.New()
                .SetQuery(query)
                .SetOperation("bar")
                .SetInitialValue(new { a = "456" })
                .AddProperty("one", "foo")
                .AddVariableValue("two", "bar")
                .SetServices(new DictionaryServiceProvider(
                                 service.GetType(), service))
                .Create();

            // assert
            request.MatchSnapshot();
        }