コード例 #1
0
        public void IsInputType(INamedType type)
        {
            /* Given */
            /* When */
            var isInput          = TypeIs.IsInputType(type);
            var isInputAsNonNull = TypeIs.IsInputType(
                new NonNull(type));
            var isInputAsList = TypeIs.IsInputType(
                new List(type));

            /* Then */
            Assert.True(isInput);
            Assert.True(isInputAsNonNull);
            Assert.True(isInputAsList);
        }
コード例 #2
0
        public InputObjectField(
            IType type,
            string description  = null,
            object defaultValue = null,
            IEnumerable <DirectiveInstance> directives = null)
        {
            if (!TypeIs.IsInputType(type))
            {
                throw new ArgumentOutOfRangeException(
                          $" Type '{type}' is not valid input type");
            }

            Type         = type;
            Description  = description ?? string.Empty;
            DefaultValue = defaultValue;
            _directives  = new DirectiveList(directives);
        }
コード例 #3
0
    public static IReadOnlyDictionary <string, object?> CoerceVariableValues(
        ISchema schema,
        OperationDefinition operation,
        Dictionary <string, object> variableValues)
    {
        var coercedValues       = new Dictionary <string, object?>();
        var variableDefinitions = operation.VariableDefinitions;

        if (variableDefinitions == null)
        {
            return(coercedValues);
        }

        foreach (var variableDefinition in variableDefinitions)
        {
            var variableName = variableDefinition.Variable.Name;
            var variableType = variableDefinition.Type;

            //  should be assert?
            if (!TypeIs.IsInputType(schema, variableType))
            {
                throw new VariableException("Variable is not of input type", variableName, variableType);
            }

            var defaultValue = variableDefinition.DefaultValue?.Value;
            var hasValue     = variableValues.ContainsKey(variableName);
            var value        = hasValue ? variableValues[variableName] : null;

            if (!hasValue && defaultValue != null)
            {
                coercedValues[variableName] = Values.CoerceValue(
                    schema,
                    defaultValue,
                    variableType);
                ;
            }

            if (variableType is NonNullType &&
                (!hasValue || value == null))
            {
                throw new ValueCoercionException(
                          $"Variable {variableName} type is non-nullable but value is null or not set",
                          value,
                          variableType);
            }

            if (hasValue)
            {
                if (value == null)
                {
                    coercedValues[variableName] = null;
                }
                else
                {
                    coercedValues[variableName] = Values.CoerceValue(
                        schema,
                        value,
                        variableType);
                }
            }
        }

        return(coercedValues);
    }