示例#1
0
        public void test_inputs()
        {
            var innerObject = new InputObjectGraphType();

            innerObject.AddField(new FieldType {
                Name = "test", ResolvedType = new StringGraphType(), Type = typeof(StringGraphType)
            });
            var list     = new ListGraphType(innerObject);
            var inputObj = new InputObjectGraphType();

            inputObj.AddField(new FieldType {
                Name = "list", ResolvedType = list, Type = list.GetType()
            });
            var obj   = new ObjectGraphType();
            var field = new FieldType
            {
                Name         = "hello",
                ResolvedType = new StringGraphType(),
                Type         = typeof(StringGraphType),
                Arguments    = new QueryArguments {
                    new QueryArgument(inputObj)
                    {
                        Name = "input"
                    }
                }
            };

            obj.AddField(field);
            var schema = new Schema
            {
                Query = obj
            };

            schema.Initialize();
        }
        public SchemaWithNotFullSpecifiedResolvedType()
        {
            var stringFilterInputType = new InputObjectGraphType {
                Name = "InputString"
            };

            stringFilterInputType.AddField(new FieldType
            {
                Name         = "eq",
                ResolvedType = new StringGraphType()
            });
            stringFilterInputType.AddField(new FieldType
            {
                Name         = "in",
                ResolvedType = new ListGraphType <StringGraphType>()
            });
            stringFilterInputType.AddField(new FieldType
            {
                Name         = "not",
                ResolvedType = new NonNullGraphType <StringGraphType>()
            });

            Query = new ObjectGraphType();
            Query.Field(
                "test",
                new StringGraphType(),
                arguments: new QueryArguments(new QueryArgument(stringFilterInputType)
            {
                Name = "a"
            }),
                resolve: context => "ok");
        }
示例#3
0
        public void should_throw_an_exception_if_input_object_graph_type_contains_object_graph_type_field()
        {
            var type      = new InputObjectGraphType();
            var exception = Should.Throw <ArgumentException>(() => type.Field <ObjectGraphType>().Name("test"));

            exception.Message.ShouldContain("InputObjectGraphType cannot have fields containing a ObjectGraphType.");
        }
示例#4
0
        public void InputTypeIsDereferenced_InputField(Type referenceType, Type mappedType)
        {
            var inputType = new InputObjectGraphType();

            inputType.Field(referenceType, "field");
            var query = new ObjectGraphType();

            query.Field(typeof(IntGraphType), "test",
                        arguments: new QueryArguments
            {
                new QueryArgument(inputType)
                {
                    Name = "arg"
                }
            });
            var schema = new Schema
            {
                Query = query
            };

            schema.RegisterTypeMapping(typeof(MyClass), typeof(MyClassObjectType));
            schema.RegisterTypeMapping(typeof(MyClass), typeof(MyClassInputType));
            schema.RegisterTypeMapping(typeof(MappedEnum), typeof(MappedEnumGraphType));
            schema.Initialize();
            var inputTypeActual = schema.Query.Fields.Find("test").Arguments.Find("arg").ResolvedType.ShouldBeOfType <InputObjectGraphType>();

            inputTypeActual.ShouldBe(inputType);
            inputTypeActual.Fields.Find("field").Type.ShouldBe(mappedType);
        }
    public void should_throw_an_exception_if_input_object_graph_type_contains_object_graph_type_field()
    {
        var type      = new InputObjectGraphType();
        var exception = Should.Throw <ArgumentOutOfRangeException>(() => type.Field <ObjectGraphType>().Name("test"));

        exception.Message.ShouldContain("Input type 'InputObject' can have fields only of input types: ScalarGraphType, EnumerationGraphType or IInputObjectGraphType.");
    }
 public InputField(InputObjectGraphType oldType, InputObjectGraphType newType, FieldType oldField, FieldType newField)
 {
     this.oldType  = oldType;
     this.newType  = newType;
     this.oldField = oldField;
     this.newField = newField;
 }
        public string PrintInputObject(InputObjectGraphType type)
        {
            var description = PrintDescription(type.Description);
            var fields      = type.Fields.Select(x => "  " + PrintInputValue(x));

            return(description + "input {1} {{{0}{2}{0}}}".ToFormat(Environment.NewLine, type.Name, string.Join(Environment.NewLine, fields)));
        }
        protected virtual InputObjectGraphType ToInputObjectType(GraphQLInputObjectTypeDefinition inputDef)
        {
            var name       = (string)inputDef.Name.Value;
            var typeConfig = Types.For(name);

            AssertKnownType(typeConfig);

            var type = new InputObjectGraphType
            {
                Name        = name,
                Description = typeConfig.Description ?? inputDef.Description?.Value.ToString() ?? inputDef.Comment?.Text.ToString(),
            }.SetAstType(inputDef);

            OverrideDeprecationReason(type, typeConfig.DeprecationReason);

            typeConfig.CopyMetadataTo(type);

            if (inputDef.Fields != null)
            {
                foreach (var f in inputDef.Fields)
                {
                    type.AddField(ToFieldType(type.Name, f));
                }
            }

            return(type);
        }
示例#9
0
        private InputObjectGraphType GetSecondGraphType(ColumnMetadata columnMetadata, TableMetadata metaTable = null)
        {
            string key = $"{columnMetadata.Type.Name}_internal_input";
            InputObjectGraphType objectGraphType = null;

            if (metaTable == null)
            {
                metaTable = _dbMetadata.GetTableMetadatas().FirstOrDefault(x => x.Type.Name == columnMetadata.Type.Name);
            }
            if (!_tableNameLookup.ExistInputGraphType(key))
            {
                //Creacion de instancia
                //var inherateListType = typeof(InputObjectGraphType<>).MakeGenericType(new Type[] { columnMetadata.Type });
                objectGraphType = new InputObjectGraphType();
                //objectGraphType = Activator.CreateInstance(inherateListType);
                objectGraphType.Name = key;
                foreach (var tableColumn in metaTable.Columns)
                {
                    objectGraphType.Field(
                        GraphUtils.ResolveGraphType(tableColumn.Type),
                        tableColumn.ColumnName
                        );
                }
            }
            return(_tableNameLookup.GetOrInsertInputGraphType(key, objectGraphType));
        }
示例#10
0
        public InputObjectType(InputObjectGraphType oldType, InputObjectGraphType newType)
        {
            this.oldType = oldType;
            this.newType = newType;


            oldFields = oldType.Fields;
            newFields = newType.Fields;
        }
        public static void Build(InputObjectGraphType graphType, Type type)
        {
            ProcessType(graphType, type);
            bool hasDataContract = type.ShouldIncludeInGraph();

            ProcessProperties(graphType, GetProperties(hasDataContract, type), true);
            ProcessFields(graphType, GetFields(hasDataContract, type));
            ProcessMethods(graphType, type, type.GetMethods());
        }
示例#12
0
        public ContentDataGraphInputType(IGraphModel model, ISchemaEntity schema)
        {
            var schemaType = schema.TypeName();
            var schemaName = schema.DisplayName();

            Name = $"{schemaType}InputDto";

            foreach (var field in schema.SchemaDef.Fields.Where(x => !x.IsHidden))
            {
                var inputType = model.GetInputGraphType(field);

                if (inputType != null)
                {
                    if (field.RawProperties.IsRequired)
                    {
                        inputType = new NonNullGraphType(inputType);
                    }

                    var fieldName = field.RawProperties.Label.WithFallback(field.Name);

                    var fieldGraphType = new InputObjectGraphType
                    {
                        Name = $"{schemaType}Data{field.Name.ToPascalCase()}InputDto"
                    };

                    var partition = model.ResolvePartition(field.Partitioning);

                    foreach (var partitionItem in partition)
                    {
                        fieldGraphType.AddField(new FieldType
                        {
                            Name         = partitionItem.Key,
                            ResolvedType = inputType,
                            Resolver     = null,
                            Description  = field.RawProperties.Hints
                        });
                    }

                    fieldGraphType.Description = $"The input structure of the {fieldName} of a {schemaName} content type.";

                    var fieldResolver = new FuncFieldResolver <NamedContentData, ContentFieldData>(c => c.Source.GetOrDefault(field.Name));

                    AddField(new FieldType
                    {
                        Name         = field.Name.ToCamelCase(),
                        Resolver     = fieldResolver,
                        ResolvedType = fieldGraphType,
                        Description  = $"The {fieldName} field."
                    });
                }
            }

            Description = $"The structure of a {schemaName} content type.";
        }
示例#13
0
        public bool TryPrimeInput <T>(InputObjectGraphType <T> graphType)
        {
            if (_inputTypeCache.ContainsKey(typeof(T)))
            {
                return(false);
            }

            _inputTypeCache[typeof(T)] = graphType;

            return(true);
        }
示例#14
0
        protected virtual InputObjectGraphType ToInputObjectType(GraphQLInputObjectTypeDefinition inputDef)
        {
            var type = new InputObjectGraphType();

            type.Name = inputDef.Name.Value;
            var fields = inputDef.Fields.Select(ToFieldType);

            fields.Apply(f => type.AddField(f));

            return(type);
        }
示例#15
0
        protected virtual InputObjectGraphType ToInputObjectType(GraphQLInputObjectTypeDefinition inputDef)
        {
            var type = new InputObjectGraphType();

            type.Name = inputDef.Name.Value;

            ApplyDeprecatedDirective(inputDef.Directives, reason =>
            {
                type.DeprecationReason = reason;
            });

            var fields = inputDef.Fields.Select(x => ToFieldType(type.Name, x));

            fields.Apply(f => type.AddField(f));

            return(type);
        }
        public static void AddFieldsFromType <T>(this InputObjectGraphType type, bool omitIdProperty)
        {
            var properties = typeof(T).GetExactProperies();

            foreach (var property in properties)
            {
                var typeName     = property.PropertyType.Name;
                var propertyName = property.Name.ToFirstLower();

                // create IdGraphType field for Id property
                if (propertyName == "id")
                {
                    // skip id property if desired
                    if (omitIdProperty)
                    {
                        continue;
                    }

                    type.Field <IdGraphType>("id", "The global unique id of an object");
                    continue;
                }

                // create primitive fields like ints, strings, floats etc.
                if (property.PropertyType.IsSimpleType())
                {
                    type.CreateFieldFromPrimitiveProperty(property);
                    continue;
                }

                // generate list field for collection / list
                if (property.PropertyType.GetTypeInfo().GetInterface("ICollection") != null && property.PropertyType.IsGenericType())
                {
                    // create an InputType<itemType>
                    var itemType    = property.PropertyType.GenericTypeArguments[0];
                    var graphQLType = typeof(InputType <>);
                    graphQLType = graphQLType.MakeGenericType(itemType);
                    graphQLType = graphQLType.ConvertToVirtualType();

                    // call AddListField like type.AddListField<Order, InputType<Order>>("orders")
                    var method = typeof(ComplexGraphTypeExtensions).GetMethod("AddListField");
                    method = method.MakeGenericMethod(new[] { itemType, graphQLType });
                    method.Invoke(type, new object[] { type, propertyName });
                }
            }
        }
示例#17
0
        protected virtual InputObjectGraphType ToInputObjectType(GraphQLInputObjectTypeDefinition inputDef)
        {
            var typeConfig = Types.For(inputDef.Name.Value);

            var type = new InputObjectGraphType
            {
                Name        = inputDef.Name.Value,
                Description = typeConfig.Description ?? inputDef.Comment?.Text
            };

            CopyMetadata(type, typeConfig);

            var fields = inputDef.Fields.Select(x => ToFieldType(type.Name, x));

            fields.Apply(f => type.AddField(f));

            return(type);
        }
示例#18
0
        public ContentDataInputGraphType(ISchemaEntity schema, string schemaName, string schemaType, IGraphModel model)
        {
            Name = $"{schemaType}DataInputDto";

            foreach (var(field, fieldName, typeName) in schema.SchemaDef.Fields.SafeFields().Where(x => x.Field.IsForApi(true)))
            {
                var resolvedType = model.GetInputGraphType(schema, field, typeName);

                if (resolvedType != null)
                {
                    var displayName = field.DisplayName();

                    var fieldGraphType = new InputObjectGraphType
                    {
                        Name = $"{schemaType}Data{typeName}InputDto"
                    };

                    var partitioning = model.ResolvePartition(field.Partitioning);

                    foreach (var partitionKey in partitioning.AllKeys)
                    {
                        fieldGraphType.AddField(new FieldType
                        {
                            Name         = partitionKey.EscapePartition(),
                            ResolvedType = resolvedType,
                            Resolver     = null,
                            Description  = field.RawProperties.Hints
                        }).WithSourceName(partitionKey);
                    }

                    fieldGraphType.Description = $"The structure of the {displayName} field of the {schemaName} content input type.";

                    AddField(new FieldType
                    {
                        Name         = fieldName,
                        ResolvedType = fieldGraphType,
                        Resolver     = null,
                        Description  = $"The {displayName} field."
                    }).WithSourceName(field.Name);
                }
            }

            Description = $"The structure of the {schemaName} data input type.";
        }
示例#19
0
        public InputObjectGraphType <TModel> BuildInputGraphType(GraphTypeCache cache)
        {
            var graphType = new InputObjectGraphType <TModel>
            {
                Name = GraphTypeName + "Input"
            };

            if (!cache.TryPrimeInput(graphType))
            {
                return((InputObjectGraphType <TModel>)cache.GetOrCreateInputType(typeof(TModel)));
            }

            foreach (var prop in typeof(TModel).GetProperties().Except(_config.PropsToIgnore))
            {
                var fieldConfig = _config.GetFieldConfig(prop);
                fieldConfig.ConfigureInputTypeField(graphType, cache);
            }
            return(graphType);
        }
示例#20
0
        public DataInputGraphType(Builder builder, SchemaInfo schemaInfo)
        {
            // The name is used for equal comparison. Therefore it is important to treat it as readonly.
            Name = schemaInfo.DataInputType;

            foreach (var fieldInfo in schemaInfo.Fields)
            {
                var resolvedType = builder.GetInputGraphType(fieldInfo);

                if (resolvedType != null)
                {
                    var fieldGraphType = new InputObjectGraphType
                    {
                        // The name is used for equal comparison. Therefore it is important to treat it as readonly.
                        Name = fieldInfo.LocalizedInputType
                    };

                    var partitioning = builder.ResolvePartition(((RootField)fieldInfo.Field).Partitioning);

                    foreach (var partitionKey in partitioning.AllKeys)
                    {
                        fieldGraphType.AddField(new FieldType
                        {
                            Name         = partitionKey.EscapePartition(),
                            ResolvedType = resolvedType,
                            Resolver     = null,
                            Description  = fieldInfo.Field.RawProperties.Hints
                        }).WithSourceName(partitionKey);
                    }

                    fieldGraphType.Description = $"The structure of the {fieldInfo.DisplayName} field of the {schemaInfo.DisplayName} content input type.";

                    AddField(new FieldType
                    {
                        Name         = fieldInfo.FieldName,
                        ResolvedType = fieldGraphType,
                        Resolver     = null
                    }).WithSourceName(fieldInfo);
                }
            }

            Description = $"The structure of the {schemaInfo.DisplayName} data input type.";
        }
示例#21
0
        private dynamic GetInternalListInstances(ColumnMetadata columnMetadata)
        {
            var metaTable = _dbMetadata.GetTableMetadatas().FirstOrDefault(x => x.Type.Name == columnMetadata.Type.Name);

            string key             = $"{metaTable.Type.Name.ToLower().ToSnakeCase()}_list_input";
            var    objectGraphType = new InputObjectGraphType
            {
                Name = key
            };
            dynamic listGraphType = null;

            if (!_tableNameLookup.ExistInputListGraphType(key))
            {
                var tableType        = GetSecondGraphType(columnMetadata, metaTable);
                var inherateListType = typeof(ListGraphType <>).MakeGenericType(new Type[] { tableType.GetType() });
                listGraphType = Activator.CreateInstance(inherateListType);
                listGraphType.ResolvedType = tableType;
                // Field<ListGraphType<CityType>>(nameof(State.cities));
            }
            return(_tableNameLookup.GetOrInsertInputListGraphType(key, listGraphType));
        }
示例#22
0
        public void ConfigureInputTypeField(InputObjectGraphType <TModel> graphType, GraphTypeCache cache)
        {
            if (TryGetScalarGraphType(out var graphQlType))
            {
                graphType.Field(
                    type: graphQlType,
                    name: Property.Name
                    );
            }
            else
            {
                var inputType = cache.GetOrCreateInputType(Property.PropertyType);
                inputType = TryWrapNonNull(inputType);

                graphType.AddField(new FieldType
                {
                    ResolvedType = inputType,
                    Type         = Property.PropertyType,
                    Name         = Property.Name
                });
            }
        }
示例#23
0
        private InputObjectGraphType GetInternalInstances(ColumnMetadata columnMetadata)
        {
            var metaTable = _dbMetadata.GetTableMetadatas().FirstOrDefault(x => x.Type.Name == columnMetadata.Type.Name);

            string key = $"{metaTable.Type.Name.ToLower().ToSnakeCase()}_first_internal_input";
            InputObjectGraphType objectGraphType = null;

            if (!_tableNameLookup.ExistInputGraphType(key))
            {
                //var inherateListType = typeof(InputObjectGraphType<>).MakeGenericType(new Type[] { columnMetadata.Type });
                objectGraphType      = new InputObjectGraphType(); // Activator.CreateInstance(inherateListType);
                objectGraphType.Name = key;
                foreach (var tableColumn in metaTable.Columns)
                {
                    objectGraphType.Field(
                        GraphUtils.ResolveGraphType(tableColumn.Type),
                        tableColumn.ColumnName
                        );
                }
            }
            return(_tableNameLookup.GetOrInsertInputGraphType(key, objectGraphType));
        }
示例#24
0
        /// <summary>
        /// Inspects the given type and, in accordance with the rules of this maker, will
        /// generate a complete set of necessary graph types required to support it.
        /// </summary>
        /// <param name="concreteType">The concrete type to incorporate into the schema.</param>
        /// <returns>GraphTypeCreationResult.</returns>
        public virtual GraphTypeCreationResult CreateGraphType(Type concreteType)
        {
            var template = GraphQLProviders.TemplateProvider.ParseType(concreteType, TypeKind.INPUT_OBJECT) as IInputObjectGraphTypeTemplate;

            if (template == null)
            {
                return(null);
            }

            var formatter = _schema.Configuration.DeclarationOptions.GraphNamingFormatter;
            var result    = new GraphTypeCreationResult();

            var fieldSet   = new List <IGraphField>();
            var fieldMaker = new GraphFieldMaker(_schema);

            foreach (var fieldTemplate in ObjectGraphTypeMaker.GatherFieldTemplates(template, _schema))
            {
                var fieldResult = fieldMaker.CreateField(fieldTemplate);
                fieldSet.Add(fieldResult.Field);

                result.MergeDependents(fieldResult);
            }

            var inputObjectType = new InputObjectGraphType(
                formatter.FormatGraphTypeName(template.Name),
                concreteType,
                fieldSet)
            {
                Description = template.Description,
                Publish     = template.Publish,
            };

            result.GraphType    = inputObjectType;
            result.ConcreteType = concreteType;
            return(result);
        }
示例#25
0
 public virtual void VisitInputObject(InputObjectGraphType type)
 {
 }
    public void can_create_custom_directive_for_all_locations_graph_type_first()
    {
        var schema = new Schema();

        schema.ApplyDirective("schema");
        schema.HasAppliedDirectives().ShouldBeTrue();
        schema.GetAppliedDirectives().Count.ShouldBe(1);

        var objectType = new ObjectGraphType();

        objectType.ApplyDirective("type");
        objectType.HasAppliedDirectives().ShouldBeTrue();
        objectType.GetAppliedDirectives().Count.ShouldBe(1);

        var field = objectType.Field <StringGraphType>("test");

        field.ApplyDirective("field");
        field.HasAppliedDirectives().ShouldBeTrue();
        field.GetAppliedDirectives().Count.ShouldBe(1);

        var interfaceType = new InterfaceGraphType();

        interfaceType.ApplyDirective("interface");
        interfaceType.HasAppliedDirectives().ShouldBeTrue();
        interfaceType.GetAppliedDirectives().Count.ShouldBe(1);

        var unionType = new UnionGraphType();

        unionType.ApplyDirective("union");
        unionType.HasAppliedDirectives().ShouldBeTrue();
        unionType.GetAppliedDirectives().Count.ShouldBe(1);

        var arg = new QueryArgument(new StringGraphType());

        arg.ApplyDirective("arg");
        arg.HasAppliedDirectives().ShouldBeTrue();
        arg.GetAppliedDirectives().Count.ShouldBe(1);

        var enumType = new EnumerationGraphType();

        enumType.ApplyDirective("enumType");
        enumType.HasAppliedDirectives().ShouldBeTrue();
        enumType.GetAppliedDirectives().Count.ShouldBe(1);

        var enumValue = new EnumValueDefinition("UNUSED", null);

        enumValue.ApplyDirective("enumValue");
        enumValue.HasAppliedDirectives().ShouldBeTrue();
        enumValue.GetAppliedDirectives().Count.ShouldBe(1);

        var inputType = new InputObjectGraphType();

        inputType.ApplyDirective("inputType");
        inputType.HasAppliedDirectives().ShouldBeTrue();
        inputType.GetAppliedDirectives().Count.ShouldBe(1);

        var input = inputType.Field <StringGraphType>("test");

        input.ApplyDirective("inputField");
        input.HasAppliedDirectives().ShouldBeTrue();
        input.GetAppliedDirectives().Count.ShouldBe(1);

        var scalarType = new BigIntGraphType();

        scalarType.ApplyDirective("scalar");
        scalarType.HasAppliedDirectives().ShouldBeTrue();
        scalarType.GetAppliedDirectives().Count.ShouldBe(1);
    }
示例#27
0
 public void should_not_throw_an_exception_if_input_object_graph_type_doesnt_contains_object_graph_type_field()
 {
     var type      = new InputObjectGraphType();
     var exception = type.Field <ComplexGraphType <object> >().Name("test");
 }