Beispiel #1
0
        private TypeDefBase CreateTypeDef(Type type, TypeKind typeKind, GraphQLModule module)
        {
            var allAttrs   = GetAllAttributesAndAdjustments(type);
            var nameAttr   = allAttrs.Find <GraphQLNameAttribute>();
            var typeName   = nameAttr?.Name ?? GetGraphQLName(type);
            var moduleName = module.Name;

            switch (typeKind)
            {
            case TypeKind.Enum:
                if (!type.IsEnum)
                {
                    AddError($"Type {type} cannot be registered as Enum GraphQL type, must be enum; module: {moduleName}");
                    return(null);
                }
                return(new EnumTypeDef(type, allAttrs, module));

            case TypeKind.Object:
                return(new ObjectTypeDef(typeName, type, allAttrs, module));

            case TypeKind.Interface:
                return(new InterfaceTypeDef(typeName, type, allAttrs, module));

            case TypeKind.InputObject:
                return(new InputObjectTypeDef(typeName, type, allAttrs, module));

            case TypeKind.Union:
                return(new UnionTypeDef(typeName, type, allAttrs, module));
            }
            // should never happen
            return(null);
        }
        /// <summary>Registers a handler for a directive that is already registered, possibly in another module. </summary>
        /// <param name="module">GraphQL module.</param>
        /// <param name="name">Directive name.</param>
        /// <param name="handler">Handler instance.</param>
        /// <remarks>The purpose of this method is to allow registering handlers separately from the directives themselves.
        /// You can have a directive defined in light, module not dependent on any server-side assemblies, so it can be
        /// used in client-side code. The directive's handler can be registered in server-side assembly, as it likely
        /// needs access to server-side functionality.
        /// </remarks>
        public static void RegisterDirectiveHandler(this GraphQLModule module, string name, IDirectiveHandler handler = null)
        {
            var reg = new DirectiveRegistration()
            {
                Name = name, Handler = handler
            };

            module.RegisteredDirectives.Add(reg);
        }
Beispiel #3
0
        /// <summary>Registers a type of handler for a directive that is already registered, possibly in another module. </summary>
        /// <param name="module">GraphQL module.</param>
        /// <param name="name">Directive name.</param>
        /// <param name="handlerType">Handler type.</param>
        /// <remarks>The purpose of this method is to allow registering handlers separately from the directives themselves.
        /// You can have a directive defined in light-weight module not dependent on any server-side assemblies, so it can be
        /// used in client-side code. The directive's handler can be registered in server-side assembly, as it likely
        /// needs access to server-side functionality.
        /// </remarks>
        public static void RegisterDirectiveHandler(this GraphQLModule module, string name, Type handlerType)
        {
            var dirInfo = new DirectiveHandlerInfo()
            {
                Name = name, Type = handlerType
            };

            module.DirectiveHandlers.Add(dirInfo);
        }
Beispiel #4
0
 public TypeDefBase(string name, TypeKind kind, Type clrType, IList <Attribute> attributes, GraphQLModule module)
 {
     Name           = name;
     Kind           = kind;
     ClrType        = clrType;
     Attributes     = attributes;
     Module         = module;
     TypeRefNull    = new TypeRef(this);
     TypeRefNotNull = new TypeRef(TypeRefNull, TypeKind.NonNull);
     TypeRefs.Add(TypeRefNull);
     TypeRefs.Add(TypeRefNotNull);
 }
        public static void RegisterDirective(this GraphQLModule module, string name, string signatureMethodName,
                                             DirectiveLocation locations, string description = null, IDirectiveHandler handler = null, bool listInSchema = true)
        {
            var method = module.GetType().GetMethod(signatureMethodName,
                                                    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);

            if (method == null)
            {
                throw new ArgumentException($"RegisterDirective, name={name}: method {signatureMethodName} not found in module {module.Name}");
            }
            RegisterDirective(module, name, method, locations, description, handler, listInSchema);
        }
        public static void RegisterDirective(this GraphQLModule module, string name, MethodInfo signature,
                                             DirectiveLocation locations, string description = null, IDirectiveHandler handler = null, bool listInSchema = true)
        {
            if (signature == null)
            {
                throw new ArgumentException("RegisterDirective method: signature parameter may not be null.");
            }
            var reg = new DirectiveRegistration()
            {
                Name         = name, Signature = signature, Locations = locations, Description = description,
                ListInSchema = listInSchema, Handler = handler
            };

            module.RegisteredDirectives.Add(reg);
        }
        public static void RegisterDirective(this GraphQLModule module, string name, Type directiveAttributeType,
                                             DirectiveLocation locations, string description = null, IDirectiveHandler handler = null, bool listInSchema = true)
        {
            if (!typeof(BaseDirectiveAttribute).IsAssignableFrom(directiveAttributeType))
            {
                throw new ArgumentException(
                          $"RegisterDirective method: directive attribute must be subclass of {nameof(directiveAttributeType)}");
            }
            var reg = new DirectiveRegistration()
            {
                Name         = name, AttributeType = directiveAttributeType, Locations = locations, Description = description,
                ListInSchema = listInSchema, Handler = handler
            };

            module.RegisteredDirectives.Add(reg);
        }
Beispiel #8
0
 private void CreateRegisterTypeDef(Type type, GraphQLModule module, TypeKind typeKind)
 {
     try {
         var typeDef = CreateTypeDef(type, typeKind, module);
         if (typeDef == null)
         {
             return;
         }
         var hideAttr = typeDef.Attributes.Find <HiddenAttribute>();
         if (hideAttr != null)
         {
             typeDef.Hidden = true;
         }
         if (typeDef.ClrType != null && typeDef.Kind != TypeKind.Scalar) //schema has no CLR type
         {
             typeDef.Description = _docLoader.GetDocString(typeDef.ClrType, typeDef.ClrType);
         }
         RegisterTypeDef(typeDef);
     } catch (Exception ex) {
         AddError($"FATAL: Failed to register type {type}, error: {ex}. ");
     }
 }
Beispiel #9
0
 public ObjectTypeDef(string name, Type clrType, IList <Attribute> attrs, GraphQLModule module,
                      TypeRole typeRole = TypeRole.Data)
     : base(name, TypeKind.Object, clrType, attrs, module, typeRole)
 {
     _possibleOutTypes = new ObjectTypeDef[] { this };
 }
Beispiel #10
0
 public ScalarTypeDef(Scalar scalar, GraphQLModule module)
     : base(scalar.Name, TypeKind.Scalar, scalar.DefaultClrType, EmptyAttributeList, module)
 {
     Scalar = scalar;
     base.IsDefaultForClrType = Scalar.IsDefaultForClrType;
 }
Beispiel #11
0
 public UnionTypeDef(string name, Type clrType, IList <Attribute> attrs, GraphQLModule module)
     : base(name, TypeKind.Union, clrType, attrs, module)
 {
 }
Beispiel #12
0
 public InputObjectTypeDef(string name, Type clrType, IList <Attribute> attrs, GraphQLModule module)
     : base(name, TypeKind.InputObject, clrType, attrs, module)
 {
 }
Beispiel #13
0
 public ObjectTypeDef(string name, Type clrType, IList <Attribute> attrs, GraphQLModule module, ObjectTypeRole typeRole = ObjectTypeRole.Data)
     : base(name, TypeKind.Object, clrType, attrs, module)
 {
     this.TypeRole = typeRole;
 }
Beispiel #14
0
 public InterfaceTypeDef(string name, Type clrType, IList <Attribute> attrs, GraphQLModule module)
     : base(name, TypeKind.Interface, clrType, attrs, module)
 {
 }
Beispiel #15
0
 public ComplexTypeDef(string name, TypeKind kind, Type clrType, IList <Attribute> attrs, GraphQLModule module)
     : base(name, kind, clrType, attrs, module)
 {
 }
Beispiel #16
0
 public EnumTypeDef(Type type, IList <Attribute> attrs, GraphQLModule module)
     : base(type.Name, TypeKind.Enum, type, attrs, module)
 {
 }
Beispiel #17
0
        } //method

        private void RegisterSpecialObjectTypeIfProvided(Type type, TypeRole typeRole, GraphQLModule module)
        {
            if (type == null)
            {
                return;
            }
            var typeName = $"{module.Name}_{type.Name}";
            var typeDef  = new ObjectTypeDef(typeName, type, GraphQLModelObject.EmptyAttributeList, module, typeRole);

            _model.Types.Add(typeDef);
            _model.TypesByClrType.Add(type, typeDef);
        }
Beispiel #18
0
 public ComplexTypeDef(string name, TypeKind kind, Type clrType, IList <Attribute> attrs, GraphQLModule module,
                       TypeRole typeRole = TypeRole.Data)
     : base(name, kind, clrType, attrs, module)
 {
     this.TypeRole = typeRole;
 }
Beispiel #19
0
 public EnumTypeDef(string name, Type enumType, bool isFlagSet, IList <Attribute> attrs, GraphQLModule module)
     : base(name, TypeKind.Enum, enumType, attrs, module)
 {
     base.ClrType = enumType;
     IsFlagSet    = isFlagSet;
     EnumBaseType = Enum.GetUnderlyingType(enumType);
     NoneValue    = Activator.CreateInstance(enumType);
     ToLong       = ReflectionHelper.GetEnumToLongConverter(enumType);
 }