예제 #1
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);
        }
예제 #2
0
        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);
        }
        protected ITypeReference RewriteTypeListType()
        {
            ITypeReference reference = Definition.Type;

            if (reference is IClrTypeReference clrRef)
            {
                if (BaseTypes.IsSchemaType(clrRef.Type))
                {
                    return(clrRef.WithType(
                               typeof(ListType <>).MakeGenericType(clrRef.Type)));
                }
                else
                {
                    return(clrRef.WithType(
                               typeof(List <>).MakeGenericType(clrRef.Type)));
                }
            }

            if (reference is ISchemaTypeReference schemaRef)
            {
                return(schemaRef.WithType(new ListType((IType)schemaRef.Type)));
            }

            if (reference is ISyntaxTypeReference syntaxRef)
            {
                return(syntaxRef.WithType(new ListTypeNode(syntaxRef.Type)));
            }

            throw new NotSupportedException();
        }
예제 #4
0
        public IObjectFieldDescriptor Resolver(
            FieldResolverDelegate fieldResolver,
            Type resultType)
        {
            if (fieldResolver == null)
            {
                throw new ArgumentNullException(nameof(fieldResolver));
            }

            Definition.Resolver = fieldResolver;

            if (resultType != null)
            {
                Definition.SetMoreSpecificType(resultType, TypeContext.Output);

                Type resultTypeDef = resultType.GetGenericTypeDefinition();
                Type clrResultType = resultType.IsGenericType &&
                                     resultTypeDef == typeof(NativeType <>)
                        ? resultType.GetGenericArguments()[0]
                        : resultType;
                if (!BaseTypes.IsSchemaType(clrResultType))
                {
                    Definition.ResultType = clrResultType;
                }
            }
            return(this);
        }
        protected ITypeReference RewriteTypeToNullableType()
        {
            ITypeReference reference = Definition.Type;

            if (reference is IClrTypeReference clrRef &&
                TypeInspector.Default.TryCreate(
                    clrRef.Type,
                    out Utilities.TypeInfo typeInfo))
            {
                if (BaseTypes.IsSchemaType(typeInfo.ClrType))
                {
                    if (clrRef.Type.IsGenericType &&
                        clrRef.Type.GetGenericTypeDefinition() ==
                        typeof(NonNullType <>))
                    {
                        return(clrRef.WithType(typeInfo.Components[1]));
                    }
                    return(clrRef);
                }
                else
                {
                    if (clrRef.Type.IsValueType)
                    {
                        if (Nullable.GetUnderlyingType(clrRef.Type) == null)
                        {
                            return(clrRef.WithType(
                                       typeof(Nullable <>).MakeGenericType(clrRef.Type)));
                        }
                        return(clrRef);
                    }
                    else if (clrRef.Type.IsGenericType &&
                             clrRef.Type.GetGenericTypeDefinition() ==
                             typeof(NonNullType <>))
                    {
                        return(clrRef.WithType(typeInfo.Components[1]));
                    }
                    return(clrRef);
                }
            }

            if (reference is ISchemaTypeReference schemaRef)
            {
                return(schemaRef.Type is NonNullType nnt
                    ? schemaRef.WithType(nnt)
                    : schemaRef);
            }

            if (reference is ISyntaxTypeReference syntaxRef)
            {
                return(syntaxRef.Type is NonNullTypeNode nnt
                    ? syntaxRef.WithType(nnt)
                    : syntaxRef);
            }

            throw new NotSupportedException();
        }
예제 #6
0
        public static bool IsTypeMoreSpecific(
            this TypeReference typeReference, Type type)
        {
            if (typeReference == null ||
                BaseTypes.IsSchemaType(type))
            {
                return(true);
            }

            if (typeReference.IsNativeTypeReference() &&
                !BaseTypes.IsSchemaType(typeReference.NativeType))
            {
                return(true);
            }

            return(false);
        }
예제 #7
0
        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));
        }
예제 #8
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);
        }
예제 #9
0
        private static bool IsTypeMoreSpecific(
            ITypeReference typeReference,
            Type type)
        {
            if (typeReference is SchemaTypeReference)
            {
                return(false);
            }

            if (typeReference == null ||
                BaseTypes.IsSchemaType(type))
            {
                return(true);
            }

            if (typeReference is ClrTypeReference clr &&
                !BaseTypes.IsSchemaType(clr.Type))
            {
                return(true);
            }

            return(false);
        }
예제 #10
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);
        }
예제 #11
0
 public bool IsSchemaType(Type type) =>
 BaseTypes.IsSchemaType(type);