コード例 #1
0
        private static bool IsComplexType(ExtendedTypeReference unresolvedType)
        {
            bool isComplexType =
                unresolvedType.Type.Type.IsClass &&
                IsPublic(unresolvedType) &&
                unresolvedType.Type.Type != typeof(string);

            if (!isComplexType && unresolvedType.Type.IsGeneric)
            {
                Type typeDefinition = unresolvedType.Type.Definition;
                return(typeDefinition == _keyValuePair);
            }

            return(isComplexType);
        }
コード例 #2
0
        public static bool TryInferSchemaType(
            ITypeInspector typeInspector,
            ExtendedTypeReference unresolvedType,
            out ExtendedTypeReference schemaType)
        {
            if (IsObjectTypeExtension(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(ObjectTypeExtension <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsUnionType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(UnionType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsInterfaceType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(InterfaceType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsObjectType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(ObjectType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsInputObjectType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(InputObjectType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsEnumType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(EnumType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else
            {
                schemaType = null;
            }

            return(schemaType != null);
        }
コード例 #3
0
        public void InferEnumType(TypeContext context)
        {
            // arrange
            ExtendedTypeReference typeReference = TypeReference.Create(TypeOf <Foo>(), context);

            // act
            var success = SchemaTypeResolver.TryInferSchemaType(
                _typeInspector,
                typeReference,
                out ExtendedTypeReference schemaType);

            // assert
            Assert.True(success);
            Assert.Equal(TypeContext.None, schemaType.Context);
            Assert.Equal(typeof(EnumType <Foo>), schemaType.Type.Source);
        }
コード例 #4
0
        public void InferInputObjectType()
        {
            // arrange
            ExtendedTypeReference typeReference = TypeReference.Create(TypeOf <Bar>(), TypeContext.Input);

            // act
            var success = SchemaTypeResolver.TryInferSchemaType(
                _typeInspector,
                typeReference,
                out ExtendedTypeReference schemaType);

            // assert
            Assert.True(success);
            Assert.Equal(TypeContext.Input, schemaType.Context);
            Assert.Equal(typeof(InputObjectType <Bar>), schemaType.Type.Source);
        }
コード例 #5
0
            private static bool IsComplexTypeInternal(
                ExtendedTypeReference unresolvedType,
                bool isPublic)
            {
                var isComplexType =
                    unresolvedType.Type.Type.IsClass &&
                    isPublic &&
                    unresolvedType.Type.Type != typeof(string);

                if (!isComplexType && unresolvedType.Type.IsGeneric)
                {
                    Type?typeDefinition = unresolvedType.Type.Definition;
                    return(typeDefinition == _keyValuePair);
                }

                return(isComplexType);
            }
コード例 #6
0
        public void TryRegister(ExtendedTypeReference runtimeTypeRef, ITypeReference typeRef)
        {
            if (runtimeTypeRef is null)
            {
                throw new ArgumentNullException(nameof(runtimeTypeRef));
            }

            if (typeRef is null)
            {
                throw new ArgumentNullException(nameof(typeRef));
            }

            if (!_runtimeTypeRefs.ContainsKey(runtimeTypeRef))
            {
                _runtimeTypeRefs.Add(runtimeTypeRef, typeRef);
            }
        }
コード例 #7
0
        private ObjectFilterOperationDescriptor CreateOperation(
            FilterOperationKind operationKind)
        {
            var operation = new FilterOperation(
                _type,
                operationKind,
                Definition.Property);

            ExtendedTypeReference typeRef = Context.TypeInspector.GetTypeRef(
                typeof(FilterInputType <>).MakeGenericType(_type),
                Definition.Type.Context);

            return(ObjectFilterOperationDescriptor.New(
                       Context,
                       this,
                       CreateFieldName(operationKind),
                       typeRef,
                       operation));
        }
コード例 #8
0
        public static bool TryInferSchemaTypeKind(
            ExtendedTypeReference unresolvedType,
            out TypeKind kind)
        {
            if (IsObjectTypeExtension(unresolvedType))
            {
                kind = TypeKind.Object;
                return(true);
            }

            if (IsUnionType(unresolvedType))
            {
                kind = TypeKind.Union;
                return(true);
            }

            if (IsInterfaceType(unresolvedType))
            {
                kind = TypeKind.Interface;
                return(true);
            }

            if (IsObjectType(unresolvedType))
            {
                kind = TypeKind.Object;
                return(true);
            }

            if (IsInputObjectType(unresolvedType))
            {
                kind = TypeKind.InputObject;
                return(true);
            }

            if (IsEnumType(unresolvedType))
            {
                kind = TypeKind.Enum;
                return(true);
            }

            kind = default;
            return(false);
        }
コード例 #9
0
ファイル: TypeLookup.cs プロジェクト: jacknugent/hotchocolate
        private bool TryNormalizeExtendedTypeReference(
            ExtendedTypeReference typeRef,
            [NotNullWhen(true)] out ITypeReference?namedTypeRef)
        {
            if (typeRef is null)
            {
                throw new ArgumentNullException(nameof(typeRef));
            }

            // if the typeRef refers to a schema type base class we skip since such a type is not
            // resolvable.
            if (typeRef.Type.Type.IsNonGenericSchemaType() ||
                !_typeInspector.TryCreateTypeInfo(typeRef.Type, out ITypeInfo? typeInfo))
            {
                namedTypeRef = null;
                return(false);
            }

            // if we have a concrete schema type we will extract the named type component of
            // the type and rewrite the type reference.
            if (typeRef.Type.IsSchemaType)
            {
                namedTypeRef = typeRef.With(_typeInspector.GetType(typeInfo.NamedType));
                return(true);
            }

            // we check each component layer since there could be a binding on a list type,
            // eg list<byte> to ByteArray.
            for (var i = 0; i < typeInfo.Components.Count; i++)
            {
                IExtendedType         componentType = typeInfo.Components[i].Type;
                ExtendedTypeReference componentRef  = typeRef.WithType(componentType);
                if (_typeRegistry.TryGetTypeRef(componentRef, out namedTypeRef) ||
                    _typeRegistry.TryGetTypeRef(componentRef.WithContext(), out namedTypeRef))
                {
                    return(true);
                }
            }

            namedTypeRef = null;
            return(false);
        }
コード例 #10
0
        /// <inheritdoc />
        public ISortConventionDescriptor ConfigureEnum <TSortEnumType>(
            ConfigureSortEnumType configure)
            where TSortEnumType : SortEnumType
        {
            ExtendedTypeReference typeReference =
                Context.TypeInspector.GetTypeRef(
                    typeof(TSortEnumType),
                    TypeContext.None,
                    Definition.Scope);

            if (!Definition.EnumConfigurations.TryGetValue(
                    typeReference,
                    out List <ConfigureSortEnumType>?configurations))
            {
                configurations = new List <ConfigureSortEnumType>();
                Definition.EnumConfigurations.Add(typeReference, configurations);
            }

            configurations.Add(configure);
            return(this);
        }
コード例 #11
0
        public void Register(
            ITypeRegistrar typeRegistrar,
            IEnumerable <ITypeReference> typeReferences)
        {
            foreach (ExtendedTypeReference typeReference in
                     typeReferences.OfType <ExtendedTypeReference>())
            {
                if (_typeInspector.TryCreateTypeInfo(typeReference.Type, out ITypeInfo? typeInfo) &&
                    !ExtendedType.Tools.IsNonGenericBaseType(typeInfo.NamedType))
                {
                    if (typeInfo.NamedType == typeof(IExecutable))
                    {
                        throw ThrowHelper.NonGenericExecutableNotAllowed();
                    }

                    Type namedType = typeInfo.NamedType;
                    if (IsTypeSystemObject(namedType))
                    {
                        IExtendedType         extendedType       = _typeInspector.GetType(namedType);
                        ExtendedTypeReference namedTypeReference = typeReference.With(extendedType);

                        if (!typeRegistrar.IsResolved(namedTypeReference))
                        {
                            typeRegistrar.Register(
                                typeRegistrar.CreateInstance(namedType),
                                typeReference.Scope,
                                ExtendedType.Tools.IsGenericBaseType(namedType));
                        }
                    }
                    else
                    {
                        TryMapToExistingRegistration(
                            typeRegistrar,
                            typeInfo,
                            typeReference.Context,
                            typeReference.Scope);
                    }
                }
            }
        }
コード例 #12
0
    private static void CreateNodeField(
        ITypeInspector typeInspector,
        IIdSerializer serializer,
        IList <ObjectFieldDefinition> fields,
        int index)
    {
        ExtendedTypeReference node = typeInspector.GetTypeRef(typeof(NodeType));
        ExtendedTypeReference id   = typeInspector.GetTypeRef(typeof(NonNullType <IdType>));

        var field = new ObjectFieldDefinition(
            Node,
            Relay_NodeField_Description,
            node,
            ResolveNodeAsync)
        {
            Arguments = { new(Id, Relay_NodeField_Id_Description, id) }
        };

        fields.Insert(index, field);

        ValueTask <object?> ResolveNodeAsync(IResolverContext ctx)
        => ResolveSingleNode(ctx, serializer, Id);
    }
コード例 #13
0
        public void Register(
            ITypeRegistrar typeRegistrar,
            IEnumerable <ITypeReference> typeReferences)
        {
            foreach (SyntaxTypeReference typeReference in
                     typeReferences.OfType <SyntaxTypeReference>())
            {
                if (Scalars.TryGetScalar(
                        typeReference.Type.NamedType().Name.Value,
                        out Type? scalarType))
                {
                    ExtendedTypeReference namedTypeReference =
                        _typeInspector.GetTypeRef(scalarType);

                    if (!typeRegistrar.IsResolved(namedTypeReference))
                    {
                        typeRegistrar.Register(
                            typeRegistrar.CreateInstance(namedTypeReference.Type.Type),
                            typeReference.Scope);
                    }
                }
            }
        }
コード例 #14
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]);
                    }
                }
            }
        }
コード例 #15
0
            protected override void Configure(IObjectTypeDescriptor descriptor)
            {
                descriptor.Name("Query")
                .Field("words")
                .Type <ListType <ObjectType <Word> > >()
                .Resolver(
                    new Word[] { new Word {
                                     Value = "Hello"
                                 }, new Word {
                                     Value = "World"
                                 } })
                .Extend()
                .OnBeforeCreate((c, d) =>
                {
                    ExtendedTypeReference reference =
                        c.TypeInspector.GetTypeRef(typeof(Word), TypeContext.Output);

                    ILazyTypeConfiguration lazyConfiguration =
                        LazyTypeConfigurationBuilder
                        .New <ObjectFieldDefinition>()
                        .Definition(d)
                        .Configure((context, definition) =>
                    {
                        ObjectType type = context.GetType <ObjectType>(reference);
                        if (!type.IsCompleted)
                        {
                            throw new Exception("Order should not matter");
                        }
                    })
                        .On(ApplyConfigurationOn.Completion)
                        .DependsOn(reference, true)
                        .Build();

                    d.Configurations.Add(lazyConfiguration);
                });
            }
コード例 #16
0
 private static bool IsPublic(ExtendedTypeReference unresolvedType)
 {
     return(unresolvedType.Type.Type.IsPublic ||
            unresolvedType.Type.Type.IsNestedPublic);
 }
コード例 #17
0
 private static bool IsEnumType(ExtendedTypeReference unresolvedType)
 {
     return((unresolvedType.Type.Type.IsEnum ||
             unresolvedType.Type.Type.IsDefined(typeof(EnumTypeAttribute), true)) &&
            IsPublic(unresolvedType));
 }
コード例 #18
0
 private static bool IsUnionType(ExtendedTypeReference unresolvedType)
 {
     return(unresolvedType.Type.Type.IsDefined(typeof(UnionTypeAttribute), true) &&
            (unresolvedType.Context == TypeContext.Output ||
             unresolvedType.Context == TypeContext.None));
 }
コード例 #19
0
 private static bool IsObjectTypeExtension(ExtendedTypeReference unresolvedType) =>
 unresolvedType.Type.Type.IsDefined(typeof(ExtendObjectTypeAttribute), true);
コード例 #20
0
 private static bool IsPublicInternal(ExtendedTypeReference unresolvedType)
 => unresolvedType.Type.Type.IsPublic ||
 unresolvedType.Type.Type.IsNestedPublic;
コード例 #21
0
        private static IObjectFieldDescriptor UseSortingInternal(
            IObjectFieldDescriptor descriptor,
            Type?sortType,
            string?scope)
        {
            FieldMiddleware placeholder         = next => context => default;
            string          argumentPlaceholder =
                "_" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);

            descriptor
            .Use(placeholder)
            .Extend()
            .OnBeforeCreate(
                (c, definition) =>
            {
                ISortConvention convention = c.GetSortConvention(scope);
                Type argumentType;
                if (sortType is null)
                {
                    if (definition.ResultType is null ||
                        definition.ResultType == typeof(object) ||
                        !c.TypeInspector.TryCreateTypeInfo(
                            definition.ResultType,
                            out ITypeInfo? typeInfo))
                    {
                        throw new ArgumentException(
                            SortObjectFieldDescriptorExtensions_UseSorting_CannotHandleType,
                            nameof(descriptor));
                    }


                    ExtendedTypeReference fieldType = convention
                                                      .GetFieldType(typeInfo.NamedType);
                    argumentType = fieldType.Type.Type;
                }
                else
                {
                    argumentType = sortType;
                }

                ExtendedTypeReference argumentTypeReference = c.TypeInspector.GetTypeRef(
                    typeof(ListType <>).MakeGenericType(
                        typeof(NonNullType <>).MakeGenericType(argumentType)),
                    TypeContext.Input,
                    scope);


                var argumentDefinition = new ArgumentDefinition
                {
                    Name = argumentPlaceholder, Type = argumentTypeReference
                };

                definition.Arguments.Add(argumentDefinition);

                definition.Configurations.Add(
                    LazyTypeConfigurationBuilder
                    .New <ObjectFieldDefinition>()
                    .Definition(definition)
                    .Configure(
                        (context, def) =>
                        CompileMiddleware(
                            context,
                            def,
                            argumentTypeReference,
                            placeholder,
                            scope))
                    .On(ApplyConfigurationOn.Completion)
                    .DependsOn(argumentTypeReference, true)
                    .Build());

                argumentDefinition.Configurations.Add(
                    LazyTypeConfigurationBuilder
                    .New <ArgumentDefinition>()
                    .Definition(argumentDefinition)
                    .Configure(
                        (context, argumentDefinition) =>
                        argumentDefinition.Name =
                            context.GetSortConvention(scope).GetArgumentName())
                    .On(ApplyConfigurationOn.Naming)
                    .Build());
            });

            return(descriptor);
        }
コード例 #22
0
 private static bool CompareSchemaAndExtendedTypeRef(
     SchemaTypeReference schemaTypeReference,
     ExtendedTypeReference extendedTypeReference) =>
 schemaTypeReference.Type.GetType() == extendedTypeReference.Type.Source;
コード例 #23
0
        public static bool TryInferSchemaType(
            ITypeInspector typeInspector,
            ExtendedTypeReference unresolvedType,
            out ExtendedTypeReference schemaType)
        {
            if (typeInspector is null)
            {
                throw new ArgumentNullException(nameof(typeInspector));
            }

            if (unresolvedType is null)
            {
                throw new ArgumentNullException(nameof(unresolvedType));
            }

            if (IsObjectTypeExtension(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(ObjectTypeExtension <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsUnionType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(UnionType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsInterfaceType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(InterfaceType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsObjectType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(ObjectType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsInputObjectType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(InputObjectType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (IsEnumType(unresolvedType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(
                        typeof(EnumType <>).MakeGenericType(unresolvedType.Type.Type)));
            }
            else if (Scalars.TryGetScalar(unresolvedType.Type.Type, out Type scalarType))
            {
                schemaType = unresolvedType.With(
                    type: typeInspector.GetType(scalarType));
            }
            else
            {
                schemaType = null;
            }

            return(schemaType != null);
        }
コード例 #24
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());
            }
        }