コード例 #1
0
            private static bool IsMutationType(
                ITypeInspector typeInspector,
                TypeSystemObjectBase type,
                Dictionary <OperationType, ITypeReference> operations)
            {
                if (type is ObjectType objectType)
                {
                    if (operations.TryGetValue(OperationType.Mutation, out ITypeReference? typeRef))
                    {
                        if (typeRef is SchemaTypeReference sr)
                        {
                            return(sr.Type == objectType);
                        }

                        if (typeRef is ExtendedTypeReference cr)
                        {
                            return(cr.Type == typeInspector.GetType(objectType.GetType()) ||
                                   cr.Type == typeInspector.GetType(objectType.RuntimeType));
                        }

                        if (typeRef is SyntaxTypeReference str)
                        {
                            return(objectType.Name.Equals(str.Type.NamedType().Name.Value));
                        }
                    }
                    else
                    {
                        return(type.Name.Equals(WellKnownTypes.Mutation));
                    }
                }

                return(false);
            }
コード例 #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));
        }
コード例 #3
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))
                {
                    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);
                    }
                }
            }
        }
コード例 #4
0
        private static void AddSerializerToObjectField(
            ITypeCompletionContext completionContext,
            ObjectFieldDefinition definition,
            FieldMiddleware placeholder,
            NameString typeName)
        {
            ITypeInspector typeInspector = completionContext.TypeInspector;
            IExtendedType? resultType;

            if (definition.ResultType is not null)
            {
                resultType = typeInspector.GetType(definition.ResultType);
            }
            else if (definition.Type is ExtendedTypeReference typeReference)
            {
                resultType = typeReference.Type;
            }
            else
            {
                throw new SchemaException(SchemaErrorBuilder.New()
                                          .SetMessage("Unable to resolve type from field `{0}`.", definition.Name)
                                          .SetTypeSystemObject(completionContext.Type)
                                          .Build());
            }

            NameString schemaName = default;

            completionContext.DescriptorContext.SchemaCompleted += (sender, args) =>
                                                                   schemaName = args.Schema.Name;

            IIdSerializer serializer =
                completionContext.Services.GetService <IIdSerializer>() ??
                new IdSerializer();
            var index = definition.MiddlewareComponents.IndexOf(placeholder);

            definition.MiddlewareComponents[index] = next => async context =>
            {
                await next(context).ConfigureAwait(false);

                if (context.Result is not null)
                {
                    if (resultType.IsArrayOrList)
                    {
                        var list = new List <object?>();
                        foreach (object?element in (IEnumerable)context.Result)
                        {
                            list.Add(element is null
                                ? element
                                : serializer.Serialize(schemaName, typeName, element));
                        }
                        context.Result = list;
                    }
                    else
                    {
                        context.Result = serializer.Serialize(schemaName, typeName, context.Result);
                    }
                }
            };
        }
コード例 #5
0
        public void ITypeReference_Equals_To_SyntaxTypeRef()
        {
            // arrange
            SyntaxTypeReference x = TypeReference.Create(
                "Foo",
                TypeContext.None);

            // act
            var xx = x.Equals(TypeReference.Create(_typeInspector.GetType(typeof(int))));

            // assert
            Assert.False(xx);
        }
コード例 #6
0
            private static bool IsOperationType(
                ObjectType objectType,
                OperationType operationType,
                ITypeInspector typeInspector,
                Dictionary <OperationType, ITypeReference> operations)
            {
                if (operations.TryGetValue(operationType, out ITypeReference? typeRef))
                {
                    if (typeRef is SchemaTypeReference sr)
                    {
                        return(sr.Type == objectType);
                    }

                    if (typeRef is ExtendedTypeReference cr)
                    {
                        return(cr.Type == typeInspector.GetType(objectType.GetType()) ||
                               cr.Type == typeInspector.GetType(objectType.RuntimeType));
                    }

                    if (typeRef is SyntaxTypeReference str)
                    {
                        return(objectType.Name.Equals(str.Type.NamedType().Name.Value));
                    }
                }
                else if (operationType == OperationType.Query)
                {
                    return(objectType.Name.Equals(OperationTypeNames.Query));
                }
                else if (operationType == OperationType.Mutation)
                {
                    return(objectType.Name.Equals(OperationTypeNames.Mutation));
                }
                else if (operationType == OperationType.Subscription)
                {
                    return(objectType.Name.Equals(OperationTypeNames.Subscription));
                }

                return(false);
            }
コード例 #7
0
        private static IExtendedType GetSourceType(
            ITypeInspector typeInspector,
            ObjectFieldDefinition definition,
            Type entityType)
        {
            // if an explicit result type is defined we will type it since it expresses the
            // intend.
            if (definition.ResultType is not null)
            {
                return(typeInspector.GetType(definition.ResultType));
            }

            // Otherwise we will look at specified members and extract the return type.
            MemberInfo?member = definition.ResolverMember ?? definition.Member;

            if (member is not null)
            {
                return(typeInspector.GetReturnType(member, true));
            }

            // if we were not able to resolve the source type we will assume that it is
            // an enumerable of the entity type.
            return(typeInspector.GetType(typeof(IEnumerable <>).MakeGenericType(entityType)));
        }
コード例 #8
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);
        }
コード例 #9
0
    private static IExtendedType RewriteType(ITypeInspector typeInspector, ITypeInfo typeInfo)
    {
        Type current = typeof(IdType);

        if (typeInfo.Components.Count > 1)
        {
            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);
                }
            }
        }

        return(typeInspector.GetType(current));
    }
コード例 #10
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);
        }
コード例 #11
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);
            }
        }
    }
コード例 #12
0
        public void TypeReference_Create(
            Type clrType,
            TypeContext context,
            string scope)
        {
            // arrange
            // act
            ExtendedTypeReference typeReference = TypeReference.Create(
                _typeInspector.GetType(clrType),
                context,
                scope: scope);

            // assert
            Assert.Equal(clrType, typeReference.Type.Source);
            Assert.Equal(context, typeReference.Context);
            Assert.Equal(scope, typeReference.Scope);
        }
コード例 #13
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);
        }
コード例 #14
0
        private static void AddSerializerToObjectField(
            ITypeCompletionContext completionContext,
            ObjectFieldDefinition definition,
            ResultConverterDefinition placeholder,
            NameString typeName)
        {
            ITypeInspector typeInspector = completionContext.TypeInspector;
            IExtendedType? resultType;

            if (definition.ResultType is not null)
            {
                resultType = typeInspector.GetType(definition.ResultType);
            }
            else if (definition.Type is ExtendedTypeReference typeReference)
            {
                resultType = typeReference.Type;
            }
            else
            {
                throw new SchemaException(SchemaErrorBuilder.New()
                                          .SetMessage("Unable to resolve type from field `{0}`.", definition.Name)
                                          .SetTypeSystemObject(completionContext.Type)
                                          .Build());
            }

            NameString schemaName = default;

            completionContext.DescriptorContext.SchemaCompleted += (_, args) =>
                                                                   schemaName = args.Schema.Name;

            IIdSerializer serializer =
                completionContext.Services.GetService <IIdSerializer>() ??
                new IdSerializer();
            var index = definition.ResultConverters.IndexOf(placeholder);

            if (typeName.IsEmpty)
            {
                typeName = completionContext.Type.Name;
            }

            definition.ResultConverters[index] = new((_, result) =>
            {
                if (result is not null)
                {
                    if (resultType.IsArrayOrList)
                    {
                        var list = new List <object?>();

                        foreach (var element in (IEnumerable)result)
                        {
                            list.Add(element is null
                                ? element
                                : serializer.Serialize(schemaName, typeName, element));
                        }

                        return(list);
                    }

                    return(serializer.Serialize(schemaName, typeName, result));
                }

                return(result);
            }, key : WellKnownMiddleware.GlobalId, isRepeatable : false);
        }