Exemple #1
0
        public void Initializer_SchemaOptions_Are_Null()
        {
            // arrange
            var initialTypes = new List <ITypeReference>();

            initialTypes.Add(new ClrTypeReference(
                                 typeof(Foo),
                                 TypeContext.Output));

            var serviceProvider = new EmptyServiceProvider();

            var typeInitializer = new TypeInitializer(
                serviceProvider,
                DescriptorContext.Create(),
                new Dictionary <string, object>(),
                initialTypes,
                new List <Type>(),
                new AggregateTypeInitializationInterceptor(),
                null,
                t => t is ObjectType <Foo>);

            // act
            Action action =
                () => typeInitializer.Initialize(() => null, null);

            // assert
            Assert.Throws <ArgumentNullException>(action);
        }
Exemple #2
0
        public void Register_ClrType_InferSchemaTypes()
        {
            // arrange
            var initialTypes = new List <ITypeReference>();

            initialTypes.Add(TypeReference.Create(
                                 typeof(Foo),
                                 TypeContext.Output));

            var serviceProvider = new EmptyServiceProvider();

            var typeInitializer = new TypeInitializer(
                serviceProvider,
                DescriptorContext.Create(),
                initialTypes,
                new List <Type>(),
                new AggregateTypeInitializationInterceptor(),
                null,
                t => t is ObjectType <Foo>);

            // act
            typeInitializer.Initialize(() => null, new SchemaOptions());

            // assert
            bool exists = typeInitializer.DiscoveredTypes.TryGetType(
                TypeReference.Create(
                    typeof(ObjectType <Foo>),
                    TypeContext.Output),
                out RegisteredType type);

            Assert.True(exists);
            Dictionary <string, string> fooType =
                Assert.IsType <ObjectType <Foo> >(type.Type).Fields.ToDictionary(
                    t => t.Name.ToString(),
                    t => TypeVisualizer.Visualize(t.Type));

            exists = typeInitializer.DiscoveredTypes.TryGetType(
                TypeReference.Create(typeof(ObjectType <Bar>), TypeContext.Output),
                out type);

            Assert.True(exists);
            Dictionary <string, string> barType =
                Assert.IsType <ObjectType <Bar> >(type.Type).Fields.ToDictionary(
                    t => t.Name.ToString(),
                    t => TypeVisualizer.Visualize(t.Type));

            new { fooType, barType }.MatchSnapshot();
        }
Exemple #3
0
        public void Register_SchemaType_ClrTypeExists()
        {
            // arrange
            var typeInterceptor = new AggregateTypeInterceptor();

            typeInterceptor.SetInterceptors(new[] { new IntrospectionTypeInterceptor() });
            IDescriptorContext context = DescriptorContext.Create(
                typeInterceptor: typeInterceptor);
            var typeRegistry = new TypeRegistry(context.TypeInterceptor);

            var typeInitializer = new TypeInitializer(
                context,
                typeRegistry,
                new List <ITypeReference>
            {
                context.TypeInspector.GetTypeRef(typeof(FooType), TypeContext.Output)
            },
                null,
                t => t is FooType ? RootTypeKind.Query : RootTypeKind.None);

            // act
            typeInitializer.Initialize(() => null, new SchemaOptions());

            // assert
            var exists = typeRegistry.TryGetType(
                context.TypeInspector.GetTypeRef(typeof(FooType), TypeContext.Output),
                out RegisteredType type);

            Assert.True(exists);
            var fooType =
                Assert.IsType <FooType>(type.Type).Fields.ToDictionary(
                    t => t.Name.ToString(),
                    t => t.Type.Print());

            exists = typeRegistry.TryGetType(
                context.TypeInspector.GetTypeRef(typeof(BarType), TypeContext.Output),
                out type);

            Assert.True(exists);
            var barType =
                Assert.IsType <BarType>(type.Type).Fields.ToDictionary(
                    t => t.Name.ToString(),
                    t => t.Type.Print());

            new { fooType, barType }.MatchSnapshot();
        }
        public void Register_ClrType_InferSchemaTypes()
        {
            // arrange
            IDescriptorContext context = DescriptorContext.Create(
                typeInterceptor: new AggregateTypeInterceptor(new IntrospectionTypeInterceptor()));
            var typeRegistry = new TypeRegistry();

            var typeInitializer = new TypeInitializer(
                context,
                typeRegistry,
                new List <ITypeReference>
            {
                context.TypeInspector.GetTypeRef(typeof(Foo), TypeContext.Output)
            },
                new List <Type>(),
                null,
                t => t is ObjectType <Foo>,
                t => t is FooType);

            // act
            typeInitializer.Initialize(() => null, new SchemaOptions());

            // assert
            var exists = typeRegistry.TryGetType(
                context.TypeInspector.GetTypeRef(typeof(ObjectType <Foo>), TypeContext.Output),
                out RegisteredType type);

            Assert.True(exists);
            var fooType =
                Assert.IsType <ObjectType <Foo> >(type.Type).Fields.ToDictionary(
                    t => t.Name.ToString(),
                    t => t.Type.Print());

            exists = typeRegistry.TryGetType(
                context.TypeInspector.GetTypeRef(typeof(ObjectType <Bar>), TypeContext.Output),
                out type);

            Assert.True(exists);
            var barType =
                Assert.IsType <ObjectType <Bar> >(type.Type).Fields.ToDictionary(
                    t => t.Name.ToString(),
                    t => t.Type.Print());

            new { fooType, barType }.MatchSnapshot();
        }
Exemple #5
0
        public void Register_SchemaType_ClrTypeExists()
        {
            // arrange
            var initialTypes = new List <ITypeReference>();

            initialTypes.Add(new ClrTypeReference(
                                 typeof(FooType),
                                 TypeContext.Output));

            var serviceProvider = new EmptyServiceProvider();

            var typeInitializer = new TypeInitializer(
                serviceProvider,
                DescriptorContext.Create(),
                new Dictionary <string, object>(),
                initialTypes,
                new List <Type>(),
                new AggregateTypeInitializationInterceptor(),
                null,
                t => t is FooType);

            // act
            typeInitializer.Initialize(() => null, new SchemaOptions());

            // assert
            bool exists = typeInitializer.DiscoveredTypes.TryGetType(
                new ClrTypeReference(typeof(FooType), TypeContext.Output),
                out RegisteredType type);

            Assert.True(exists);
            Assert.IsType <FooType>(type.Type).Fields.ToDictionary(
                t => t.Name.ToString(),
                t => TypeVisualizer.Visualize(t.Type))
            .MatchSnapshot(new SnapshotNameExtension("FooType"));

            exists = typeInitializer.DiscoveredTypes.TryGetType(
                new ClrTypeReference(typeof(BarType), TypeContext.Output),
                out type);

            Assert.True(exists);
            Assert.IsType <BarType>(type.Type).Fields.ToDictionary(
                t => t.Name.ToString(),
                t => TypeVisualizer.Visualize(t.Type))
            .MatchSnapshot(new SnapshotNameExtension("BarType"));
        }
Exemple #6
0
        public TypeCompletionContext(
            TypeDiscoveryContext initializationContext,
            TypeInitializer typeInitializer,
            IsOfTypeFallback isOfType,
            Func <ISchema> schemaResolver)
        {
            _initializationContext = initializationContext
                                     ?? throw new ArgumentNullException(nameof(initializationContext));
            _typeInitializer = typeInitializer
                               ?? throw new ArgumentNullException(nameof(typeInitializer));
            IsOfType        = isOfType;
            _schemaResolver = schemaResolver
                              ?? throw new ArgumentNullException(nameof(schemaResolver));
            GlobalComponents = new ReadOnlyCollection <FieldMiddleware>(
                _typeInitializer.GlobalComponents);

            _alternateNames.Add(_initializationContext.InternalName);
        }