Example #1
0
        private static ISchemaType CacheType(Type propType, ISchemaProvider schema, bool createEnumTypes, bool createNewComplexTypes, Func<MemberInfo, string> fieldNamer)
        {
            if (propType.IsEnumerableOrArray())
            {
                propType = propType.GetEnumerableOrArrayType();
            }

            if (!schema.HasType(propType) && !ignoreTypes.Contains(propType.Name))
            {
                var typeInfo = propType.GetTypeInfo();
                string description = "";
                var d = (DescriptionAttribute)typeInfo.GetCustomAttribute(typeof(DescriptionAttribute), false);
                if (d != null)
                {
                    description = d.Description;
                }

                /*== JT 
                 * Allow user to change the casing from the default camel case
                 * TODO: Commented out due to changes in code */
                //string name = SchemaGenerator.ToCamelCaseStartsLower(typeInfo.Name);
                //var n = (DisplayNameAttribute)typeInfo.GetCustomAttribute(typeof(DisplayNameAttribute), false);
                //if (n != null)
                //    name = n.DisplayName;
                //==

                if (createNewComplexTypes && (typeInfo.IsClass || typeInfo.IsInterface))
                {
                    // add type before we recurse more that may also add the type
                    // dynamcially call generic method
                    // hate this, but want to build the types with the right Genenics so you can extend them later.
                    // this is not the fastest, but only done on schema creation
                    var method = schema.GetType().GetMethod("AddType", new[] { typeof(string), typeof(string) });
                    method = method.MakeGenericMethod(propType);
                    var t = (ISchemaType)method.Invoke(schema, new object[] { propType.Name, description });
                    var attributes = propType.GetTypeInfo().GetCustomAttributes(typeof(GraphQLAuthorizeAttribute), true).Cast<GraphQLAuthorizeAttribute>();
                    t.AuthorizeClaims = new RequiredClaims(attributes);

                    var fields = GetFieldsFromObject(propType, schema, createEnumTypes, fieldNamer);
                    t.AddFields(fields);
                    return t;
                }
                else if (createEnumTypes && typeInfo.IsEnum && !schema.HasType(propType.Name))
                {
                    var t = schema.AddEnum(propType.Name, propType, description);
                    return t;
                }
                else if (createEnumTypes && propType.IsNullableType() && Nullable.GetUnderlyingType(propType).GetTypeInfo().IsEnum && !schema.HasType(Nullable.GetUnderlyingType(propType).Name))
                {
                    Type type = Nullable.GetUnderlyingType(propType);
                    var t = schema.AddEnum(type.Name, type, description);
                    return t;
                }
            }
            else if (schema.HasType(propType.Name))
            {
                return schema.Type(propType.Name);
            }
            return null;
        }
Example #2
0
        private static ISchemaType CacheType(Type propType, ISchemaProvider schema, bool createEnumTypes, bool createNewComplexTypes, Func <MemberInfo, string> fieldNamer)
        {
            if (propType.IsEnumerableOrArray())
            {
                propType = propType.GetEnumerableOrArrayType();
            }

            if (!schema.HasType(propType) && !ignoreTypes.Contains(propType.Name))
            {
                var    typeInfo    = propType.GetTypeInfo();
                string description = "";
                var    d           = (DescriptionAttribute)typeInfo.GetCustomAttribute(typeof(DescriptionAttribute), false);
                if (d != null)
                {
                    description = d.Description;
                }

                if (createNewComplexTypes && (typeInfo.IsClass || typeInfo.IsInterface))
                {
                    // add type before we recurse more that may also add the type
                    // dynamcially call generic method
                    // hate this, but want to build the types with the right Genenics so you can extend them later.
                    // this is not the fastest, but only done on schema creation
                    var method = schema.GetType().GetMethod("AddType", new[] { typeof(string), typeof(string) });
                    method = method.MakeGenericMethod(propType);
                    var t = (ISchemaType)method.Invoke(schema, new object[] { propType.Name, description });

                    var fields = GetFieldsFromObject(propType, schema, createEnumTypes, fieldNamer);
                    t.AddFields(fields);
                    return(t);
                }
                else if (createEnumTypes && typeInfo.IsEnum && !schema.HasType(propType.Name))
                {
                    var t = schema.AddEnum(propType.Name, propType, description);
                    return(t);
                }
                else if (createEnumTypes && propType.IsNullableType() && Nullable.GetUnderlyingType(propType).GetTypeInfo().IsEnum&& !schema.HasType(Nullable.GetUnderlyingType(propType).Name))
                {
                    Type type = Nullable.GetUnderlyingType(propType);
                    var  t    = schema.AddEnum(type.Name, type, description);
                    return(t);
                }
            }
            else if (schema.HasType(propType.Name))
            {
                return(schema.Type(propType.Name));
            }
            return(null);
        }