コード例 #1
0
        /// <summary>
        /// Returns all of the variable values defined for the operation from the attached <see cref="Inputs"/> object.
        /// </summary>
        public Variables GetVariableValues(ISchema schema, VariableDefinitions variableDefinitions, Inputs inputs, IVariableVisitor visitor = null)
        {
            if ((variableDefinitions?.List?.Count ?? 0) == 0)
            {
                return(Variables.None);
            }

            var variables = new Variables(variableDefinitions.List.Count);

            if (variableDefinitions != null)
            {
                foreach (var variableDef in variableDefinitions.List)
                {
                    // find the IGraphType instance for the variable type
                    var graphType = variableDef.Type.GraphTypeFromType(schema);

                    if (graphType == null)
                    {
                        ReportError(new InvalidVariableError(this, variableDef, variableDef.Name, $"Variable has unknown type '{variableDef.Type.Name()}'"));
                        continue;
                    }

                    // create a new variable object
                    var variable = new Variable(variableDef.Name);

                    // attempt to retrieve the variable value from the inputs
                    if (inputs.TryGetValue(variableDef.Name, out var variableValue))
                    {
                        // parse the variable via ParseValue (for scalars) and ParseDictionary (for objects) as applicable
                        try
                        {
                            variable.Value = GetVariableValue(graphType, variableDef, variableValue, visitor);
                        }
                        catch (ValidationError error)
                        {
                            ReportError(error);
                            continue;
                        }
                    }
                    else if (variableDef.DefaultValue != null)
                    {
                        // if the variable was not specified in the inputs, and a default literal value was specified, use the specified default variable value

                        // parse the variable literal via ParseLiteral (for scalars) and ParseDictionary (for objects) as applicable
                        try
                        {
                            variable.Value = ExecutionHelper.CoerceValue(graphType, variableDef.DefaultValue, variables, null).Value;
                        }
                        catch (Exception ex)
                        {
                            ReportError(new InvalidVariableError(this, variableDef, variableDef.Name, "Error coercing default value.", ex));
                            continue;
                        }
                        variable.IsDefault = true;
                    }
                    else if (graphType is NonNullGraphType)
                    {
                        ReportError(new InvalidVariableError(this, variableDef, variable.Name, "No value provided for a non-null variable."));
                        continue;
                    }

                    // if the variable was not specified and no default was specified, do not set variable.Value

                    // add the variable to the list of parsed variables defined for the operation
                    variables.Add(variable);
                }
            }

            // return the list of parsed variables defined for the operation
            return(variables);
        }