public ScopedVariableValue Resolve(
            IResolverContext context,
            ScopedVariableNode variable,
            IInputType targetType)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            if (!ScopeNames.Fields.Equals(variable.Scope.Value))
            {
                throw new ArgumentException(
                          FieldScopedVariableResolver_CannotHandleVariable,
                          nameof(variable));
            }

            if (context.ObjectType.Fields.TryGetField(variable.Name.Value, out IObjectField? field))
            {
                object parent = context.Parent <object>();

                IValueNode?valueLiteral = null;

                if (parent is IReadOnlyDictionary <string, object> dict &&
                    dict.TryGetValue(field.Name, out var value))
                {
                    InputFormatter formatter = context.Service <InputFormatter>();

                    if (value is IValueNode v)
                    {
                        valueLiteral = v;
                    }
                    else if (field.Type.IsInputType() && field.Type is IInputType type)
                    {
                        valueLiteral = formatter.FormatValue(value, type, field.Name);
                    }
                }

                return(new ScopedVariableValue
                       (
                           variable.ToVariableName(),
                           targetType.ToTypeNode(),
                           valueLiteral ?? NullValueNode.Default,
                           null
                       ));
            }

            throw ThrowHelper.FieldScopedVariableResolver_InvalidFieldName(
                      variable.Name.Value,
                      context.Selection.SyntaxNode,
                      context.Path);
        }
        public void ParseValue_Should_Throw_When_InvalidType(string typeName)
        {
            // arrange
            var             inputFormatter = new InputFormatter();
            INamedInputType type           = CreateInputType(typeName);

            // act
            // assert
            Assert.Throws <SerializationException>(() => inputFormatter.FormatValue("", type));
        }
        public void ParseValue_Should_Pass_When_NullValue(string typeName)
        {
            // arrange
            var             inputFormatter = new InputFormatter();
            INamedInputType type           = CreateInputType(typeName);

            // act
            // assert
            Assert.Equal(NullValueNode.Default, inputFormatter.FormatValue(null, type));
        }
        public void ParseValue_Should_Pass_When_Value(string typeName)
        {
            // arrange
            var             inputFormatter = new InputFormatter();
            INamedInputType type           = CreateInputType(typeName);

            // act
            IValueNode literal = inputFormatter.FormatValue(_geometry, type);

            // assert
            literal.ToString().MatchSnapshot();
        }
        public ScopedVariableValue Resolve(
            IResolverContext context,
            ScopedVariableNode variable,
            IInputType targetType)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            if (!ScopeNames.ContextData.Equals(variable.Scope.Value))
            {
                throw new ArgumentException(
                          ContextDataScopedVariableResolver_CannotHandleVariable,
                          nameof(variable));
            }

            context.ContextData.TryGetValue(variable.Name.Value, out var data);
            InputFormatter formatter = context.Service <InputFormatter>();

            IValueNode literal = data switch
            {
                IValueNode l => l,
                       null => NullValueNode.Default,
                       _ => formatter.FormatValue(data, targetType, Path.New(variable.Name.Value))
            };

            return(new ScopedVariableValue
                   (
                       variable.ToVariableName(),
                       targetType.ToTypeNode(),
                       literal,
                       null
                   ));
        }
    }