Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
        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());
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
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"]);
        }
Exemplo n.º 5
0
        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"]);
        }
Exemplo n.º 6
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"]);
        }
Exemplo n.º 7
0
        public void CheckFieldsAreCorrect()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            // act
            var type = new ConnectionType <StringType>();

            // assert
            INeedsInitialization init = type;

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

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

            Assert.Collection(type.Fields.Where(t => !t.IsIntrospectionField),
                              t =>
            {
                Assert.Equal("pageInfo", t.Name);
                Assert.IsType <NonNullType>(t.Type);
                Assert.IsType <PageInfoType>(((NonNullType)t.Type).Type);
            },
                              t =>
            {
                Assert.Equal("edges", t.Name);
                Assert.IsType <ListType>(t.Type);
                Assert.IsType <NonNullType>(((ListType)t.Type).ElementType);
                Assert.IsType <EdgeType <StringType> >(
                    ((NonNullType)((ListType)t.Type).ElementType).Type);
            });
        }
        public void CheckFieldsAreCorrect()
        {
            // arrange
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            // act
            var type = new PageInfoType();

            // assert
            INeedsInitialization init = type;

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

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

            Assert.Collection(type.Fields.Where(t => !t.IsIntrospectionField),
                              t =>
            {
                Assert.Equal("hasNextPage", t.Name);
                Assert.Equal(
                    "Indicates whether more edges exist following " +
                    "the set defined by the clients arguments.",
                    t.Description);
                Assert.IsType <NonNullType>(t.Type);
                Assert.IsType <BooleanType>(((NonNullType)t.Type).Type);
            },
                              t =>
            {
                Assert.Equal("hasPreviousPage", t.Name);
                Assert.Equal(
                    "Indicates whether more edges exist prior " +
                    "the set defined by the clients arguments.",
                    t.Description);
                Assert.IsType <NonNullType>(t.Type);
                Assert.IsType <BooleanType>(((NonNullType)t.Type).Type);
            },
                              t =>
            {
                Assert.Equal("startCursor", t.Name);
                Assert.IsType <StringType>(t.Type);
            },
                              t =>
            {
                Assert.Equal("endCursor", t.Name);
                Assert.IsType <StringType>(t.Type);
            });
        }
Exemplo n.º 9
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)));
        }
Exemplo n.º 10
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));
            });
        }
Exemplo n.º 11
0
        public void IntializeImpicitFieldWithImplicitResolver()
        {
            // arrange
            ServiceManager     services = new ServiceManager();
            List <SchemaError> errors   = new List <SchemaError>();
            SchemaContext      context  = new SchemaContext(services);

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

            ((INeedsInitialization)fooType).RegisterDependencies(
                context, e => errors.Add(e));
            context.CompleteTypes();

            // assert
            Assert.Empty(errors);
            Assert.NotNull(fooType.Fields.Values.First().Resolver);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        public void EnsureObjectTypeKindIsCorret()
        {
            // arrange
            ServiceManager     services = new ServiceManager();
            List <SchemaError> errors   = new List <SchemaError>();
            SchemaContext      context  = new SchemaContext(services);

            ObjectType <Foo> someObject = new ObjectType <Foo>(
                d => d.Field(f => f.Description).Name("a"));

            ((INeedsInitialization)someObject).RegisterDependencies(
                context, e => errors.Add(e));
            context.CompleteTypes();

            // act
            TypeKind kind = someObject.Kind;

            // assert
            Assert.Equal(TypeKind.Object, kind);
        }
Exemplo n.º 15
0
        internal static TypeResult <T> CreateType <T>(
            Func <ISchemaContext, T> factory)
            where T : class, INamedType, INeedsInitialization
        {
            var errors        = new List <SchemaError>();
            var schemaContext = new SchemaContext();

            var type = factory(schemaContext);

            schemaContext.Types.RegisterType(type);

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

            type.RegisterDependencies(initializationContext);

            schemaContext.CompleteTypes();

            return(new TypeResult <T>(type, errors));
        }
Exemplo n.º 16
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);
        }
Exemplo n.º 17
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));
        }
Exemplo n.º 19
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);
            });
        }
Exemplo n.º 20
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);
            });
        }
Exemplo n.º 21
0
        private static Schema CreateSchema(
            IServiceProvider services,
            SchemaContext context,
            SchemaNames names,
            Action <ISchemaConfiguration> configure,
            bool strict)
        {
            List <SchemaError> errors = new List <SchemaError>();

            // setup introspection fields
            IntrospectionFields introspectionFields =
                new IntrospectionFields(context, e => errors.Add(e));

            SchemaNames internalNames = names;

            try
            {
                // configure resolvers, custom types and type mappings.
                SchemaConfiguration configuration = new SchemaConfiguration(services);
                configure(configuration);
                errors.AddRange(configuration.RegisterTypes(context));
                configuration.RegisterResolvers(context);
                errors.AddRange(context.CompleteTypes());

                string queryTypeName        = configuration.QueryTypeName ?? names.QueryTypeName;
                string mutationTypeName     = configuration.MutationTypeName ?? names.MutationTypeName;
                string subscriptionTypeName = configuration.SubscriptionTypeName ?? names.SubscriptionTypeName;

                internalNames = new SchemaNames(queryTypeName, mutationTypeName, subscriptionTypeName);
            }
            catch (ArgumentException ex)
            {
                // TODO : maybe we should throw a more specific
                // argument exception that at least contains the config object.
                throw new SchemaException(new[]
                {
                    new SchemaError(ex.Message, null)
                });
            }

            if (strict && errors.Any())
            {
                throw new SchemaException(errors);
            }

            internalNames = string.IsNullOrEmpty(names.QueryTypeName)
                ? new SchemaNames(null, null, null)
                : names;

            if (strict && !context.Types.TryGetType <ObjectType>(
                    internalNames.QueryTypeName, out ObjectType ot))
            {
                throw new SchemaException(new SchemaError(
                                              "Schema is missing the mandatory `Query` type."));
            }

            return(new Schema(
                       services,
                       SchemaTypes.Create(
                           context.Types.GetTypes(),
                           context.Types.GetTypeBindings(),
                           internalNames),
                       introspectionFields));
        }