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))
                {
                    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);
                    }
                }
            }
        }
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));
        }
Ejemplo n.º 3
0
        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);
        }
        private static Type GetArgumentType(
            ObjectFieldDefinition definition,
            Type?filterType,
            ITypeInspector typeInspector)
        {
            Type?argumentType = filterType;

            if (argumentType is null)
            {
                if (definition.ResultType is null ||
                    definition.ResultType == typeof(object) ||
                    !typeInspector.TryCreateTypeInfo(
                        definition.ResultType, out ITypeInfo? typeInfo))
                {
                    throw new SchemaException(
                              SchemaErrorBuilder.New()
                              .SetMessage("Cannot handle the specified type.")
                              .SetExtension("fieldName", definition.Name)
                              .Build());
                }

                argumentType = typeof(SortInputType <>).MakeGenericType(typeInfo.NamedType);
            }

            if (argumentType == typeof(object))
            {
                // TODO : resources
                throw new SchemaException(
                          SchemaErrorBuilder.New()
                          .SetMessage(
                              "The sort type cannot be " +
                              "inferred from `System.Object`.")
                          .SetCode(ErrorCodes.Filtering.FilterObjectType)
                          .Build());
            }

            return(argumentType);
        }
Ejemplo n.º 5
0
    public void Handle(ITypeRegistrar typeRegistrar, ITypeReference typeReference)
    {
        var typeRef = (ExtendedTypeReference)typeReference;

        if (_typeInspector.TryCreateTypeInfo(typeRef.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 = typeRef.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);
            }
        }
    }