Exemplo n.º 1
0
 public TypeElement()
 {
     EnumValues    = new Models.EnumValue[] {};
     Interfaces    = new TypeElement[] {};
     PossibleTypes = new TypeElement[] {};
     InputFields   = new InputValue[] {};
 }
        /// <summary>
        /// Build INPUT Type to be used by Mutations
        /// </summary>
        /// <param name="schema"></param>
        /// <param name="combinedMapping"></param>
        /// <remarks>
        /// Since Types and Inputs cannot have the same name, camelCase the name to prevent duplicates.
        /// </remarks>
        /// <returns></returns>
        private static List <Models.TypeElement> BuildInputTypes(ISchemaProvider schema, IReadOnlyDictionary <Type, string> combinedMapping)
        {
            var types = new List <Models.TypeElement>();

            foreach (ISchemaType schemaType in schema.GetNonContextTypes().Where(s => s.IsInput))
            {
                if (schemaType.Name.StartsWith("__"))
                {
                    continue;
                }

                var inputValues = new List <Models.InputValue>();
                foreach (Field field in schemaType.GetFields())
                {
                    if (field.Name.StartsWith("__"))
                    {
                        continue;
                    }

                    // Skip any property with special attribute
                    var property = schemaType.ContextType.GetProperty(field.Name);
                    if (property != null && GraphQLIgnoreAttribute.ShouldIgnoreMemberFromInput(property))
                    {
                        continue;
                    }

                    // Skipping custom fields added to schema
                    if (field.Resolve.NodeType == System.Linq.Expressions.ExpressionType.Call)
                    {
                        continue;
                    }

                    // Skipping ENUM type
                    if (field.ReturnTypeClr.GetTypeInfo().IsEnum)
                    {
                        continue;
                    }

                    inputValues.Add(new Models.InputValue
                    {
                        Name        = field.Name,
                        Description = field.Description,
                        Type        = BuildType(schema, field.ReturnTypeClr, field.ReturnTypeClrSingle, combinedMapping, true)
                    });
                }

                var typeElement = new Models.TypeElement
                {
                    Kind        = "INPUT_OBJECT",
                    Name        = schemaType.Name,
                    Description = schemaType.Description,
                    InputFields = inputValues.ToArray()
                };

                types.Add(typeElement);
            }

            return(types);
        }
        private static List <Models.TypeElement> BuildQueryTypes(ISchemaProvider schema, IReadOnlyDictionary <Type, string> combinedMapping)
        {
            var types = new List <Models.TypeElement>();

            foreach (var st in schema.GetNonContextTypes().Where(s => !s.IsInput))
            {
                var typeElement = new Models.TypeElement
                {
                    Kind        = "OBJECT",
                    Name        = st.Name,
                    Description = st.Description
                };

                types.Add(typeElement);
            }

            return(types);
        }
        private static IEnumerable <TypeElement> BuildScalarTypes(ISchemaProvider schema, IReadOnlyDictionary <Type, string> combinedMapping)
        {
            var types = new List <Models.TypeElement>();

            foreach (var customScalar in schema.CustomScalarTypes)
            {
                var typeElement = new Models.TypeElement
                {
                    Kind        = "SCALAR",
                    Name        = customScalar,
                    Description = null,
                };

                types.Add(typeElement);
            }

            return(types);
        }
        private static List <Models.TypeElement> BuildEnumTypes(ISchemaProvider schema, IReadOnlyDictionary <Type, string> combinedMapping)
        {
            var types = new List <Models.TypeElement>();

            foreach (ISchemaType schemaType in schema.GetNonContextTypes().Where(s => s.IsEnum))
            {
                var typeElement = new Models.TypeElement
                {
                    Kind        = "ENUM",
                    Name        = schemaType.Name,
                    Description = schemaType.Description,
                    EnumValues  = new Models.EnumValue[] { }
                };
                if (schemaType.Name.StartsWith("__"))
                {
                    continue;
                }

                var enumTypes = new List <Models.EnumValue>();

                //filter to ENUM type ONLY!
                foreach (Field field in schemaType.GetFields())
                {
                    enumTypes.Add(new Models.EnumValue
                    {
                        Name              = field.Name,
                        Description       = field.Description,
                        IsDeprecated      = false,
                        DeprecationReason = null
                    });
                }

                typeElement.EnumValues = enumTypes.ToArray();
                if (typeElement.EnumValues.Count() > 0)
                {
                    types.Add(typeElement);
                }
            }

            return(types);
        }
        private static Models.TypeElement BuildType(ISchemaProvider schema, Type clrType, string gqlTypeName, IReadOnlyDictionary <Type, string> combinedMapping, bool isInput = false)
        {
            // Is collection of objects?
            var type = new Models.TypeElement();

            if (clrType.IsEnumerableOrArray())
            {
                type.Kind   = "LIST";
                type.Name   = null;
                type.OfType = BuildType(schema, clrType.GetEnumerableOrArrayType(), gqlTypeName, combinedMapping, isInput);
            }
            else if (clrType.Name == "RequiredField`1")
            {
                type.Kind   = "NON_NULL";
                type.Name   = null;
                type.OfType = BuildType(schema, clrType.GetGenericArguments()[0], gqlTypeName, combinedMapping, isInput);
            }
            else if (clrType.GetTypeInfo().IsEnum)
            {
                type.Kind   = "ENUM";
                type.Name   = FindNamedMapping(clrType, combinedMapping, gqlTypeName);
                type.OfType = null;
            }
            else
            {
                type.Kind   = combinedMapping.Any(x => x.Key == clrType) ? "SCALAR" : "OBJECT";
                type.OfType = null;
                if (type.Kind == "OBJECT" && isInput)
                {
                    type.Name = SchemaGenerator.ToCamelCaseStartsLower(FindNamedMapping(clrType, combinedMapping, gqlTypeName));
                }
                else
                {
                    type.Name = FindNamedMapping(clrType, combinedMapping, gqlTypeName);
                }
            }

            return(type);
        }