コード例 #1
0
        public void Trivial2Test()
        {
            var api = new ApiDescription
            {
                ApiName  = "TestApi1",
                TypeName = "TestApi1",
                Version  = new Version("0.0.0.1"),
            };

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

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

            Assert.NotNull(schema.Query);
            Assert.Equal(3, schema.Query.Fields.Count());
            Assert.True(schema.Query.HasField("api"));
        }
コード例 #2
0
        public async Task EnumApiTest()
        {
            var enumType = new ApiEnumType("EnumType", new[] { "item1", "item2" });

            var api = new ApiDescription(
                "TestApi1",
                "0.0.0.1",
                new ApiType[] { enumType },
                new[] { ApiField.Object("enumField", enumType.TypeName) });

            var provider = new MoqProvider {
                Description = api, Data = "{\"enumField\": \"item2\"}"
            };

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

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

            Assert.NotNull(schema.Query);
            Assert.Equal(3, schema.Query.Fields.Count());
            Assert.True(schema.Query.HasField("api"));

            var result = await new DocumentExecuter().ExecuteAsync(
                r =>
            {
                r.Schema = schema;
                r.Query  = "query { api { enumField } } ";
            }).ConfigureAwait(true);

            this.output.WriteLine("-------- Response -----------");
            var response = new DocumentWriter(true).Write(result);

            this.output.WriteLine(response);

            var expectedResponse = @"{
                                      ""data"": {
                                        ""api"": {
                                            ""enumField"": ""item2"" 
                                        }
                                     }                                      
                                    }";

            Assert.Equal(CleanResponse(expectedResponse), CleanResponse(response));
        }
コード例 #3
0
        public async Task ArraysApiTest()
        {
            var viewerFields = new[]
            {
                ApiField.Scalar("id", EnScalarType.Guid, EnFieldFlags.Queryable | EnFieldFlags.IsKey),
                ApiField.Scalar("name", EnScalarType.String),
                ApiField.Scalar("numbers", EnScalarType.Integer, EnFieldFlags.IsArray | EnFieldFlags.Queryable)
            };
            var viewerType = new ApiObjectType("viewer", viewerFields);

            var objectFields = new[]
            {
                ApiField.Scalar("id", EnScalarType.Integer, EnFieldFlags.IsKey | EnFieldFlags.Queryable),
                ApiField.Scalar("name", EnScalarType.String)
            };

            var objectType = new ApiObjectType("object", objectFields);

            var api = new ApiDescription(
                "TestApi1",
                "0.0.0.1",
                new[] { viewerType, objectType },
                new[] { viewerType.CreateField("viewer"), objectType.CreateField("object", EnFieldFlags.IsConnection | EnFieldFlags.Queryable) });

            var provider = new MoqProvider
            {
                Description = api,
                Data        = @"{
	                    ""viewer"": {
		                    ""__id"": ""FD73BAFB-3698-4FA1-81F5-27C8C83BB4F0"", 
		                    ""name"": ""test name"",
		                    ""numbers"": [1, 2, 3]
	                    }, 
	                    ""object"": {
		                    ""count"": 2, 
		                    ""edges"": [
			                    {""__id"": 10, ""node___id"": 10}, 
			                    {""__id"": 20, ""node___id"": 20}
		                    ]
	                    }
                    }"
            };

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

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

            Assert.NotNull(schema.Query);
            Assert.Equal(3, schema.Query.Fields.Count());
            Assert.True(schema.Query.HasField("api"));

            var result = await new DocumentExecuter().ExecuteAsync(
                r =>
            {
                r.Schema = schema;
                r.Query  = @"
                                query {
                                    api {
                                        viewer {
                                            __id,
                                            name,
                                            numbers
                                        },
                                        object {
                                            count,
                                            edges {
                                                cursor,                                                
                                                node {
                                                    __id
                                                }
                                            }
                                        }
                                    }
                                }            
                                ";
            }).ConfigureAwait(true);

            this.output.WriteLine("-------- Response -----------");
            var response = new DocumentWriter(true).Write(result);

            this.output.WriteLine(response);

            var expectedResponse = @"{
                                      ""data"": {
                                        ""api"": {
                                          ""viewer"": {
                                            ""__id"": ""fd73bafb-3698-4fa1-81f5-27c8c83bb4f0"",
                                            ""name"": ""test name"",
		                                    ""numbers"": [1, 2, 3]
                                          },
                                          ""object"": {
                                            ""count"": 2,
                                            ""edges"": [
                                              {
                                                ""cursor"": 10,
                                                ""node"": {
                                                  ""__id"": 10
                                                }
                                              },
                                              {
                                                ""cursor"": 20,
                                                ""node"": {
                                                  ""__id"": 20
                                                }
                                              }
                                            ]
                                          }
                                        }
                                      }
                                    }";

            Assert.Equal(CleanResponse(expectedResponse), CleanResponse(response));
        }
コード例 #4
0
        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));
        }
コード例 #5
0
        public async Task NonEmptyApiTest()
        {
            var viewerType = new ApiObjectType(
                "viewer",
                new[] { ApiField.Scalar("id", EnScalarType.Integer), ApiField.Scalar("name", EnScalarType.String) });

            var api = new ApiDescription(
                "TestApi1",
                "0.0.0.1",
                new[] { viewerType },
                new[] { viewerType.CreateField("viewer") });

            var provider = new MoqProvider
            {
                Description = api,
                Data        = "{\"viewer\": {\"__id\": 1, \"name\": \"test name\"}}"
            };

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

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

            Assert.NotNull(schema.Query);
            Assert.Equal(3, schema.Query.Fields.Count());
            Assert.True(schema.Query.HasField("api"));

            var result = await new DocumentExecuter().ExecuteAsync(
                r =>
            {
                r.Schema = schema;
                r.Query  = @"
                                query {
                                    api {
                                        viewer {
                                            __id,
                                            name
                                        }
                                    }
                                }            
                                ";
            }).ConfigureAwait(true);

            this.output.WriteLine("-------- Response -----------");
            var response = new DocumentWriter(true).Write(result);

            this.output.WriteLine(response);
            var expectedResponse = @"{
                                      ""data"": {
                                        ""api"": {
                                          ""viewer"": {
                                            ""__id"": 1,
                                            ""name"": ""test name""
                                          }
                                        }
                                      }
                                    }";

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