Input Objects

Specification

Input objects are created as instances of InputObjectType.

Creating input object


        [Fact]
        public void Define()
        {
            /* Given */
            /* When */
            _builder.InputObject("ExampleInputObject", out var input)
                .Connections(connect => connect
                    .InputField(input, "a", ScalarType.Boolean));

            var schema = _builder.Build();

            /* Then */
            var inputFields = schema.GetInputFields(input.Name);
            Assert.Single(inputFields,
                fk => fk.Key == "a"
                      && (ScalarType) fk.Value.Type == ScalarType.Boolean);
        }

When using List or NonNull wrappers make sure that the wrapped type passes TypeIs.IsInputType.

Input Coercion


        [Fact]
        public void Input_coercion()
        {
            /* Given */
            _builder.InputObject("ExampleInputObject", out var input)
                .Connections(connect => connect
                    .InputField(input, "a", ScalarType.String)
                    .InputField(input, "b", ScalarType.Int));

            var schema = _builder.Build();

            /* When */
            var literalValue = new Dictionary<string, object>
            {
                ["a"] = "abc",
                ["b"] = 123
            };

            var actual = (Dictionary<string, object>) Values.CoerceValue(
                    schema.GetInputFields, 
                    schema.GetValueConverter,
                    literalValue, 
                    input);

            /* Then */
            foreach (var expectedKv in literalValue)
            {
                Assert.True(actual.ContainsKey(expectedKv.Key));
                Assert.Equal(expectedKv.Value, actual[expectedKv.Key]);
            }
        }