コード例 #1
0
ファイル: Arguments.cs プロジェクト: qonn/tanka-graphql
        public static IReadOnlyDictionary <string, object> CoerceArgumentValues(
            ISchema schema,
            ObjectType objectType,
            GraphQLFieldSelection field,
            IReadOnlyDictionary <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 argument           = argumentValues.SingleOrDefault(a => a.Name.Value == argumentName);
                coercedValues[argumentName] = CoerceArgumentValue(
                    schema,
                    coercedVariableValues,
                    argumentName,
                    argumentDefinition,
                    argument);
            }

            return(coercedValues);
        }
コード例 #2
0
        public void GetField()
        {
            /* Given */
            var namedTypeName = Schema.Query.Name;

            /* When */
            var field = Schema.GetField(namedTypeName, "name");

            /* Then */
            Assert.NotNull(field);
            Assert.Same(ScalarType.String, field.Type);
        }
コード例 #3
0
ファイル: TypeTracker.cs プロジェクト: pekkah/tanka-graphql
    public TypeTracker(ISchema schema)
    {
        EnterOperationDefinition = node =>
        {
            var root = node.Operation switch
            {
                OperationType.Query => schema.Query,
                OperationType.Mutation => schema.Mutation,
                OperationType.Subscription => schema.Subscription,
                _ => throw new ArgumentOutOfRangeException()
            };

            Types.Push(root);
        };
        LeaveOperationDefinition = node => { Types.TryPop(out _); };

        EnterSelectionSet = node => { ParentTypes.Push(CurrentType); };


        LeaveSelectionSet = node => { ParentTypes.TryPop(out _); };

        EnterFieldSelection = node =>
        {
            if (ParentType is not null)
            {
                var fieldDefinition = schema.GetField(ParentType.Name, node.Name);
                FieldDefinitions.Push(fieldDefinition ?? null);

                if (fieldDefinition?.Type is not null)
                {
                    var fieldTypeDefinition = Ast.UnwrapAndResolveType(schema, fieldDefinition.Type);

                    if (fieldTypeDefinition is not null && TypeIs.IsOutputType(fieldTypeDefinition))
                    {
                        Types.Push(fieldTypeDefinition);
                    }
                    else
                    {
                        Types.Push(null);
                    }
                }
                else
                {
                    Types.Push(null);
                }
            }
コード例 #4
0
        private IRecord GetRecordFromTableRow(IRecordType recordType, object[] row, ISchema schema)
        {
            IRecord record = new Record(recordType);

            foreach (var field in recordType.Fields)
            {
                var column = schema.GetField(field.Name);

                if (column != null && column.Type == field.Type.UnterlyingDotNetType)
                {
                    int fieldPosition = schema.GetFieldPosition(field.Name);

                    IValue value = new TypedValue(field.Type, row[fieldPosition]);
                    record.SetFieldValue(field.Name, value);
                }
            }

            return(record);
        }
コード例 #5
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);
        }
コード例 #6
0
 public static FieldDefinition GetRequiredField(this ISchema schema, string type, string fieldName)
 {
     return(schema.GetField(type, fieldName) ??
            throw new ArgumentOutOfRangeException(nameof(fieldName),
                                                  $"Schema does not contain a field '{type}.{fieldName}'."));
 }