示例#1
0
        public void Register(
            ITypeRegistrar typeRegistrar,
            IEnumerable <ITypeReference> typeReferences)
        {
            foreach (ClrTypeReference typeReference in typeReferences.OfType <ClrTypeReference>())
            {
                if (!BaseTypes.IsNonGenericBaseType(typeReference.Type) &&
                    _typeInspector.TryCreate(typeReference.Type, out TypeInfo typeInfo))
                {
                    Type type = typeInfo.ClrType;

                    if (IsTypeSystemObject(type))
                    {
                        ClrTypeReference namedTypeReference = typeReference.With(type);

                        if (!typeRegistrar.IsResolved(namedTypeReference))
                        {
                            typeRegistrar.Register(
                                typeRegistrar.CreateInstance(type),
                                typeReference.Scope,
                                BaseTypes.IsGenericBaseType(type));
                        }
                    }
                    else
                    {
                        TryMapToExistingRegistration(
                            typeRegistrar,
                            typeInfo,
                            typeReference.Context,
                            typeReference.Scope);
                    }
                }
            }
        }
        internal static bool IsNonGenericBaseType(Type type)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return(BaseTypes.IsNonGenericBaseType(type));
        }
示例#3
0
        private RegisteredType InitializeType(
            TypeSystemObjectBase typeSystemObject,
            string?scope,
            bool isInferred)
        {
            try
            {
                var discoveryContext = new TypeDiscoveryContext(
                    typeSystemObject,
                    scope,
                    _serviceFactory.Services,
                    _descriptorContext,
                    _interceptor);

                typeSystemObject.Initialize(discoveryContext);

                var references = new List <ITypeReference>();

                if (!isInferred)
                {
                    references.Add(TypeReference.Create(
                                       typeSystemObject,
                                       scope: scope));
                }

                if (!BaseTypes.IsNonGenericBaseType(typeSystemObject.GetType()))
                {
                    references.Add(TypeReference.Create(
                                       typeSystemObject.GetType(),
                                       SchemaTypeReference.InferTypeContext(typeSystemObject),
                                       scope: scope));
                }

                if (typeSystemObject is IHasTypeIdentity hasTypeIdentity &&
                    hasTypeIdentity.TypeIdentity is { })
                {
                    var reference = TypeReference.Create(
                        hasTypeIdentity.TypeIdentity,
                        SchemaTypeReference.InferTypeContext(typeSystemObject),
                        scope: scope);

                    if (!references.Contains(reference))
                    {
                        references.Add(reference);
                    }
                }

                var registeredType = new RegisteredType(
                    references,
                    typeSystemObject,
                    discoveryContext,
                    CollectDependencies(discoveryContext),
                    isInferred);

                return(registeredType);
            }
示例#4
0
        private RegisteredType InitializeType(
            TypeSystemObjectBase typeSystemObject,
            bool isInferred)
        {
            try
            {
                var initializationContext = new InitializationContext(
                    typeSystemObject,
                    _serviceFactory.Services,
                    _descriptorContext,
                    _contextData,
                    _interceptor);

                typeSystemObject.Initialize(initializationContext);

                var references = new List <ITypeReference>();

                if (!isInferred)
                {
                    references.Add(new SchemaTypeReference(typeSystemObject));
                }

                if (!BaseTypes.IsNonGenericBaseType(typeSystemObject.GetType()))
                {
                    references.Add(new ClrTypeReference(
                                       typeSystemObject.GetType(),
                                       SchemaTypeReference.InferTypeContext(typeSystemObject)));
                }

                if (typeSystemObject is IHasTypeIdentity hasTypeIdentity &&
                    hasTypeIdentity.TypeIdentity is { })
                {
                    var reference = new ClrTypeReference(
                        hasTypeIdentity.TypeIdentity,
                        SchemaTypeReference.InferTypeContext(typeSystemObject));

                    if (!references.Contains(reference))
                    {
                        references.Add(reference);
                    }
                }

                var registeredType = new RegisteredType(
                    references,
                    typeSystemObject,
                    initializationContext,
                    initializationContext.TypeDependencies,
                    isInferred);

                return(registeredType);
            }
        private INamedType CreateAndRegisterType(Type type)
        {
            if (BaseTypes.IsNonGenericBaseType(type))
            {
                throw new SchemaException(new SchemaError(
                                              "You cannot add a type without specifing its " +
                                              "name and attributes."));
            }

            TypeReference typeReference = type.GetOutputType();

            _typeRegistry.RegisterType(typeReference);
            return(_typeRegistry.GetType <INamedType>(typeReference));
        }
        public void RegisterType(TypeReference typeReference)
        {
            if (typeReference == null)
            {
                throw new ArgumentNullException(nameof(typeReference));
            }

            if (!_sealed &&
                typeReference.IsClrTypeReference() &&
                !BaseTypes.IsNonGenericBaseType(typeReference.ClrType))
            {
                RegisterNativeType(typeReference.ClrType);
            }
        }
示例#7
0
        private T CreateAndRegisterType <T>()
            where T : class, INamedType
        {
            if (BaseTypes.IsNonGenericBaseType(typeof(T)))
            {
                throw new SchemaException(new SchemaError(
                                              "You cannot add a type without specifing its " +
                                              "name and attributes."));
            }

            TypeReference typeReference = new TypeReference(typeof(T));

            _typeRegistry.RegisterType(typeReference);
            return(_typeRegistry.GetType <T>(typeReference));
        }
示例#8
0
        private void RegisterExternalResolvers()
        {
            if (_externalResolverTypes.Count == 0)
            {
                return;
            }

            IDescriptorContext descriptorContext =
                DescriptorContext.Create(_services);

            Dictionary <NameString, ObjectType> types =
                _types.Select(t => t.Value.Type)
                .OfType <ObjectType>()
                .ToDictionary(t => t.Name);

            foreach (Type type in _externalResolverTypes)
            {
                GraphQLResolverOfAttribute attribute =
                    type.GetCustomAttribute <GraphQLResolverOfAttribute>();

                if (attribute.TypeNames != null)
                {
                    foreach (string typeName in attribute.TypeNames)
                    {
                        if (types.TryGetValue(typeName,
                                              out ObjectType objectType))
                        {
                            AddResolvers(descriptorContext, objectType, type);
                        }
                    }
                }

                if (attribute.Types != null)
                {
                    foreach (Type sourceType in attribute.Types
                             .Where(t => !BaseTypes.IsNonGenericBaseType(t)))
                    {
                        ObjectType objectType = types.Values
                                                .FirstOrDefault(t => t.GetType() == sourceType);
                        if (objectType != null)
                        {
                            AddResolvers(descriptorContext, objectType, type);
                        }
                    }
                }
            }
        }
示例#9
0
 private void InitializeTypes()
 {
     foreach (ITypeReference typeReference in _unregistered.ToList())
     {
         if (typeReference is IClrTypeReference ctr)
         {
             RegisterClrType(ctr);
         }
         else if (typeReference is ISchemaTypeReference str &&
                  str.Type is TypeSystemObjectBase tso)
         {
             if (BaseTypes.IsNonGenericBaseType(tso.GetType()))
             {
                 RegisterTypeSystemObject(tso, str);
             }
             else
             {
                 RegisterTypeSystemObject(tso);
             }
         }
示例#10
0
        private void AddResolverType(Type type)
        {
            GraphQLResolverOfAttribute attribute =
                type.GetCustomAttribute <GraphQLResolverOfAttribute>(true);

            _resolverTypes.Add(type);

            if (attribute.Types != null)
            {
                foreach (Type schemaType in attribute.Types)
                {
                    if (typeof(ObjectType).IsAssignableFrom(schemaType) &&
                        !BaseTypes.IsNonGenericBaseType(schemaType))
                    {
                        _types.Add(new ClrTypeReference(
                                       schemaType,
                                       SchemaTypeReference.InferTypeContext(schemaType)));
                    }
                }
            }
        }
示例#11
0
        public ISchemaBuilder AddRootType(
            Type type,
            OperationType operation)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (!type.IsClass)
            {
                // TODO : resources
                throw new ArgumentException(
                          "Root type must be a class",
                          nameof(type));
            }

            if (BaseTypes.IsNonGenericBaseType(type))
            {
                // TODO : resources
                throw new ArgumentException(
                          "Non-generic schema types are not allowed.",
                          nameof(type));
            }

            if (BaseTypes.IsSchemaType(type) &&
                !typeof(ObjectType).IsAssignableFrom(type))
            {
                // TODO : resources
                throw new ArgumentException(
                          "must be object type",
                          nameof(type));
            }

            var reference = new ClrTypeReference(type, TypeContext.Output);

            _operations.Add(operation, reference);
            _types.Add(reference);
            return(this);
        }
示例#12
0
        public void RegisterType(TypeReference typeReference)
        {
            if (typeReference == null)
            {
                throw new ArgumentNullException(nameof(typeReference));
            }

            if (!_sealed)
            {
                if (typeReference.IsSchemaTypeReference())
                {
                    TryUpdateNamedType(typeReference.SchemaType.NamedType());
                }
                else if (typeReference.IsClrTypeReference() &&
                         !BaseTypes.IsNonGenericBaseType(typeReference.ClrType))
                {
                    RegisterType(
                        typeReference.ClrType,
                        typeReference.Context);
                }
            }
        }
示例#13
0
        private bool TryNormalizeClrReference(
            IClrTypeReference typeReference,
            out ITypeReference normalized)
        {
            if (!BaseTypes.IsNonGenericBaseType(typeReference.Type) &&
                _typeInspector.TryCreate(typeReference.Type,
                                         out Utilities.TypeInfo typeInfo))
            {
                if (IsTypeSystemObject(typeInfo.ClrType))
                {
                    normalized = new ClrTypeReference(
                        typeInfo.ClrType,
                        SchemaTypeReference.InferTypeContext(typeInfo.ClrType));
                    return(true);
                }
                else
                {
                    for (int i = 0; i < typeInfo.Components.Count; i++)
                    {
                        var n = new ClrTypeReference(
                            typeInfo.Components[i],
                            typeReference.Context);

                        if ((ClrTypes.TryGetValue(
                                 n, out ITypeReference r) ||
                             ClrTypes.TryGetValue(
                                 n.WithoutContext(), out r)))
                        {
                            normalized = r;
                            return(true);
                        }
                    }
                }
            }

            normalized = null;
            return(false);
        }
示例#14
0
        private void TryUpdateNamedType(INamedType namedType)
        {
            if (!_namedTypes.TryGetValue(namedType.Name,
                                         out INamedType namedTypeRef))
            {
                namedTypeRef = namedType;
                _namedTypes[namedTypeRef.Name] = namedTypeRef;
            }

            Type type = namedTypeRef.GetType();

            if (!_clrTypeToSchemaType.ContainsKey(type) &&
                !BaseTypes.IsNonGenericBaseType(type))
            {
                _clrTypeToSchemaType[type] = namedTypeRef.Name;
            }

            if (namedTypeRef is IHasClrType inputType &&
                inputType.ClrType != null)
            {
                AddNativeTypeBinding(inputType.ClrType, namedTypeRef.Name);
            }
        }
示例#15
0
        public ISchemaBuilder AddRootType(
            Type type,
            OperationType operation)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (!type.IsClass)
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_RootType_MustBeClass,
                          nameof(type));
            }

            if (BaseTypes.IsNonGenericBaseType(type))
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_RootType_NonGenericType,
                          nameof(type));
            }

            if (BaseTypes.IsSchemaType(type) &&
                !typeof(ObjectType).IsAssignableFrom(type))
            {
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_RootType_MustBeObjectType,
                          nameof(type));
            }

            var reference = new ClrTypeReference(type, TypeContext.Output);

            _operations.Add(operation, reference);
            _types.Add(reference);
            return(this);
        }
示例#16
0
        private void InitializeTypes()
        {
            foreach (ITypeReference typeReference in _unregistered.ToList())
            {
                if (typeReference is IClrTypeReference ctr)
                {
                    RegisterClrType(ctr);
                }
                else if (typeReference is ISchemaTypeReference str &&
                         str.Type is TypeSystemObjectBase tso)
                {
                    if (BaseTypes.IsNonGenericBaseType(tso.GetType()))
                    {
                        RegisterTypeSystemObject(tso, str);
                    }
                    else
                    {
                        var secondaryRef = new ClrTypeReference(
                            tso.GetType(),
                            SchemaTypeReference.InferTypeContext(tso));

                        RegisterTypeSystemObject(tso, str, secondaryRef);
                    }
                }
示例#17
0
        private bool TryNormalizeClrReference(
            IClrTypeReference typeReference,
            out IClrTypeReference normalized)
        {
            if (!BaseTypes.IsNonGenericBaseType(typeReference.Type) &&
                _typeInspector.TryCreate(typeReference.Type,
                                         out Utilities.TypeInfo typeInfo))
            {
                if (IsTypeSystemObject(typeInfo.ClrType))
                {
                    normalized = new ClrTypeReference(
                        typeInfo.ClrType,
                        SchemaTypeReference.InferTypeContext(typeInfo.ClrType));
                    return(true);
                }
                else
                {
                    normalized = new ClrTypeReference(
                        typeInfo.ClrType,
                        typeReference.Context);

                    if ((_clrTypes.TryGetValue(
                             normalized, out ITypeReference r) ||
                         _clrTypes.TryGetValue(
                             normalized.WithoutContext(), out r)) &&
                        r is IClrTypeReference cr)
                    {
                        normalized = cr;
                        return(true);
                    }
                }
            }

            normalized = null;
            return(false);
        }