예제 #1
0
 internal static IReadOnlyList <ITypeReference> CreateReferences(
     ITypeInspector typeInspector) =>
 new List <ITypeReference>
 {
     typeInspector.GetTypeRef(typeof(SkipDirectiveType), TypeContext.None),
     typeInspector.GetTypeRef(typeof(IncludeDirectiveType), TypeContext.None),
 };
예제 #2
0
        public void Register_SchemaType_ClrTypeExists_NoSystemTypes()
        {
            // arrange
            var context      = DescriptorContext.Create();
            var typeRegistry = new TypeRegistry();
            var typeLookup   = new TypeLookup(context.TypeInspector, typeRegistry);

            var typeDiscoverer = new TypeDiscoverer(
                context,
                typeRegistry,
                typeLookup,
                new HashSet <ITypeReference>
            {
                _typeInspector.GetTypeRef(typeof(FooType), TypeContext.Output)
            },
                new AggregateTypeInterceptor(),
                false);

            // act
            IReadOnlyList <ISchemaError> errors = typeDiscoverer.DiscoverTypes();

            // assert
            Assert.Empty(errors);

            new
            {
                registered = typeRegistry.Types
                             .Select(t => new
                {
                    type        = t.Type.GetType().GetTypeName(),
                    runtimeType = t.Type is IHasRuntimeType hr
                            ? hr.RuntimeType.GetTypeName()
                            : null,
                    references = t.References.Select(t => t.ToString()).ToList()
                }).ToList(),
예제 #3
0
        private bool TryInferTypes()
        {
            var inferred = false;

            foreach (ExtendedTypeReference unresolvedType in
                     _typeRegistrar.GetUnresolved().OfType <ExtendedTypeReference>())
            {
                if (Scalars.TryGetScalar(unresolvedType.Type.Type, out Type? scalarType))
                {
                    inferred = true;

                    ExtendedTypeReference typeReference = _typeInspector.GetTypeRef(scalarType);
                    _unregistered.Add(typeReference);
                    _typeRegistrar.MarkResolved(unresolvedType);
                    _typeRegistry.TryRegister(unresolvedType, typeReference);
                }
                else if (SchemaTypeResolver.TryInferSchemaType(
                             _typeInspector, unresolvedType, out ExtendedTypeReference schemaType))
                {
                    inferred = true;

                    _unregistered.Add(schemaType);
                    _typeRegistrar.MarkResolved(unresolvedType);
                }
            }

            return(inferred);
        }
예제 #4
0
    public static void AddDirective <T>(
        this IHasDirectiveDefinition directivesContainer,
        T directive,
        ITypeInspector typeInspector)
        where T : class
    {
        if (directive is null)
        {
            throw new ArgumentNullException(nameof(directive));
        }

        switch (directive)
        {
        case DirectiveNode node:
            directivesContainer.Directives.Add(
                new DirectiveDefinition(node));
            break;

        case string s:
            AddDirective(
                directivesContainer,
                new NameString(s),
                Array.Empty <ArgumentNode>());
            break;

        default:
            directivesContainer.Directives.Add(
                new DirectiveDefinition(
                    directive,
                    typeInspector.GetTypeRef(directive.GetType(), TypeContext.None)));
            break;
        }
    }
예제 #5
0
        public bool TryNormalizeReference(
            IDirectiveReference directiveRef,
            [NotNullWhen(true)] out ITypeReference?namedTypeRef)
        {
            if (directiveRef is null)
            {
                throw new ArgumentNullException(nameof(directiveRef));
            }

            if (directiveRef is ClrTypeDirectiveReference cr)
            {
                ExtendedTypeReference directiveTypeRef = _typeInspector.GetTypeRef(cr.ClrType);
                if (!_typeRegistry.TryGetTypeRef(directiveTypeRef, out namedTypeRef))
                {
                    namedTypeRef = directiveTypeRef;
                }
                return(true);
            }

            if (directiveRef is NameDirectiveReference nr)
            {
                namedTypeRef = _typeRegistry.Types
                               .FirstOrDefault(t => t.Type is DirectiveType && t.Type.Name.Equals(nr.Name))?
                               .References[0];
                return(namedTypeRef is not null);
            }

            namedTypeRef = null;
            return(false);
        }
예제 #6
0
        public bool TryNormalizeReference(
            ITypeReference typeRef,
            [NotNullWhen(true)] out ITypeReference?namedTypeRef)
        {
            if (typeRef is null)
            {
                throw new ArgumentNullException(nameof(typeRef));
            }

            // if we already created a lookup for this type reference we can just return the
            // the type reference to the named type.
            if (_refs.TryGetValue(typeRef, out namedTypeRef))
            {
                return(true);
            }

            switch (typeRef)
            {
            case ExtendedTypeReference r:
                if (TryNormalizeExtendedTypeReference(r, out namedTypeRef))
                {
                    _refs[typeRef] = namedTypeRef;
                    return(true);
                }
                break;

            case SchemaTypeReference r:
                namedTypeRef = _typeInspector.GetTypeRef(
                    r.Type.GetType(), r.Context, typeRef.Scope);
                _refs[typeRef] = namedTypeRef;
                return(true);

            case SyntaxTypeReference r:
                NameString typeName = r.Type.NamedType().Name.Value;
                if (_typeRegistry.TryGetTypeRef(typeName, out namedTypeRef))
                {
                    _refs[typeRef] = namedTypeRef;
                    return(true);
                }
                break;
            }

            namedTypeRef = null;
            return(false);
        }
예제 #7
0
 internal static IReadOnlyList <ITypeReference> CreateReferences(
     ITypeInspector typeInspector) =>
 new List <ITypeReference>
 {
     typeInspector.GetTypeRef(typeof(__Directive)),
     typeInspector.GetTypeRef(typeof(__DirectiveLocation)),
     typeInspector.GetTypeRef(typeof(__EnumValue)),
     typeInspector.GetTypeRef(typeof(__Field)),
     typeInspector.GetTypeRef(typeof(__InputValue)),
     typeInspector.GetTypeRef(typeof(__Schema)),
     typeInspector.GetTypeRef(typeof(__Type)),
     typeInspector.GetTypeRef(typeof(__TypeKind))
 };
예제 #8
0
    private static void CreateNodeField(
        ITypeInspector typeInspector,
        IIdSerializer serializer,
        IList <ObjectFieldDefinition> fields,
        int index)
    {
        ExtendedTypeReference node = typeInspector.GetTypeRef(typeof(NodeType));
        ExtendedTypeReference id   = typeInspector.GetTypeRef(typeof(NonNullType <IdType>));

        var field = new ObjectFieldDefinition(
            Node,
            Relay_NodeField_Description,
            node,
            ResolveNodeAsync)
        {
            Arguments = { new(Id, Relay_NodeField_Id_Description, id) }
        };

        fields.Insert(index, field);

        ValueTask <object?> ResolveNodeAsync(IResolverContext ctx)
        => ResolveSingleNode(ctx, serializer, Id);
    }
예제 #9
0
        /// <inheritdoc />
        public virtual ExtendedTypeReference GetFieldType(MemberInfo member)
        {
            if (member is null)
            {
                throw new ArgumentNullException(nameof(member));
            }

            if (TryCreateFilterType(_typeInspector.GetReturnType(member, true), out Type? rt))
            {
                return(_typeInspector.GetTypeRef(rt, TypeContext.Input, Scope));
            }

            throw FilterConvention_TypeOfMemberIsUnknown(member);
        }
예제 #10
0
        private void RegisterImplicitInterfaceDependencies()
        {
            var withRuntimeType = _typeRegistry.Types
                                  .Where(t => t.RuntimeType != typeof(object))
                                  .Distinct()
                                  .ToList();

            var interfaceTypes = withRuntimeType
                                 .Where(t => t.Type is InterfaceType)
                                 .Distinct()
                                 .ToList();

            var objectTypes = withRuntimeType
                              .Where(t => t.Type is ObjectType)
                              .Distinct()
                              .ToList();

            var dependencies = new List <TypeDependency>();

            foreach (RegisteredType objectType in objectTypes)
            {
                foreach (RegisteredType interfaceType in interfaceTypes)
                {
                    if (interfaceType.RuntimeType.IsAssignableFrom(objectType.RuntimeType))
                    {
                        dependencies.Add(
                            new TypeDependency(
                                _typeInspector.GetTypeRef(
                                    interfaceType.RuntimeType,
                                    TypeContext.Output),
                                TypeDependencyKind.Completed));
                    }
                }

                if (dependencies.Count > 0)
                {
                    objectType.AddDependencies(dependencies);
                    _typeRegistry.Register(objectType);
                    dependencies.Clear();
                }
            }
        }
        public void Register(
            ITypeRegistrar typeRegistrar,
            IEnumerable <ITypeReference> typeReferences)
        {
            foreach (SyntaxTypeReference typeReference in
                     typeReferences.OfType <SyntaxTypeReference>())
            {
                if (Scalars.TryGetScalar(
                        typeReference.Type.NamedType().Name.Value,
                        out Type? scalarType))
                {
                    ExtendedTypeReference namedTypeReference =
                        _typeInspector.GetTypeRef(scalarType);

                    if (!typeRegistrar.IsResolved(namedTypeReference))
                    {
                        typeRegistrar.Register(
                            typeRegistrar.CreateInstance(namedTypeReference.Type.Type),
                            typeReference.Scope);
                    }
                }
            }
        }
 public static ITypeReference GetOutputTypeRef(
     this ITypeInspector typeInspector,
     Type type)
 {
     return(typeInspector.GetTypeRef(type, TypeContext.Output));
 }