Example #1
0
        public void InterfaceType_AddDirectives_DirectiveType()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            schemaContext.Directives.RegisterDirectiveType <FooDirectiveType>();

            // act
            var fooType = new InterfaceType(
                d => d.Directive <FooDirective>()
                .Field("id")
                .Type <StringType>()
                .Directive <FooDirective>());

            // assert
            schemaContext.Types.RegisterType(fooType);
            INeedsInitialization init = fooType;
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            init.RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            Assert.Empty(errors);
            Assert.NotEmpty(fooType.Directives["foo"]);
            Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
        }
        public void IntArgumentIsInferedAsNonNullType()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();
            var intType       = new IntType();

            schemaContext.Types.RegisterType(intType);

            // act
            var fooType = new ObjectType <QueryWithIntArg>();

            // assert
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            ((INeedsInitialization)fooType)
            .RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            Assert.Empty(errors);

            IType argumentType = fooType.Fields["bar"]
                                 .Arguments.First().Type;

            Assert.NotNull(argumentType);
            Assert.True(argumentType.IsNonNullType());
            Assert.Equal(intType, argumentType.NamedType());
        }
        public void GenericInputObject_AddDirectives_NameArgs2()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            schemaContext.Directives.RegisterDirectiveType <FooDirectiveType>();

            // act
            var fooType = new InputObjectType <SimpleInput>(
                d => d.Directive(new NameString("foo"))
                .Field(f => f.Id)
                .Directive(new NameString("foo")));

            // assert
            schemaContext.Types.RegisterType(fooType);
            INeedsInitialization init = fooType;
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            init.RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            Assert.Empty(errors);
            Assert.NotEmpty(fooType.Directives["foo"]);
            Assert.NotEmpty(fooType.Fields["id"].Directives["foo"]);
        }
Example #4
0
        public void DeclareUnion_MarkerInterfaceAndTypeSet()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            schemaContext.Types.RegisterType(new FooType());
            schemaContext.Types.RegisterType(new BarType());

            // act
            var fooType = new UnionType <IFooOrBar>(c => c.Type <BazType>());

            // assert
            schemaContext.Types.RegisterType(fooType);
            INeedsInitialization init = fooType;
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            init.RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            Assert.Empty(errors);
            Assert.Collection(fooType.Types.Values,
                              t => Assert.Equal("Baz", t.Name),
                              t => Assert.Equal("Foo", t.Name),
                              t => Assert.Equal("Bar", t.Name));
        }
Example #5
0
        public void UnionType_AddDirectives_DirectiveClassInstance()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            schemaContext.Directives.RegisterDirectiveType <FooDirectiveType>();

            // act
            var fooType = new UnionType(
                d => d.Directive(new FooDirective())
                .Type <FooType>()
                .Type <BarType>());

            // assert
            schemaContext.Types.RegisterType(fooType);
            INeedsInitialization init = fooType;
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            init.RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            Assert.Empty(errors);
            Assert.NotEmpty(fooType.Directives["foo"]);
        }
Example #6
0
        public void DeclareUnion_ByProvidingExplicitTypeSet()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            // act
            var fooType = new UnionType(d => d
                                        .Type <FooType>()
                                        .Type <BarType>());

            // assert
            schemaContext.Types.RegisterType(fooType);
            INeedsInitialization init = fooType;
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            init.RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            Assert.Empty(errors);
            Assert.Collection(fooType.Types.Values,
                              t => Assert.Equal("Foo", t.Name),
                              t => Assert.Equal("Bar", t.Name));
        }
Example #7
0
        public void CreateObjectType()
        {
            // arrange
            var scalarType = new StringType();

            var          parser   = new Parser();
            DocumentNode document = parser.Parse(
                "type Simple { a: String b: [String] }");
            ObjectTypeDefinitionNode objectTypeDefinition = document
                                                            .Definitions.OfType <ObjectTypeDefinitionNode>().First();
            var resolverBinding = new DelegateResolverBinding(
                "Simple", "a",
                (c, r) => "hello");

            var serviceManager = new ServiceManager();
            var schemaContext  = new SchemaContext(serviceManager);

            schemaContext.Types.RegisterType(scalarType);
            schemaContext.Resolvers.RegisterResolver(resolverBinding);

            // act
            var        factory    = new ObjectTypeFactory();
            ObjectType objectType = factory.Create(objectTypeDefinition);

            schemaContext.Types.RegisterType(objectType);

            var initializationContext = new TypeInitializationContext(
                schemaContext, error => { }, objectType, false);

            ((INeedsInitialization)objectType)
            .RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            // assert
            Assert.Equal("Simple", objectType.Name);
            Assert.Equal(3, objectType.Fields.Count);
            Assert.True(objectType.Fields.ContainsField("a"));
            Assert.True(objectType.Fields.ContainsField("b"));
            Assert.False(objectType.Fields["a"].Type.IsNonNullType());
            Assert.False(objectType.Fields["a"].Type.IsListType());
            Assert.True(objectType.Fields["a"].Type.IsScalarType());
            Assert.Equal("String", objectType.Fields["a"].Type.TypeName());
            Assert.False(objectType.Fields["b"].Type.IsNonNullType());
            Assert.True(objectType.Fields["b"].Type.IsListType());
            Assert.False(objectType.Fields["b"].Type.IsScalarType());
            Assert.Equal("String", objectType.Fields["b"].Type.TypeName());
            Assert.Equal("hello", (objectType.Fields["a"]
                                   .Resolver(null, CancellationToken.None)));
        }
Example #8
0
        public void InferFieldsFromClrInterface()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            schemaContext.Types.RegisterType(new BooleanType());
            schemaContext.Types.RegisterType(new StringType());
            schemaContext.Types.RegisterType(new IntType());

            // act
            var fooType = new InterfaceType <IFoo>();

            schemaContext.Types.RegisterType(fooType);

            INeedsInitialization init = fooType;
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            init.RegisterDependencies(initializationContext);

            schemaContext.CompleteTypes();

            // assert
            Assert.Empty(errors);
            Assert.Collection(
                fooType.Fields.Where(t => !t.IsIntrospectionField),
                t =>
            {
                Assert.Equal("bar", t.Name);
                Assert.IsType <BooleanType>(
                    Assert.IsType <NonNullType>(t.Type).Type);
            },
                t =>
            {
                Assert.Equal("baz", t.Name);
                Assert.IsType <StringType>(t.Type);
            },
                t =>
            {
                Assert.Equal("qux", t.Name);
                Assert.IsType <IntType>(
                    Assert.IsType <NonNullType>(t.Type).Type);
                Assert.Collection(t.Arguments,
                                  a => Assert.Equal("a", a.Name));
            });
        }
        public void GenericObjectTypes()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            // act
            var genericType           = new ObjectType <GenericFoo <string> >();
            INeedsInitialization init = genericType;

            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), genericType, false);

            init.RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();
            // assert
            Assert.Equal("GenericFooOfString", genericType.Name);
        }
        public void IntializeImpicitFieldWithImplicitResolver()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            // act
            var fooType = new ObjectType <Foo>();
            INeedsInitialization init = fooType;

            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            init.RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            // assert
            Assert.Empty(errors);
            Assert.NotNull(fooType.Fields.First().Resolver);
        }
Example #11
0
        public async Task FieldMiddlewareIsIntegrated()
        {
            // arrange
            var resolverContext = new Mock <IMiddlewareContext>();

            resolverContext.SetupAllProperties();

            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();
            var stringType    = new StringType();

            schemaContext.Types.RegisterType(stringType);
            schemaContext.Resolvers.RegisterMiddleware(next => async context =>
            {
                await next(context);

                if (context.Result is string s)
                {
                    context.Result = s.ToUpperInvariant();
                }
            });

            // act
            var fooType = new ObjectType(c =>
                                         c.Name("Foo").Field("bar").Resolver(() => "baz"));

            // assert
            schemaContext.Types.RegisterType(fooType);
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            ((INeedsInitialization)fooType)
            .RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            Assert.Empty(errors);

            await fooType.Fields["bar"].Middleware(resolverContext.Object);

            Assert.Equal("BAZ", resolverContext.Object.Result);
        }
Example #12
0
        private void CompleteType(
            INamedType namedType,
            Action <SchemaContext> configure = null)
        {
            var schemaContext = new SchemaContext();

            schemaContext.Types.RegisterType(new StringType());
            schemaContext.Types.RegisterType(namedType);

            if (configure != null)
            {
                configure(schemaContext);
            }

            var initializationContext = new TypeInitializationContext(
                schemaContext, error => { }, namedType, false);

            ((INeedsInitialization)namedType)
            .RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();
        }
        public void Initialize_IgnoreProperty_PropertyIsNotInSchemaType()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            // act
            var fooType = new InputObjectType <SimpleInput>(
                d => d.Field(f => f.Id).Ignore());
            INeedsInitialization init = fooType;

            // assert
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            init.RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            Assert.Empty(errors);
            Assert.Collection(fooType.Fields,
                              t => Assert.Equal("name", t.Name));
        }
Example #14
0
        public void IntializeExplicitFieldWithImplicitResolver()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            // act
            var nodeInterface = new NodeType();

            // assert
            INeedsInitialization init = nodeInterface;
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), nodeInterface, false);

            schemaContext.Types.RegisterType(nodeInterface);
            init.RegisterDependencies(initializationContext);
            schemaContext.CompleteTypes();

            Assert.Empty(errors);

            Assert.Equal(
                "Node",
                nodeInterface.Name);

            Assert.Equal(
                "The node interface is implemented by entities that have " +
                "a gloabl unique identifier.",
                nodeInterface.Description);

            Assert.Collection(nodeInterface.Fields,
                              t =>
            {
                Assert.Equal("id", t.Name);
                Assert.IsType <IdType>(
                    Assert.IsType <NonNullType>(t.Type).Type);
            });
        }
Example #15
0
        public void ExplicitInterfaceFieldDeclaration()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            schemaContext.Types.RegisterType(new BooleanType());
            schemaContext.Types.RegisterType(new StringType());
            schemaContext.Types.RegisterType(new IntType());

            // act
            var fooType = new InterfaceType <IFoo>(t =>
                                                   t.BindFields(BindingBehavior.Explicit)
                                                   .Field(p => p.Bar));

            schemaContext.Types.RegisterType(fooType);

            INeedsInitialization init = fooType;
            var initializationContext = new TypeInitializationContext(
                schemaContext, a => errors.Add(a), fooType, false);

            init.RegisterDependencies(initializationContext);

            schemaContext.CompleteTypes();

            // assert
            Assert.Empty(errors);
            Assert.Collection(
                fooType.Fields.Where(t => !t.IsIntrospectionField),
                t =>
            {
                Assert.Equal("bar", t.Name);
                Assert.IsType <BooleanType>(
                    Assert.IsType <NonNullType>(t.Type).Type);
            });
        }