Exemple #1
0
        private void BuildInputObjectFields(InputObjectTypeDef inpTypeDef)
        {
            var members = inpTypeDef.ClrType.GetFieldsProps();

            foreach (var member in members)
            {
                var attrs   = GetAllAttributesAndAdjustments(member);
                var mtype   = member.GetMemberReturnType();
                var typeRef = GetMemberGraphQLTypeRef(mtype, member, $"Field {inpTypeDef.Name}.{member.Name}");
                if (typeRef == null)
                {
                    return; // error found, it is already logged
                }
                if (typeRef.IsList && !typeRef.TypeDef.IsEnumFlagArray())
                {
                    // list members must be IList<T> or T[] - this is important, lists are instantiated as arrays when deserializing
                    if (!mtype.IsArray && !mtype.IsInterface)
                    {
                        AddError($"Input type member {inpTypeDef.Name}.{member.Name}: list must be either array or IList<T>.");
                        continue;
                    }
                }
                switch (typeRef.TypeDef.Kind)
                {
                case TypeKind.Scalar:
                case TypeKind.Enum:
                case TypeKind.InputObject:
                    break;

                default:
                    AddError($"Input type member {inpTypeDef.Name}.{member.Name}: type {mtype} is not scalar or input type.");
                    continue;
                }
                var inpFldDef = new InputValueDef()
                {
                    Name = member.Name.FirstLower(), TypeRef = typeRef, Attributes = attrs,
                    InputObjectClrMember = member,
                    Description          = _docLoader.GetDocString(member, member.DeclaringType)
                };
                inpFldDef.Directives = BuildDirectivesFromAttributes(member, DirectiveLocation.InputFieldDefinition);
                inpTypeDef.Fields.Add(inpFldDef);
            } //foreach
        }
Exemple #2
0
        // used for interface and Object types
        private void BuildObjectTypeFields(ComplexTypeDef typeDef)
        {
            var objTypeDef = typeDef as ObjectTypeDef;
            var clrType    = typeDef.ClrType;
            var members    = clrType.GetFieldsPropsMethods(withMethods: true);

            foreach (var member in members)
            {
                var attrs      = GetAllAttributes(member);
                var ignoreAttr = attrs.Find <IgnoreAttribute>();
                if (ignoreAttr != null)
                {
                    continue;
                }
                var mtype   = member.GetMemberReturnType();
                var typeRef = GetTypeRef(mtype, member, $"Field {clrType.Name}.{member.Name}");
                if (typeRef == null)
                {
                    continue; //error should be logged already
                }
                var name  = GetGraphQLName(member);
                var descr = _docLoader.GetDocString(member, clrType);
                var fld   = new FieldDef(typeDef, name, typeRef)
                {
                    ClrMember = member, Description = descr, Attributes = attrs
                };
                fld.Directives = BuildDirectivesFromAttributes(member, DirectiveLocation.FieldDefinition, fld);
                if (attrs.Find <HiddenAttribute>() != null)
                {
                    fld.Flags |= FieldFlags.Hidden;
                }
                typeDef.Fields.Add(fld);
                if (member is MethodInfo method)
                {
                    BuildFieldArguments(fld, method);
                }
            }
        }