Ejemplo n.º 1
0
        public void ApiGenerationTest()
        {
            var system = ActorSystem.Create("test");
            var api    = new ApiProvider(system, new TestRepository());

            foreach (var error in api.GenerationErrors)
            {
                this.output.WriteLine($"Error: {error}");
            }

            Assert.Equal(0, api.GenerationErrors.Count);

            var webApiProvider = new DirectProvider(api, this.output.WriteLine)
            {
                UseJsonRepack = true
            };
            var schema = SchemaGenerator.Generate(new List <Web.GraphQL.Publisher.ApiProvider> {
                webApiProvider
            });

            var hasSchemaErrors = false;

            foreach (var error in SchemaGenerator.CheckSchema(schema))
            {
                hasSchemaErrors = true;
                this.output.WriteLine($"Schema error: {error}");
            }

            using (var printer = new SchemaPrinter(schema))
            {
                var description = printer.Print();
                this.output.WriteLine("-------- Schema -----------");
                this.output.WriteLine(description);
                Assert.False(string.IsNullOrWhiteSpace(description));
            }

            Assert.False(hasSchemaErrors);

            hasSchemaErrors = false;
            foreach (var error in SchemaGenerator.CheckSchemaIntrospection(schema))
            {
                hasSchemaErrors = true;
                this.output.WriteLine($"Schema introspection error: {error}");
            }

            Assert.False(hasSchemaErrors);
        }
        public async Task SchemaDescriptionTest()
        {
            var checkAttributeArguments = new[]
            {
                ApiField.Scalar(
                    "attribute",
                    EnScalarType.String,
                    description: "attribute to check")
            };

            var objectFields = new[]
            {
                ApiField.Scalar(
                    "uid",
                    EnScalarType.Guid,
                    description: "The object unique identifier"),
                ApiField.Scalar("name", EnScalarType.String, description: "The object name"),
                ApiField.Scalar(
                    "attributes",
                    EnScalarType.String,
                    EnFieldFlags.IsArray,
                    description: "The object attributes"),
                ApiField.Scalar(
                    "checkAttribute",
                    EnScalarType.Boolean,
                    arguments: checkAttributeArguments,
                    description: "checks the attribute")
            };

            var objectType = new ApiObjectType("object", objectFields)
            {
                Description = "Some abstract object"
            };
            var apiField = objectType.CreateField(
                "new",
                description: "The new object data");

            var mutations = new[]
            {
                ApiMutation.CreateFromField(
                    ApiField.Object(
                        "objects_create",
                        "object",
                        arguments: new[] { apiField },
                        description: "creates a new object"),
                    ApiMutation.EnType.ConnectionCreate,
                    new List <ApiRequest>())
            };

            var api = new ApiDescription(
                "TestApi",
                "0.0.0.1",
                new[] { objectType },
                new[] { objectType.CreateField("objects", EnFieldFlags.IsConnection, "The objects dataset") },
                mutations)
            {
                Description = "The test api"
            };

            var provider = new MoqProvider {
                Description = api
            };
            var schema = SchemaGenerator.Generate(new List <ApiProvider> {
                provider
            });

            var errors = SchemaGenerator.CheckSchema(schema).Select(e => $"Schema type error: {e}")
                         .Union(SchemaGenerator.CheckSchemaIntrospection(schema))
                         .Select(e => $"Schema introspection error: {e}");

            var hasErrors = false;

            foreach (var error in errors)
            {
                hasErrors = true;
                this.output.WriteLine(error);
            }

            using (var printer = new SchemaPrinter(schema))
            {
                var description = printer.Print();
                this.output.WriteLine("-------- Schema -----------");
                this.output.WriteLine(description);
                Assert.False(string.IsNullOrWhiteSpace(description));
            }

            Assert.False(hasErrors);
            var query = BaseInstaller.ReadTextResource(
                this.GetType().GetTypeInfo().Assembly,
                "KlusterKite.Web.Tests.GraphQL.Resources.IntrospectionQuery.txt");

            var result = await new DocumentExecuter().ExecuteAsync(
                r =>
            {
                r.Schema = schema;

                r.Query = query;
            }).ConfigureAwait(true);
            var response = new DocumentWriter(true).Write(result);

            this.output.WriteLine(response);

            var expectedResponse = BaseInstaller.ReadTextResource(
                this.GetType().GetTypeInfo().Assembly,
                "KlusterKite.Web.Tests.GraphQL.Resources.SchemaDescriptionTestSnapshot.txt");

            Assert.Equal(CleanResponse(expectedResponse), CleanResponse(response));
        }