private static Field ProcessFieldOrProperty <TContextType>(MemberInfo prop, Type fieldOrPropType, ParameterExpression param, MappedSchemaProvider <TContextType> schema)
        {
            if (ignoreProps.Contains(prop.Name) || GraphQLIgnoreAttribute.ShouldIgnoreMemberFromQuery(prop))
            {
                return(null);
            }

            // Get Description from ComponentModel.DescriptionAttribute
            string description = "";
            var    d           = (DescriptionAttribute)prop.GetCustomAttribute(typeof(DescriptionAttribute), false);

            if (d != null)
            {
                description = d.Description;
            }

            LambdaExpression le = Expression.Lambda(prop.MemberType == MemberTypes.Property ? Expression.Property(param, prop.Name) : Expression.Field(param, prop.Name), param);
            var f = new Field(SchemaGenerator.ToCamelCaseStartsLower(prop.Name), le, description);
            var t = CacheType <TContextType>(fieldOrPropType, schema);

            if (t != null && t.IsEnum && !f.ReturnTypeClr.IsNullableType())
            {
                f.ReturnTypeNotNullable = true;
            }
            return(f);
        }
Example #2
0
        private static Field ProcessFieldOrProperty(MemberInfo prop, ParameterExpression param, ISchemaProvider schema, bool createEnumTypes, bool createNewComplexTypes, Func <MemberInfo, string> fieldNamer)
        {
            if (ignoreProps.Contains(prop.Name) || GraphQLIgnoreAttribute.ShouldIgnoreMemberFromQuery(prop))
            {
                return(null);
            }

            // Get Description from ComponentModel.DescriptionAttribute
            string description = "";
            var    d           = (DescriptionAttribute)prop.GetCustomAttribute(typeof(DescriptionAttribute), false);

            if (d != null)
            {
                description = d.Description;
            }

            LambdaExpression le = Expression.Lambda(prop.MemberType == MemberTypes.Property ? Expression.Property(param, prop.Name) : Expression.Field(param, prop.Name), param);
            var attributes      = prop.GetCustomAttributes(typeof(GraphQLAuthorizeAttribute), true).Cast <GraphQLAuthorizeAttribute>();
            var requiredClaims  = new RequiredClaims(attributes);
            // get the object type returned (ignoring list etc) so we know the context to find fields etc
            var returnType = le.ReturnType.IsEnumerableOrArray() ? le.ReturnType.GetEnumerableOrArrayType() : le.ReturnType.GetNonNullableType();
            var t          = CacheType(returnType, schema, createEnumTypes, createNewComplexTypes, fieldNamer);
            // see if there is a direct type mapping from the expression return to to something.
            // otherwise build the type info
            var returnTypeInfo = schema.GetCustomTypeMapping(le.ReturnType) ?? new GqlTypeInfo(() => schema.Type(returnType), le.Body.Type);
            var f = new Field(fieldNamer(prop), le, description, returnTypeInfo, requiredClaims);

            return(f);
        }
Example #3
0
        public MutationType(ISchemaProvider schema, string methodName, GqlTypeInfo returnType, object mutationClassInstance, MethodInfo method, string description, RequiredClaims authorizeClaims, bool isAsync)
        {
            Description = description;
            ReturnType  = returnType;
            this.mutationClassInstance = mutationClassInstance;
            this.method     = method;
            Name            = methodName;
            AuthorizeClaims = authorizeClaims;
            this.isAsync    = isAsync;

            argInstanceType = method.GetParameters()
                              .FirstOrDefault(p => p.GetCustomAttribute(typeof(MutationArgumentsAttribute)) != null || p.ParameterType.GetTypeInfo().GetCustomAttribute(typeof(MutationArgumentsAttribute)) != null)?.ParameterType;
            if (argInstanceType != null)
            {
                foreach (var item in argInstanceType.GetProperties())
                {
                    if (GraphQLIgnoreAttribute.ShouldIgnoreMemberFromInput(item))
                    {
                        continue;
                    }
                    argumentTypes.Add(SchemaGenerator.ToCamelCaseStartsLower(item.Name), ArgType.FromProperty(schema, item));
                }
                foreach (var item in argInstanceType.GetFields())
                {
                    if (GraphQLIgnoreAttribute.ShouldIgnoreMemberFromInput(item))
                    {
                        continue;
                    }
                    argumentTypes.Add(SchemaGenerator.ToCamelCaseStartsLower(item.Name), ArgType.FromField(schema, item));
                }
            }
        }
Example #4
0
        /// <summary>
        /// Build INPUT Type to be used by Mutations
        /// </summary>
        /// <param name="schema"></param>
        /// <remarks>
        /// Since Types and Inputs cannot have the same name, camelCase the name to prevent duplicates.
        /// </remarks>
        /// <returns></returns>
        private static List <TypeElement> BuildInputTypes(ISchemaProvider schema)
        {
            var types = new List <TypeElement>();

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

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

                    // Skip any property with special attribute
                    //== JT: When reading PropertyInfo, must use the default casing. Had to convert back to Pascal from Camel case
                    var property = schemaType.ContextType.GetProperty(SchemaGenerator.ToPascalCaseStartsUpper(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.ReturnType.TypeDotnet.GetTypeInfo().IsEnum)
                    {
                        continue;
                    }

                    inputValues.Add(new InputValue
                    {
                        Name        = field.Name,
                        Description = field.Description,
                        Type        = BuildType(schema, field.ReturnType, field.ReturnType.TypeDotnet, true)
                    });
                }

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

                types.Add(typeElement);
            }

            return(types);
        }
        private static Field ProcessFieldOrProperty(MemberInfo prop, Type fieldOrPropType, ParameterExpression param, ISchemaProvider schema, bool createEnumTypes, bool createNewComplexTypes)
        {
            if (ignoreProps.Contains(prop.Name) || GraphQLIgnoreAttribute.ShouldIgnoreMemberFromQuery(prop))
            {
                return(null);
            }

            // Get Description from ComponentModel.DescriptionAttribute
            string description = "";
            var    d           = (DescriptionAttribute)prop.GetCustomAttribute(typeof(DescriptionAttribute), false);

            if (d != null)
            {
                description = d.Description;
            }

            LambdaExpression le = Expression.Lambda(prop.MemberType == MemberTypes.Property ? Expression.Property(param, prop.Name) : Expression.Field(param, prop.Name), param);
            var attributes      = prop.GetCustomAttributes(typeof(GraphQLAuthorizeAttribute), true).Cast <GraphQLAuthorizeAttribute>();
            var requiredClaims  = new RequiredClaims(attributes);
            var returnType      = le.ReturnType.IsEnumerableOrArray() ? le.ReturnType.GetEnumerableOrArrayType() : le.ReturnType;
            var t = CacheType(returnType, schema, createEnumTypes, createNewComplexTypes);
            var f = new Field(SchemaGenerator.ToCamelCaseStartsLower(prop.Name), le, description, null, null, requiredClaims);

            if (t != null && t.IsEnum && !f.ReturnTypeClr.IsNullableType())
            {
                f.ReturnTypeNotNullable = true;
            }
            return(f);
        }
        /// <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 <TypeElement> BuildInputTypes(ISchemaProvider schema, IReadOnlyDictionary <Type, string> combinedMapping)
        {
            var types = new List <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 TypeElement
                {
                    Kind        = "INPUT_OBJECT",
                    Name        = schemaType.Name,
                    Description = schemaType.Description,
                    InputFields = inputValues.ToArray()
                };

                types.Add(typeElement);
            }

            return(types);
        }
Example #7
0
        public MutationType(ISchemaProvider schema, string methodName, GqlTypeInfo returnType, object mutationClassInstance, MethodInfo method, string description, RequiredClaims authorizeClaims, bool isAsync, Func <MemberInfo, string> fieldNamer)
        {
            Description = description;
            ReturnType  = returnType;
            this.mutationClassInstance = mutationClassInstance;
            this.method     = method;
            Name            = methodName;
            AuthorizeClaims = authorizeClaims;
            this.isAsync    = isAsync;

            argInstanceType = method.GetParameters()
                              .FirstOrDefault(p => p.GetCustomAttribute(typeof(MutationArgumentsAttribute)) != null || p.ParameterType.GetTypeInfo().GetCustomAttribute(typeof(MutationArgumentsAttribute)) != null)?.ParameterType;
            if (argInstanceType != null)
            {
                foreach (var item in argInstanceType.GetProperties())
                {
                    if (GraphQLIgnoreAttribute.ShouldIgnoreMemberFromInput(item))
                    {
                        continue;
                    }
                    argumentTypes.Add(fieldNamer(item), ArgType.FromProperty(schema, item));
                }
                foreach (var item in argInstanceType.GetFields())
                {
                    if (GraphQLIgnoreAttribute.ShouldIgnoreMemberFromInput(item))
                    {
                        continue;
                    }
                    argumentTypes.Add(fieldNamer(item), ArgType.FromField(schema, item));
                }
            }
            //== JT: Start Here
            else
            {
                /*== JT
                 * When parameters are the standard System Types like; Int, String, etc or using the EntityGraphQL.Schema.RequiredField,
                 * loop through the parameters and NOT the object's properties
                 * NOTE: some parameters might be Services
                 * EX: public ReturnModel RemoveById(DataContext context, RequiredField<int> id)
                 */
                // !! Forcing a single parameter ONLY. Anything more than that should require a POCO !!
                ParameterInfo item = method.GetParameters()
                                     .FirstOrDefault(p => p.ParameterType.Namespace.StartsWith("EntityGraphQL.Schema") || p.ParameterType.Namespace.StartsWith("System"));

                if (item.ParameterType.Name == "Nullable`1")
                {
                    Type argType = item.ParameterType.GetGenericArguments()[0];
                    argInstanceType = argType;
                }
                else
                {
                    argInstanceType = item.ParameterType;
                }
                //TODO: see if able to use the "fieldNamer()" function
                argumentTypes.Add(SchemaGenerator.ToCamelCaseStartsLower(item.Name), ArgType.FromParameter(schema, item));
            }
            //==
        }
Example #8
0
        public MutationType(string methodName, ISchemaType returnType, object mutationClassInstance, MethodInfo method, string description, RequiredClaims authorizeClaims)
        {
            this.Description           = description;
            this.ReturnType            = returnType;
            this.mutationClassInstance = mutationClassInstance;
            this.method     = method;
            Name            = methodName;
            AuthorizeClaims = authorizeClaims;

            var methodArg = method.GetParameters().ElementAt(1);

            this.argInstanceType = methodArg.ParameterType;
            foreach (var item in argInstanceType.GetProperties())
            {
                if (GraphQLIgnoreAttribute.ShouldIgnoreMemberFromInput(item))
                {
                    continue;
                }
                argumentTypes.Add(SchemaGenerator.ToCamelCaseStartsLower(item.Name), new ArgType
                {
                    Type            = item.PropertyType,
                    TypeNotNullable = GraphQLNotNullAttribute.IsMemberMarkedNotNull(item) || item.PropertyType.GetTypeInfo().IsEnum
                });
            }
            foreach (var item in argInstanceType.GetFields())
            {
                if (GraphQLIgnoreAttribute.ShouldIgnoreMemberFromInput(item))
                {
                    continue;
                }
                argumentTypes.Add(SchemaGenerator.ToCamelCaseStartsLower(item.Name), new ArgType
                {
                    Type            = item.FieldType,
                    TypeNotNullable = GraphQLNotNullAttribute.IsMemberMarkedNotNull(item) || item.FieldType.GetTypeInfo().IsEnum
                });
            }
        }