Beispiel #1
0
        private static bool GetIfArgumentValue(
            ISchema schema,
            GraphQLDirective directive,
            Dictionary <string, object> coercedVariableValues,
            GraphQLArgument argument)
        {
            if (directive == null)
            {
                throw new ArgumentNullException(nameof(directive));
            }
            if (coercedVariableValues == null)
            {
                throw new ArgumentNullException(nameof(coercedVariableValues));
            }
            if (argument == null)
            {
                throw new GraphQLError(
                          "Directive is missing argument which is required", directive);
            }

            switch (argument.Value)
            {
            case GraphQLScalarValue scalarValue:
                return((bool)Values.CoerceValue(schema.GetInputFields, scalarValue, ScalarType.NonNullBoolean));

            case GraphQLVariable variable:
                var variableValue = coercedVariableValues[variable.Name.Value];
                return((bool)variableValue);

            default:
                return(false);
            }
        }
Beispiel #2
0
        public static IReadOnlyDictionary <string, object> CoerceVariableValues(
            ISchema schema,
            GraphQLOperationDefinition 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.Value;
                var variableType = Ast.TypeFromAst(schema, variableDefinition.Type);

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

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

                if (!hasValue && defaultValue != null)
                {
                    coercedValues[variableName] = defaultValue;
                }

                if (variableType is NonNull &&
                    (!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.GetInputFields,
                            value,
                            variableType);
                    }
                }
            }

            return(coercedValues);
        }
Beispiel #3
0
        public static object CoerceArgumentValue(
            ISchema schema,
            IReadOnlyDictionary <string, object> coercedVariableValues,
            string argumentName,
            Argument argumentDefinition,
            GraphQLArgument argument)
        {
            var argumentType  = argumentDefinition.Type;
            var defaultValue  = argumentDefinition.DefaultValue;
            var argumentValue = argument?.Value;

            var    hasValue = argumentValue != null;
            object value    = null;

            if (argumentValue is GraphQLVariable variable)
            {
                if (coercedVariableValues == null)
                {
                    hasValue = false;
                }
                else
                {
                    var variableName = variable.Name.Value;
                    hasValue = coercedVariableValues.ContainsKey(variableName);
                    if (hasValue)
                    {
                        value = coercedVariableValues[variableName];
                    }
                }
            }
            else
            {
                value = argumentValue;
            }

            if (argumentType is NonNull && (!hasValue || value == null))
            {
                throw new ValueCoercionException(
                          $"Argument '{argumentName}' is non-null but no value could be coerced",
                          null,
                          argumentType);
            }

            if (hasValue)
            {
                if (value == null)
                {
                    return(null);
                }

                if (argumentValue is GraphQLVariable)
                {
                    return(value);
                }

                var coercedValue = Values.CoerceValue(
                    schema.GetInputFields,
                    value,
                    argumentType);

                return(coercedValue);
            }

            return(defaultValue);
        }
Beispiel #4
0
        public static Dictionary <string, object> CoerceArgumentValues(
            ISchema schema,
            ObjectType objectType,
            GraphQLFieldSelection field,
            Dictionary <string, object> coercedVariableValues)
        {
            var coercedValues = new Dictionary <string, object>();

            var argumentValues      = field.Arguments?.ToList() ?? new List <GraphQLArgument>();
            var fieldName           = field.Name.Value;
            var argumentDefinitions = schema.GetField(objectType.Name, fieldName).Arguments;

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

            foreach (var argumentDefinitionPair in argumentDefinitions)
            {
                var argumentDefinition = argumentDefinitionPair.Value;
                var argumentName       = argumentDefinitionPair.Key;
                var argumentType       = argumentDefinition.Type;
                var defaultValue       = argumentDefinition.DefaultValue;

                var    argument      = argumentValues.SingleOrDefault(a => a.Name.Value == argumentName);
                var    argumentValue = argument?.Value;
                var    hasValue      = argumentValue != null;
                object value         = null;

                if (argumentValue is GraphQLVariable variable)
                {
                    var variableName = variable.Name.Value;
                    hasValue = coercedVariableValues.ContainsKey(variableName);
                    if (hasValue)
                    {
                        value = coercedVariableValues[variableName];
                    }
                }
                else
                {
                    value = argumentValue;
                }

                if (!hasValue)
                {
                    coercedValues[argumentName] = defaultValue;
                }

                if (argumentType is NonNull && (!hasValue || value == null))
                {
                    throw new ValueCoercionException(
                              $"Argument {argumentName} is non-null but no value could be coerced",
                              null,
                              argumentType);
                }

                if (hasValue)
                {
                    if (value == null)
                    {
                        coercedValues[argumentName] = null;
                    }
                    else if (argumentValue is GraphQLVariable)
                    {
                        coercedValues[argumentName] = value;
                    }
                    else
                    {
                        var coercedValue = Values.CoerceValue(
                            schema.GetInputFields,
                            value,
                            argumentType);

                        coercedValues[argumentName] = coercedValue;
                    }
                }
            }

            return(coercedValues);
        }