Example #1
0
        public void Register(TypeSystemObjectBase typeSystemObject, bool isInferred = false)
        {
            RegisteredType registeredType = InitializeType(typeSystemObject, isInferred);

            if (registeredType.References.Count > 0)
            {
                foreach (ITypeReference typeReference in registeredType.References)
                {
                    _registered[typeReference] = registeredType;
                    _unresolved.Remove(typeReference);
                }

                if (typeSystemObject is IHasClrType hasClrType &&
                    hasClrType.ClrType != typeof(object))
                {
                    var clrRef = new ClrTypeReference(
                        hasClrType.ClrType,
                        SchemaTypeReference.InferTypeContext(typeSystemObject));
                    _unresolved.Remove(clrRef);

                    if (!_clrTypeReferences.ContainsKey(clrRef))
                    {
                        _clrTypeReferences.Add(clrRef, registeredType.References[0]);
                    }
                }
            }
        }
Example #2
0
        public void Register(TypeSystemObjectBase typeSystemObject, bool isInferred = false)
        {
            RegisteredType registeredType = InitializeType(typeSystemObject, isInferred);

            if (registeredType.References.Count > 0)
            {
                foreach (ITypeReference typeReference in registeredType.References)
                {
                    _registered[typeReference] = registeredType;
                    _unresolved.Remove(typeReference);
                }

                if (typeSystemObject is IHasClrType hasClrType &&
                    hasClrType.ClrType != typeof(object))
                {
                    var clrRef = new ClrTypeReference(
                        hasClrType.ClrType,
                        SchemaTypeReference.InferTypeContext(typeSystemObject));
                    _unresolved.Remove(clrRef);

                    bool autoBind = true;

                    if (typeSystemObject is ScalarType scalar &&
                        scalar.Bind == BindingBehavior.Explicit)
                    {
                        autoBind = false;
                    }

                    if (autoBind && !_clrTypeReferences.ContainsKey(clrRef))
                    {
                        _clrTypeReferences.Add(clrRef, registeredType.References[0]);
                    }
                }
            }
        }
Example #3
0
        public ISchemaBuilder BindClrType(Type clrType, Type schemaType)
        {
            if (clrType == null)
            {
                throw new ArgumentNullException(nameof(clrType));
            }

            if (schemaType == null)
            {
                throw new ArgumentNullException(nameof(schemaType));
            }

            if (!BaseTypes.IsSchemaType(schemaType))
            {
                // TODO : resources
                throw new ArgumentException(
                          TypeResources.SchemaBuilder_MustBeSchemaType,
                          nameof(schemaType));
            }

            TypeContext context =
                SchemaTypeReference.InferTypeContext(schemaType);

            _clrTypes[new ClrTypeReference(clrType, context)] =
                new ClrTypeReference(schemaType, context);

            return(this);
        }
Example #4
0
        public void Register(
            ITypeRegistrar typeRegistrar,
            IEnumerable <ITypeReference> typeReferences)

        {
            foreach (IClrTypeReference typeReference in typeReferences.OfType <IClrTypeReference>())
            {
                if (!BaseTypes.IsNonGenericBaseType(typeReference.Type) &&
                    _typeInspector.TryCreate(typeReference.Type, out TypeInfo typeInfo))
                {
                    Type type = typeInfo.ClrType;

                    if (IsTypeSystemObject(type))
                    {
                        var namedTypeReference = new ClrTypeReference(
                            type,
                            SchemaTypeReference.InferTypeContext(type));

                        if (!typeRegistrar.IsResolved(namedTypeReference))
                        {
                            typeRegistrar.Register(
                                typeRegistrar.CreateInstance(type),
                                BaseTypes.IsGenericBaseType(type));
                        }
                    }
                    else
                    {
                        TryMapToExistingRegistration(
                            typeRegistrar,
                            typeInfo,
                            typeReference.Context);
                    }
                }
            }
        }
Example #5
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);
            }
Example #6
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);
            }
Example #7
0
        public ISchemaBuilder AddType(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.IsDefined(typeof(GraphQLResolverOfAttribute), true))
            {
                AddResolverType(type);
            }
            else
            {
                _types.Add(new ClrTypeReference(
                               type,
                               SchemaTypeReference.InferTypeContext(type)));
            }

            return(this);
        }
        public static TypeDependency FromSchemaType(
            Type type,
            TypeDependencyKind kind)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (BaseTypes.IsSchemaType(type))
            {
                TypeContext context   = SchemaTypeReference.InferTypeContext(type);
                var         reference = new ClrTypeReference(type, context);
                return(new TypeDependency(reference, kind));
            }

            throw new ArgumentException(
                      TypeResources.TypeDependency_MustBeSchemaType,
                      nameof(type));
        }
Example #9
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)));
                    }
                }
            }
        }
Example #10
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);
        }
Example #11
0
        public void Register(
            TypeSystemObjectBase typeSystemObject,
            string?scope,
            bool isInferred = false)
        {
            RegisteredType registeredType = InitializeType(
                typeSystemObject,
                scope,
                isInferred);

            if (registeredType.References.Count > 0)
            {
                ResolveReferences(registeredType);

                if (typeSystemObject is IHasRuntimeType hasClrType &&
                    hasClrType.RuntimeType != typeof(object))
                {
                    var clrRef = TypeReference.Create(
                        hasClrType.RuntimeType,
                        SchemaTypeReference.InferTypeContext(typeSystemObject),
                        scope: scope);

                    var explicitBind = typeSystemObject is ScalarType scalar &&
                                       scalar.Bind == BindingBehavior.Explicit;

                    if (!explicitBind)
                    {
                        MarkResolved(clrRef);

                        if (!_clrTypeReferences.ContainsKey(clrRef))
                        {
                            _clrTypeReferences.Add(clrRef, registeredType.References[0]);
                        }
                    }
                }
            }
        }
Example #12
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);
                    }
                }
Example #13
0
        public void Register(
            TypeSystemObjectBase typeSystemObject,
            string?scope,
            bool isInferred = false)
        {
            if (typeSystemObject is null)
            {
                throw new ArgumentNullException(nameof(typeSystemObject));
            }

            RegisteredType registeredType = InitializeType(typeSystemObject, scope, isInferred);

            if (registeredType.References.Count > 0)
            {
                ResolveReferences(registeredType);

                if (typeSystemObject is IHasRuntimeType hasRuntimeType &&
                    hasRuntimeType.RuntimeType != typeof(object))
                {
                    ExtendedTypeReference runtimeTypeRef =
                        _context.TypeInspector.GetTypeRef(
                            hasRuntimeType.RuntimeType,
                            SchemaTypeReference.InferTypeContext(typeSystemObject),
                            scope: scope);

                    var explicitBind = typeSystemObject is ScalarType scalar &&
                                       scalar.Bind == BindingBehavior.Explicit;

                    if (!explicitBind)
                    {
                        MarkResolved(runtimeTypeRef);
                        _typeRegistry.TryRegister(runtimeTypeRef, registeredType.References[0]);
                    }
                }
            }
        }
Example #14
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);
        }
Example #15
0
        private RegisteredType InitializeType(
            TypeSystemObjectBase typeSystemObject,
            string?scope,
            bool isInferred)
        {
            try
            {
                var discoveryContext = new TypeDiscoveryContext(
                    typeSystemObject,
                    _typeRegistry,
                    _typeLookup,
                    _context,
                    _interceptor,
                    scope);

                typeSystemObject.Initialize(discoveryContext);

                var references = new List <ITypeReference>();

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

                if (!ExtendedType.Tools.IsNonGenericBaseType(typeSystemObject.GetType()))
                {
                    references.Add(_context.TypeInspector.GetTypeRef(
                                       typeSystemObject.GetType(),
                                       SchemaTypeReference.InferTypeContext(typeSystemObject),
                                       scope));
                }

                if (typeSystemObject is IHasTypeIdentity hasTypeIdentity &&
                    hasTypeIdentity.TypeIdentity is not null)
                {
                    ExtendedTypeReference reference =
                        _context.TypeInspector.GetTypeRef(
                            hasTypeIdentity.TypeIdentity,
                            SchemaTypeReference.InferTypeContext(typeSystemObject),
                            scope);

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

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

                return(registeredType);
            }
            catch (Exception ex)
            {
                throw new SchemaException(
                          SchemaErrorBuilder.New()
                          .SetMessage(ex.Message)
                          .SetException(ex)
                          .SetTypeSystemObject(typeSystemObject)
                          .Build());
            }
        }