Ejemplo n.º 1
0
        private Type GetSchemaType(
            IDescriptorContext context,
            MemberInfo member)
        {
            Type?          type       = SchemaType;
            ITypeReference returnType = context.TypeInspector.GetOutputReturnTypeRef(member);

            if (type is null &&
                returnType is ExtendedTypeReference clr &&
                context.TypeInspector.TryCreateTypeInfo(clr.Type, out ITypeInfo? typeInfo))
            {
                if (typeInfo.IsSchemaType)
                {
                    type = typeInfo.NamedType;
                }
                else if (SchemaTypeResolver.TryInferSchemaType(
                             context.TypeInspector,
                             clr.WithType(context.TypeInspector.GetType(typeInfo.NamedType)),
                             out ExtendedTypeReference schemaType))
                {
                    type = schemaType.Type.Source;
                }
            }

            if (type is null || !typeof(IType).IsAssignableFrom(type))
            {
                throw UsePagingAttribute_NodeTypeUnknown(member);
            }

            return(type);
        }
Ejemplo n.º 2
0
        public static IExtendedType GetSchemaType(
            ITypeInspector typeInspector,
            MemberInfo?member,
            Type?type)
        {
            if (type is null &&
                member is not null &&
                typeInspector.GetOutputReturnTypeRef(member) is ExtendedTypeReference r &&
                typeInspector.TryCreateTypeInfo(r.Type, out ITypeInfo? typeInfo))
            {
                // if the member has already associated a schema type with an attribute for instance
                // we will just take it. Since we want the entity element we are going to take
                // the element type of the list or array as our entity type.
                if (r.Type.IsSchemaType && r.Type.IsArrayOrList)
                {
                    return(r.Type.ElementType !);
                }

                // if the member type is unknown we will try to infer it by extracting
                // the named type component from it and running the type inference.
                // It might be that we either are unable to infer or get the wrong type
                // in special cases. In the case we are getting it wrong the user has
                // to explicitly bind the type.
                if (SchemaTypeResolver.TryInferSchemaType(
                        typeInspector,
                        r.WithType(typeInspector.GetType(typeInfo.NamedType)),
                        out ExtendedTypeReference schemaTypeRef))
                {
                    // if we are able to infer the type we will reconstruct its structure so that
                    // we can correctly extract from it the element type with the correct
                    // nullability information.
                    Type current = schemaTypeRef.Type.Type;

                    foreach (TypeComponent component in typeInfo.Components.Reverse().Skip(1))
                    {
                        if (component.Kind == TypeComponentKind.NonNull)
                        {
                            current = typeof(NonNullType <>).MakeGenericType(current);
                        }
                        else if (component.Kind == TypeComponentKind.List)
                        {
                            current = typeof(ListType <>).MakeGenericType(current);
                        }
                    }

                    if (typeInspector.GetType(current) is { IsArrayOrList : true } schemaType)
                    {
                        return(schemaType.ElementType !);
                    }
                }
            }

            if (type is null || !typeof(IType).IsAssignableFrom(type))
            {
                throw ThrowHelper.UsePagingAttribute_NodeTypeUnknown(member);
            }

            return(typeInspector.GetType(type));
        }
        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);
        }
        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);
        }
        public void InferEnumType(TypeContext context)
        {
            // arrange
            ClrTypeReference typeReference = TypeReference.Create(
                typeof(Foo),
                context);

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

            // assert
            Assert.True(success);
            Assert.Equal(TypeContext.None, schemaType.Context);
            Assert.Equal(typeof(EnumType <Foo>), schemaType.Type);
        }
        public void InferInputObjectType()
        {
            // arrange
            ClrTypeReference typeReference = TypeReference.Create(
                typeof(Bar),
                TypeContext.Input);

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

            // assert
            Assert.True(success);
            Assert.Equal(TypeContext.Input, schemaType.Context);
            Assert.Equal(typeof(InputObjectType <Bar>), schemaType.Type);
        }
        public void InferInterfaceType(TypeContext context)
        {
            // arrange
            ClrTypeReference typeReference = TypeReference.Create(
                typeof(IBar),
                context);

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

            // assert
            Assert.True(success);
            Assert.Equal(TypeContext.Output, schemaType.Context);
            Assert.Equal(typeof(InterfaceType <IBar>), schemaType.Type);
        }
Ejemplo n.º 8
0
        public void InferEnumType(TypeContext context)
        {
            // arrange
            var typeReference = new ClrTypeReference(
                typeof(Foo),
                context);

            // act
            bool success = SchemaTypeResolver.TryInferSchemaType(
                typeReference,
                out IClrTypeReference schemaType);

            // assert
            Assert.True(success);
            Assert.Equal(context, schemaType.Context);
            Assert.Equal(typeof(EnumType <Foo>), schemaType.Type);
        }
Ejemplo n.º 9
0
        public void InferObjectType(TypeContext context)
        {
            // arrange
            var typeReference = new ClrTypeReference(
                typeof(Bar),
                context);

            // act
            bool success = SchemaTypeResolver.TryInferSchemaType(
                typeReference,
                out IClrTypeReference schemaType);

            // assert
            Assert.True(success);
            Assert.Equal(TypeContext.Output, schemaType.Context);
            Assert.Equal(typeof(ObjectType <Bar>), schemaType.Type);
        }