private Type GetSchemaType(
            IDescriptorContext context,
            MemberInfo member)
        {
            Type?          type       = SchemaType;
            ITypeReference returnType = context.Inspector.GetReturnType(
                member, TypeContext.Output);

            if (type is null &&
                returnType is ClrTypeReference clr &&
                TypeInspector.Default.TryCreate(clr.Type, out var typeInfo))
            {
                if (BaseTypes.IsSchemaType(typeInfo.ClrType))
                {
                    type = typeInfo.ClrType;
                }
                else if (SchemaTypeResolver.TryInferSchemaType(
                             clr.WithType(typeInfo.ClrType),
                             out ClrTypeReference schemaType))
                {
                    type = schemaType.Type;
                }
            }

            if (type is null || !typeof(IType).IsAssignableFrom(type))
            {
                throw new SchemaException(
                          SchemaErrorBuilder.New()
                          .SetMessage("The UsePaging attribute needs a valid node schema type.")
                          .SetCode("ATTR_USEPAGING_SCHEMATYPE_INVALID")
                          .Build());
            }

            return(type);
        }
        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);
        }
Beispiel #3
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);
        }
Beispiel #9
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);
        }
Beispiel #10
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);
        }