Beispiel #1
0
        public void CreateUnion()
        {
            // arrange
            DocumentNode document = Parser.Default.Parse(
                "union X = A | B");
            UnionTypeDefinitionNode unionTypeDefinition = document
                                                          .Definitions.OfType <UnionTypeDefinitionNode>().First();

            var context       = new SchemaContext();
            var configuration = new SchemaConfiguration(
                context.RegisterServiceProvider,
                context.Types);

            configuration.RegisterType(new ObjectType(d =>
                                                      d.Name("A").Field("a").Type <StringType>()));
            configuration.RegisterType(new ObjectType(d =>
                                                      d.Name("B").Field("a").Type <StringType>()));

            // act
            var       factory   = new UnionTypeFactory();
            UnionType unionType = factory.Create(unionTypeDefinition);

            configuration.RegisterType(unionType);

            var typeFinalizer = new TypeFinalizer(configuration);

            typeFinalizer.FinalizeTypes(context, null);

            // assert
            Assert.Equal("X", unionType.Name);
            Assert.Equal(2, unionType.Types.Count);
            Assert.Equal("A", unionType.Types.First().Key);
            Assert.Equal("B", unionType.Types.Last().Key);
        }
Beispiel #2
0
        public void CreateEnum()
        {
            // arrange
            DocumentNode document = Parser.Default.Parse(
                "enum Abc { A B C }");
            EnumTypeDefinitionNode typeDefinition = document
                                                    .Definitions.OfType <EnumTypeDefinitionNode>().First();

            var context       = new SchemaContext();
            var configuration = new SchemaConfiguration(
                context.RegisterServiceProvider,
                context.Types);

            // act
            var      factory  = new EnumTypeFactory();
            EnumType enumType = factory.Create(typeDefinition);

            configuration.RegisterType(enumType);

            var typeFinalizer = new TypeFinalizer(configuration);

            typeFinalizer.FinalizeTypes(context, null);

            // assert
            Assert.Equal("Abc", enumType.Name);
            Assert.Collection(enumType.Values,
                              t => Assert.Equal("A", t.Name),
                              t => Assert.Equal("B", t.Name),
                              t => Assert.Equal("C", t.Name));
        }
Beispiel #3
0
        private static IReadOnlySchemaOptions ExecuteSchemaConfiguration(
            SchemaContext context,
            Action <ISchemaConfiguration> configure,
            List <SchemaError> errors)
        {
            try
            {
                // configure resolvers, custom types and type mappings.
                SchemaConfiguration configuration = new SchemaConfiguration(
                    context.ServiceManager.RegisterServiceProvider,
                    context.Types);
                configure(configuration);

                TypeFinalizer typeFinalizer = new TypeFinalizer(configuration);
                typeFinalizer.FinalizeTypes(context);
                errors.AddRange(typeFinalizer.Errors);

                return(new ReadOnlySchemaOptions(configuration.Options));
            }
            catch (Exception ex)
            {
                throw new SchemaException(new[]
                {
                    new SchemaError(ex.Message, null, ex)
                });
            }
        }
        public void CreateIncludeDirective()
        {
            // arrange
            SchemaContext       schemaContext       = SchemaContextFactory.Create();
            SchemaConfiguration schemaConfiguration =
                new SchemaConfiguration(sp => { }, schemaContext.Types);
            TypeFinalizer typeFinalizer = new TypeFinalizer(schemaConfiguration);

            typeFinalizer.FinalizeTypes(schemaContext, null);

            // assert
            Directive directive = schemaContext.Directives
                                  .GetDirectives().FirstOrDefault(t => t.Name == "include");

            // assert
            Assert.NotNull(directive);
            Assert.IsType <IncludeDirective>(directive);
            Assert.Equal("include", directive.Name);
            Assert.Collection(directive.Arguments,
                              t =>
            {
                Assert.Equal("if", t.Name);
                Assert.IsType <NonNullType>(t.Type);
                Assert.IsType <BooleanType>(((NonNullType)t.Type).Type);
            });
            Assert.Collection(directive.Locations,
                              t => Assert.Equal(DirectiveLocation.Field, t),
                              t => Assert.Equal(DirectiveLocation.FragmentSpread, t),
                              t => Assert.Equal(DirectiveLocation.InlineFragment, t));
        }
        private static IReadOnlySchemaOptions ExecuteSchemaConfiguration(
            SchemaContext context,
            Action <ISchemaConfiguration> configure,
            List <SchemaError> errors)
        {
            try
            {
                // configure resolvers, custom types and type mappings.
                var configuration = new SchemaConfiguration(
                    context.RegisterServiceProvider,
                    context.Types,
                    context.Resolvers,
                    context.Directives);

                configuration.RegisterCustomContext <IResolverCache>(
                    ExecutionScope.Global,
                    s => new ResolverCache());

                configure(configuration);

                var options       = new ReadOnlySchemaOptions(configuration.Options);
                var typeFinalizer = new TypeFinalizer(configuration);
                typeFinalizer.FinalizeTypes(context, options.QueryTypeName);
                errors.AddRange(typeFinalizer.Errors);

                return(options);
            }
            catch (Exception ex)
            {
                throw new SchemaException(new[]
                {
                    new SchemaError(ex.Message, null, ex)
                });
            }
        }
        private DirectiveType CreateDirective <T>(T directiveType)
            where T : DirectiveType
        {
            var schemaContext = new SchemaContext();

            schemaContext.Types.RegisterType(new StringType());
            schemaContext.Directives.RegisterDirectiveType(directiveType);

            var schemaConfiguration = new SchemaConfiguration(
                sp => { },
                schemaContext.Types,
                schemaContext.Directives);

            var typeFinalizer = new TypeFinalizer(schemaConfiguration);

            typeFinalizer.FinalizeTypes(schemaContext, null);

            return(schemaContext.Directives.GetDirectiveTypes().Single());
        }
        public void CreateCostDirective()
        {
            // arrange
            SchemaContext schemaContext = SchemaContextFactory.Create();

            var schemaConfiguration = new SchemaConfiguration(
                sp => { },
                schemaContext.Types,
                schemaContext.Resolvers,
                schemaContext.Directives);

            var typeFinalizer = new TypeFinalizer(schemaConfiguration);

            typeFinalizer.FinalizeTypes(schemaContext, null);

            // assert
            DirectiveType directive = schemaContext.Directives
                                      .GetDirectiveTypes()
                                      .FirstOrDefault(t => t.Name == "cost");

            // assert
            Assert.NotNull(directive);
            Assert.IsType <CostDirectiveType>(directive);
            Assert.Equal("cost", directive.Name);
            Assert.Collection(directive.Arguments,
                              t =>
            {
                Assert.Equal("complexity", t.Name);
                Assert.IsType <IntType>(
                    Assert.IsType <NonNullType>(t.Type).Type);
            },
                              t =>
            {
                Assert.Equal("multipliers", t.Name);
                Assert.IsType <MultiplierPathType>(
                    Assert.IsType <NonNullType>(
                        Assert.IsType <ListType>(t.Type).ElementType).Type);
            });
            Assert.Collection(directive.Locations,
                              t => Assert.Equal(DirectiveLocation.FieldDefinition, t));
        }