Example #1
0
        /// <summary>
        /// Returns a DIFieldType for the specified method, with the metadata set and Name, Type,
        /// Description, and DeprecationReason properties initialized.
        /// </summary>
        public static DIFieldType?CreateField(MethodInfo method, Func <Type> inferGraphType)
        {
            //get the method name
            string methodName          = method.Name;
            var    methodNameAttribute = method.GetCustomAttribute <NameAttribute>();

            if (methodNameAttribute != null)
            {
                methodName = methodNameAttribute.Name;
            }
            else
            {
                if (methodName.EndsWith("Async") && method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
                {
                    methodName = methodName.Substring(0, methodName.Length - "Async".Length);
                }
            }
            if (methodName == null)
            {
                return(null); //ignore method if set to null
            }
            //ignore method if it does not return a value
            if (method.ReturnType == typeof(void) || method.ReturnType == typeof(Task))
            {
                return(null);
            }

            //determine the graphtype of the field
            var  graphTypeAttribute = method.GetCustomAttribute <GraphTypeAttribute>();
            Type?graphType          = graphTypeAttribute?.Type;

            //infer the graphtype if it is not specified
            if (graphType == null)
            {
                graphType = inferGraphType();
            }
            //load the description
            string?description = method.GetCustomAttribute <DescriptionAttribute>()?.Description;
            //load the deprecation reason
            string?obsoleteDescription = method.GetCustomAttribute <ObsoleteAttribute>()?.Message;

            var fieldType = new DIFieldType {
                Name              = methodName,
                Type              = graphType,
                Description       = description,
                DeprecationReason = obsoleteDescription,
            };

            //load the metadata
            foreach (var metaAttribute in method.GetCustomAttributes <MetadataAttribute>())
            {
                fieldType.WithMetadata(metaAttribute.Key, metaAttribute.Value);
            }

            return(fieldType);
        }
Example #2
0
        /// <summary>
        /// Returns a DIFieldType for the specified property, with the metadata set and Name, Type,
        /// Description, and DeprecationReason properties initialized.
        /// </summary>
        public static DIFieldType?CreateField(PropertyInfo property, Func <Type> inferGraphType)
        {
            //get the field name
            string fieldName          = property.Name;
            var    fieldNameAttribute = property.GetCustomAttribute <NameAttribute>();

            if (fieldNameAttribute != null)
            {
                fieldName = fieldNameAttribute.Name;
            }
            if (fieldName == null)
            {
                return(null); //ignore field if set to null (or Ignore is specified)
            }
            //determine the graphtype of the field
            var  graphTypeAttribute = property.GetCustomAttribute <GraphTypeAttribute>();
            Type?graphType          = graphTypeAttribute?.Type;

            //infer the graphtype if it is not specified
            if (graphType == null)
            {
                graphType = inferGraphType();
            }
            //load the description
            string?description = property.GetCustomAttribute <DescriptionAttribute>()?.Description;
            //load the deprecation reason
            string?obsoleteDescription = property.GetCustomAttribute <ObsoleteAttribute>()?.Message;
            //create the field
            var fieldType = new DIFieldType {
                Name              = fieldName,
                Type              = graphType,
                Description       = description,
                DeprecationReason = obsoleteDescription,
            };

            //set name of property
            fieldType.WithMetadata(ORIGINAL_EXPRESSION_PROPERTY_NAME, property.Name);
            //load the metadata
            foreach (var metaAttribute in property.GetCustomAttributes <MetadataAttribute>())
            {
                fieldType.WithMetadata(metaAttribute.Key, metaAttribute.Value);
            }
            //return the field
            return(fieldType);
        }