Exemplo n.º 1
0
        public void CreateValues_ObjectAsDictionary_Object()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: BarInput!) { a }");

            var fooInput = new Dictionary <string, object>();

            fooInput["b"] = "B";

            var barInput = new Dictionary <string, object>();

            barInput["f"] = fooInput;

            var variableValues = new Dictionary <string, object>();

            variableValues.Add("test", barInput);

            var resolver = new VariableValueBuilder(schema, operation);

            // act
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Bar bar = coercedVariableValues.GetVariable <Bar>("test");

            Assert.NotNull(bar.F);
            Assert.Equal(BarEnum.B, bar.F.B);
        }
        public void Variable_InputObjectAsDict_NonNullListItemNull()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: BazInput) { a }");
            var variableValues = new Dictionary <string, object>
            {
                { "test", new Dictionary <string, object>
                  {
                      { "foo", "foo" },
                      { "quox", new List <object>
                        {
                            null
                        } }
                  } }
            };

            // act
            var    resolver = new VariableValueBuilder(schema, operation);
            Action action   = () => resolver.CreateValues(variableValues);

            // assert
            Assert.Throws <QueryException>(action);
        }
        public void CreateValues_ListOfObject_ListOfString()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: [String]) { a }");

            var variableValues = new Dictionary <string, object>
            {
                { "test", new List <object> {
                      "a", "b"
                  } }
            };

            var resolver = new VariableValueBuilder(schema, operation);

            // act
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            var list = coercedVariableValues.GetVariable <string[]>("test");

            Assert.Collection(list,
                              t => Assert.Equal("a", t),
                              t => Assert.Equal("b", t));
        }
Exemplo n.º 4
0
        public void Variable_InputObjectAsClrType_NonNullListItemNull()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: BazInput) { a }");
            var variableValues = new Dictionary <string, object>
            {
                { "test", new Baz
                  {
                      Foo  = "foo",
                      Quox = new string[] { null }
                  } }
            };

            // act
            var resolver = new VariableValueBuilder(schema, operation);
            VariableValueCollection variables = resolver.CreateValues(variableValues);

            // assert
            Baz baz = variables.GetVariable <Baz>("test");

            Assert.Equal("foo", baz.Foo);
            Assert.Collection(baz.Quox, t => Assert.Null(t));
        }
Exemplo n.º 5
0
        public void Backing_Type_Can_Be_Used_As_Variable()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: BarInput!) { a }");

            var variableValues = new Dictionary <string, object>
            {
                { "test", new Bar {
                      F = new Foo {
                          B = BarEnum.A
                      }
                  } }
            };

            var resolver = new VariableValueBuilder(schema, operation);

            // act
            VariableValueCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Bar bar = coercedVariableValues.GetVariable <Bar>("test");

            Assert.NotNull(bar.F);
            Assert.Equal(BarEnum.A, bar.F.B);
        }
        public void CoerceInputObjectWithEnumInDictionaryGraph()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: BarInput!) { a }");

            var fooInput = new Dictionary <string, object>
            {
                { "b", "B" }
            };

            var barInput = new Dictionary <string, object>
            {
                { "f", fooInput }
            };

            var variableValues = new Dictionary <string, object>
            {
                { "test", barInput }
            };

            var resolver = new VariableValueBuilder(schema, operation);

            // act
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Bar bar = coercedVariableValues.GetVariable <Bar>("test");

            Assert.NotNull(bar.F);
            Assert.Equal(BarEnum.B, bar.F.B);
        }
        public void CoerceInputObjectWithEnumAsEnumValueNode()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: BarInput!) { a }");

            ObjectValueNode fooInput = new ObjectValueNode(
                new ObjectFieldNode("b",
                                    new EnumValueNode("B")));

            ObjectValueNode barInput = new ObjectValueNode(
                new ObjectFieldNode("f", fooInput));

            var variableValues = new Dictionary <string, object>();

            variableValues.Add("test", barInput);

            var resolver = new VariableValueBuilder(schema, operation);

            // act
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Bar bar = coercedVariableValues.GetVariable <Bar>("test");

            Assert.NotNull(bar.F);
            Assert.Equal(BarEnum.B, bar.F.B);
        }
Exemplo n.º 8
0
        private VariableCollection CoerceVariables(
            ISchema schema,
            OperationDefinitionNode operation,
            IReadOnlyDictionary <string, object> variableValues)
        {
            var variableBuilder = new VariableValueBuilder(schema, operation);

            return(variableBuilder.CreateValues(variableValues));
        }
Exemplo n.º 9
0
        private IExecutionContext CreateExecutionContext(
            OperationRequest request)
        {
            VariableCollection variables = _variableValueBuilder
                                           .CreateValues(request.VariableValues);

            var executionContext = new ExecutionContext(
                _schema, _queryDocument, _operation, request, variables);

            return(executionContext);
        }
Exemplo n.º 10
0
 public Task InvokeAsync(IQueryContext context)
 {
     if (IsContextValid(context))
     {
         var variableBuilder = new VariableValueBuilder(
             context.Schema, context.Operation.Definition);
         context.Variables = variableBuilder.CreateValues(
             context.Request.VariableValues);
     }
     return(_next(context));
 }
Exemplo n.º 11
0
        private IExecutionContext CreateExecutionContext(
            IReadOnlyDictionary <string, IValueNode> variableValues,
            object initialValue)
        {
            VariableCollection variables = _variableValueBuilder
                                           .CreateValues(variableValues);

            ExecutionContext executionContext = new ExecutionContext(
                _schema, _queryDocument, _operation, variables,
                initialValue, null);

            return(executionContext);
        }
Exemplo n.º 12
0
        private IExecutionContext CreateExecutionContext(
            OperationRequest request,
            CancellationToken cancellationToken)
        {
            VariableCollection variables = _variableValueBuilder
                                           .CreateValues(request.VariableValues);

            var executionContext = new ExecutionContext(
                _schema, _directiveLookup, _queryDocument,
                _operation, request, variables,
                cancellationToken);

            return(executionContext);
        }
        public void QueryWithNonNullVariableAndDefaultWhereValueIsNull()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: String! = \"foo\") { a }");
            var variableValues = new Dictionary <string, object>();

            // act
            var resolver = new VariableValueBuilder(schema, operation);
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Assert.Equal("foo",
                         coercedVariableValues.GetVariable <string>("test"));
        }
        public void QueryWithNonNullVariableAndDefaultWhereValueWasNotProvided()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: String! = \"foo\") { a }");
            var variableValues = new Dictionary <string, object>
            {
                { "test", NullValueNode.Default }
            };

            // act
            var    resolver = new VariableValueBuilder(schema, operation);
            Action action   = () => resolver.CreateValues(variableValues);

            // assert
            Assert.Throws <QueryException>(action);
        }
Exemplo n.º 15
0
        public Task InvokeAsync(IQueryContext context)
        {
            if (!IsContextValid(context))
            {
                context.Result = QueryResult.CreateError(new QueryError(
                                                             "The coerce variables middleware expects the " +
                                                             "query document to be parsed and the operation " +
                                                             "to be resolved."));
                return(Task.CompletedTask);
            }

            var variableBuilder = new VariableValueBuilder(
                context.Schema, context.Operation.Definition);

            context.Variables = variableBuilder.CreateValues(
                context.Request.VariableValues);

            return(_next(context));
        }
        public void QueryWithNullableVariableAndNoDefaultWhereNoValueWasProvided()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: String) { a }");
            var variableValues = new Dictionary <string, object>();

            variableValues.Add("test", NullValueNode.Default);

            var resolver = new VariableValueBuilder(schema, operation);

            // act
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Assert.Null(coercedVariableValues.GetVariable <string>("test"));
        }
        public void Variable_InputObjectAsClrType_NonNullFieldNull()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: BazInput) { a }");
            var variableValues = new Dictionary <string, object>
            {
                { "test", new Baz {
                      Bar = "bar"
                  } }
            };

            // act
            var    resolver = new VariableValueBuilder(schema, operation);
            Action action   = () => resolver.CreateValues(variableValues);

            // assert
            Assert.Throws <QueryException>(action);
        }
        public void QueryWithNonNullVariableAndDefaultWhereValueWasProvided()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: String! = \"foo\") { a }");
            Dictionary <string, IValueNode> variableValues =
                new Dictionary <string, IValueNode>();

            variableValues.Add("test", new StringValueNode(null, "123456", false));

            // act
            VariableValueBuilder resolver =
                new VariableValueBuilder(schema, operation);
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Assert.Equal("123456", coercedVariableValues.GetVariable <string>("test"));
        }
        public void CoerceEnumFromEnumValueNode()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: BarEnum!) { a }");

            var variableValues = new Dictionary <string, object>();

            variableValues.Add("test", new EnumValueNode("A"));

            var resolver = new VariableValueBuilder(schema, operation);

            // act
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Assert.Equal(BarEnum.A,
                         coercedVariableValues.GetVariable <BarEnum>("test"));
        }
Exemplo n.º 20
0
        public void Variable_InputObjectAsClrType_NonNullFieldNull()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: BazInput) { a }");
            var variableValues = new Dictionary <string, object>
            {
                { "test", new Baz {
                      Bar = "bar"
                  } }
            };

            // act
            var resolver = new VariableValueBuilder(schema, operation);
            VariableValueCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Assert.Equal("bar", coercedVariableValues.GetVariable <Baz>("test").Bar);
        }
Exemplo n.º 21
0
        public void Coerce_Variable_Value_Float_To_Int()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: Int!) { a }");
            var variableValues = new Dictionary <string, object>
            {
                {
                    "test",
                    123.123
                }
            };

            // act
            var    resolver = new VariableValueBuilder(schema, operation);
            Action action   = () => resolver.CreateValues(variableValues);

            // assert
            Assert.Throws <QueryException>(action).Errors.MatchSnapshot();
        }
        public void Variable_List_NonNullListItemHasValue()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: [String!]) { a }");
            var variableValues = new Dictionary <string, object>
            {
                { "test", new List <string> {
                      "abc"
                  } }
            };

            // act
            var resolver = new VariableValueBuilder(schema, operation);
            VariableCollection variables =
                resolver.CreateValues(variableValues);

            // assert
            variables.GetVariable <List <string> >("test").MatchSnapshot();
        }
        public Task InvokeAsync(IQueryContext context)
        {
            string operationName = context.Request.OperationName;
            string cacheKey      = CreateKey(operationName, context.QueryKey);

            OperationDefinitionNode node = _queryCache.GetOrCreate(cacheKey,
                                                                   () => QueryDocumentHelper.GetOperation(
                                                                       context.Document, operationName));

            ObjectType rootType         = ResolveRootType(context, node.Operation);
            object     rootValue        = ResolveRootValue(context, rootType);
            var        disposeRootValue = false;

            if (rootValue == null)
            {
                rootValue        = CreateRootValue(context, rootType);
                disposeRootValue = true;
            }

            var variableBuilder = new VariableValueBuilder(
                context.Schema, node);

            context.Operation = new Operation(
                context.Document, node,
                variableBuilder.CreateValues(context.Request.VariableValues),
                rootType, rootValue);

            try
            {
                return(_next(context));
            }
            finally
            {
                if (disposeRootValue && rootValue is IDisposable d)
                {
                    d.Dispose();
                }
            }
        }
Exemplo n.º 24
0
        public void CreateValues_IntValue_Int(int value)
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: Int) { a }");

            var variableValues = new Dictionary <string, object>();

            variableValues.Add("test", 1);

            var resolver = new VariableValueBuilder(schema, operation);

            // act
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            int result = coercedVariableValues.GetVariable <int>("test");

            Assert.Equal(1, result);
        }
Exemplo n.º 25
0
        public void CreateValues_SerializedDecimal_Decimal()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: Decimal) { a }");
            string input = "1.000000E-004";

            var variableValues = new Dictionary <string, object>();

            variableValues.Add("test", input);

            var resolver = new VariableValueBuilder(schema, operation);

            // act
            VariableCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            decimal result = coercedVariableValues.GetVariable <decimal>("test");

            Assert.Equal(0.0001m, result);
        }
Exemplo n.º 26
0
        public void Coerce_Variable_Value_Int_To_Float()
        {
            // arrange
            Schema schema = CreateSchema();
            OperationDefinitionNode operation = CreateQuery(
                "query test($test: Float!) { a }");
            var variableValues = new Dictionary <string, object>
            {
                {
                    "test",
                    123
                }
            };

            // act
            var resolver = new VariableValueBuilder(schema, operation);
            VariableValueCollection coercedVariableValues =
                resolver.CreateValues(variableValues);

            // assert
            Assert.Equal((float)123,
                         coercedVariableValues.GetVariable <float>("test"));
        }