public void VariablesAreCoercedToTypesOtherThanTheDefinedClrTypes()
        {
            // arrange
            ISchema schema    = Schema.Create(c => c.RegisterQueryType <Query>());
            var     variables = new Dictionary <string, object>
            {
                {
                    "a",
                    new Dictionary <string, object>
                    {
                        { "id", "934b987bc0d842bbabfd8a3b3f8b476e" },
                        { "time", "2018-05-29T01:00Z" },
                        { "number", "123" }
                    }
                }
            };

            // act
            IExecutionResult result = schema.Execute(@"
                query foo($a: FooInput) {
                    foo(foo: $a) {
                        id
                        time
                        number
                    }
                }", variables);

            // assert
            result.Snapshot();
        }
Exemple #2
0
        protected async override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            //try get query from body, could check http method :)
            var query = await request.Content.ReadAsStringAsync();

            //if not body try query string, dont hack like this in real world..
            if (query.Length == 0)
            {
                var decoded = WebUtility.UrlDecode(request.RequestUri.Query);
                query = decoded.Replace("?query=", "");
            }

            var result = _schema.Execute(_ =>
            {
                _.Query = query;
            });

            //maybe check for errors and headers etc in real world?
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(result)
            };

            //ocelot will treat this like any other http request...
            return(response);
        }
        public void VariableIsPartlyNotSerializedAndMustBeConvertedToClrType()
        {
            // arrange
            ISchema schema    = Schema.Create(c => c.RegisterQueryType <Query>());
            var     variables = new Dictionary <string, object>
            {
                {
                    "a",
                    new Dictionary <string, object>
                    {
                        { "id", "934b987bc0d842bbabfd8a3b3f8b476e" },
                        { "time", "2018-05-29T01:00Z" },
                        { "number", 123 }
                    }
                }
            };

            // act
            IExecutionResult result = schema.Execute(@"
                query foo($a: FooInput) {
                    foo(foo: $a) {
                        id
                        time
                        number
                    }
                }", variables);

            // assert
            result.Snapshot();
        }
Exemple #4
0
        public async Task <ActionResult> Get(string query)
        {
            var result = _schema.Execute(conf =>
            {
                //conf.Query = "{ film(film_id: 133) { film_id title } }";
                //conf.Query = "{ films { film_id title } }";
                conf.Query = query;
            });

            return(Ok(result));
        }
Exemple #5
0
        public void NameIsNotNull()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result = schema.Execute("{ sayHello }");

            // assert
            result.Snapshot();
        }
Exemple #6
0
        public void NameIsNotNull()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result = schema.Execute("{ sayHello }");

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
Exemple #7
0
        public void UniqueDirectives_OnFieldLevel_OverwriteOnesOnObjectLevel()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result = schema.Execute("{ bar baz }");

            // assert
            result.Snapshot();
        }
Exemple #8
0
        public void UniqueDirectives_FieldSelection_OverwriteTypeSystemOnes()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result = schema.Execute(
                "{ bar baz @constant(value: \"baz\") }");

            // assert
            result.Snapshot();
        }
        public void SingleMethodMiddleware()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result = schema.Execute(
                "{ sayHello @resolve @appendStringMethod(s: \"abc\") }");

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
        public void DirectivesOnObjectType()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result =
                schema.Execute("{ person { phone } }");

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
        public void DirectivesOnFieldSelection()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result =
                schema.Execute("{ person { name @c(append:\"Baz\") } }");

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
Exemple #12
0
        public string Index([FromBody] GraphQLRequest request)
        {
            var result = _schema.Execute(_ =>
            {
                _.Query            = request.Query;
                _.OperationName    = request.OperationName;
                _.Inputs           = request.Variables.ToInputs();
                _.ExposeExceptions = true;
            });

            return(result);
        }
Exemple #13
0
        public void DirectivesOnObjectType()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result =
                schema.Execute("{ person { phone } }");

            // assert
            result.Snapshot();
        }
        public void SingleAsyncMethodMiddleware()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result = schema.Execute(
                "{ sayHello @resolve @appendStringMethodAsync(s: \"abc\") }");

            // assert
            result.Snapshot();
        }
Exemple #15
0
        public void DirectivesOnFieldSelection()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result =
                schema.Execute("{ person { name @c(append:\"Baz\") } }");

            // assert
            result.Snapshot();
        }
Exemple #16
0
        public void DirectivesOnFieldDefinition()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result =
                schema.Execute("{ person { name } }");

            // assert
            result.Snapshot();
        }
Exemple #17
0
        public void SchemaFirst_AuthorsTest_GetAllAuthors()
        {
            ISchema schema = PubsSchema.GetSchema();

            string query = $" {{ authors {{ authorId lastName firstName phone address city state zip contract }} }}";
            string json  = schema.Execute(_ =>
            {
                //_.Query = query;
                _.Query =
                    @"query AuthorsQuery 
                    {
                        authors
                        { 
                            authorId lastName firstName phone address city state zip contract 
                            titles
                            {
                                titleId title price pubId type pubDate notes advance royalty ytdSales 
                            }
                        } 
                    }";
            });

            var d = JsonConvert.DeserializeObject <dynamic>(json);

            Assert.IsNull(d.errors);
            json = JsonConvert.SerializeObject(d.data.authors);
            Author[] authors = JsonConvert.DeserializeObject <Author[]>(json);

            foreach (Author expected in PubsData.Authors)
            {
                Author author = authors.FirstOrDefault(a => a.AuthorId == expected.AuthorId);
                Assert.IsNotNull(author);

                Assert.AreEqual(expected.AuthorId, author.AuthorId);
                Assert.AreEqual(expected.LastName, author.LastName);
                Assert.AreEqual(expected.FirstName, author.FirstName);
                Assert.AreEqual(expected.Phone, author.Phone);
                Assert.AreEqual(expected.Address, author.Address);
                Assert.AreEqual(expected.City, author.City);
                Assert.AreEqual(expected.State, author.State);
                Assert.AreEqual(expected.Zip, author.Zip);
                Assert.AreEqual(expected.Contract, author.Contract);
            }

            foreach (Author author in authors)
            {
                Author expected = PubsData.Authors.FirstOrDefault(a => a.AuthorId == author.AuthorId);
                Assert.IsNotNull(expected);
            }
        }
Exemple #18
0
        public void SchemaFirst_PublisherTest_GetSinglePublisher()
        {
            string publisherId = "0736";

            ISchema schema = PubsSchema.GetSchema();

            // The id parameter here has to match the id parameter in the C# GetAuthor method.
            string json = schema.Execute(_ =>
            {
                //_.Query = $" {{ publisher(id: \"{publisherId}\") {{ pubId name city state country }} }}";
                _.Query =
                    @"  query PublisherQuery
                        {
                            publisher(id: """ + publisherId + @""")
                            {
                                pubId name city state country
                                titles
                                {
                                    titleId title type pubDate price notes pubId advance royalty ytdSales
                                }
                            }
                        }";
            });

            var d = JsonConvert.DeserializeObject <dynamic>(json);

            Assert.IsNull(d.errors);

            json = JsonConvert.SerializeObject(d.data.publisher);
            Publisher publisher = JsonConvert.DeserializeObject <Publisher>(json);

            Assert.IsNotNull(publisher);

            Publisher expectedPublisher = PubsData.Publishers.FirstOrDefault(p => p.PubId == publisherId);

            Assert.IsNotNull(expectedPublisher);

            Assert.AreEqual(expectedPublisher.PubId, (string)d.data.publisher.pubId);
            Assert.AreEqual(expectedPublisher.Name, (string)d.data.publisher.name);
            Assert.AreEqual(expectedPublisher.City, (string)d.data.publisher.city);
            Assert.AreEqual(expectedPublisher.State, (string)d.data.publisher.state);
            Assert.AreEqual(expectedPublisher.Country, (string)d.data.publisher.country);

            Assert.AreEqual(expectedPublisher.PubId, publisher.PubId);
            Assert.AreEqual(expectedPublisher.Name, publisher.Name);
            Assert.AreEqual(expectedPublisher.City, publisher.City);
            Assert.AreEqual(expectedPublisher.State, publisher.State);
            Assert.AreEqual(expectedPublisher.Country, publisher.Country);
        }
Exemple #19
0
        public void SchemaFirst_TitlesTest_GetAllTitles()
        {
            ISchema schema = PubsSchema.GetSchema();

            string json = schema.Execute(_ =>
            {
                //_.Query = $" {{ titles {{ titleId title pubId type pubDate notes price advance royalty ytdSales }} }}";
                _.Query =
                    @"query TitleQuery 
                    {
                        titles
                        { 
                            titleId title price pubId type pubDate notes advance royalty ytdSales 
                            authors { authorId lastName firstName phone address city state zip contract }
                            publisher { pubId name city state country }
                        } 
                    }";
            });

            var d = JsonConvert.DeserializeObject <dynamic>(json);

            Assert.IsNull(d.errors);

            json = JsonConvert.SerializeObject(d.data.titles);
            Title[] titles = JsonConvert.DeserializeObject <Title[]>(json);

            foreach (Title expected in PubsData.Titles)
            {
                Title title = titles.FirstOrDefault(a => a.TitleId == expected.TitleId);
                Assert.IsNotNull(title);

                Assert.AreEqual(expected.TitleId, title.TitleId);
                Assert.AreEqual(expected.title, title.title);
                Assert.AreEqual(expected.Price, title.Price);
                Assert.AreEqual(expected.PubId, title.PubId);
                Assert.AreEqual(expected.Type, title.Type);
                Assert.AreEqual(expected.PubDate, title.PubDate);
                Assert.AreEqual(expected.Notes, title.Notes);
                Assert.AreEqual(expected.Advance, title.Advance);
                Assert.AreEqual(expected.Royalty, title.Royalty);
                Assert.AreEqual(expected.YTDSales, title.YTDSales);
            }

            foreach (Title title in titles)
            {
                Title expected = PubsData.Titles.FirstOrDefault(a => a.TitleId == title.TitleId);
                Assert.IsNotNull(expected);
            }
        }
        public void MiddlewarePipeline()
        {
            // arrange
            ISchema schema = CreateSchema();

            // act
            IExecutionResult result = schema.Execute(
                "{ sayHello @resolve " +
                "@appendString(s: \"abc\") " +
                "@appendStringMethod(s: \"def\") " +
                "@appendStringMethodAsync(s: \"ghi\") }");

            // assert
            result.Snapshot();
        }
Exemple #21
0
        public void ResultIsCorrectlyPassedIntoTheContext()
        {
            // arrange
            ISchema schema = Schema.Create(
                "type Query { a: String }",
                c =>
            {
                c.RegisterDirective <UpperCaseDirectiveType>();
                c.RegisterDirective <LowerCaseDirectiveType>();
                c.BindResolver(() => "hello").To("Query", "a");
            });

            // act
            IExecutionResult result =
                schema.Execute("{ a @lower @upper }");

            // assert
            result.Snapshot();
        }
        public void VariableIsCoercedToTypesOtherThanTheDefinedClrTypes()
        {
            // arrange
            ISchema schema = Schema.Create(
                c => c.RegisterQueryType <QueryType>());

            var variables = new Dictionary <string, object>
            {
                { "time", "2018-05-29T01:00Z" }
            };

            // act
            IExecutionResult result = schema.Execute(@"
                query foo($time: DateTime) {
                    time(time: $time)
                }", variables);

            // assert
            result.Snapshot();
        }
        public IActionResult ViewDashboard()
        {
            C.WriteLine("DASHBOARD");

            ISchema schema = Schema.For(@"
                type Query {
                    hello: String
                }
            ");

            var    root = new { Hello = "Hello World!" };
            string json = schema.Execute(_ =>
            {
                _.Query = "{ hello }";
                _.Root  = root;
            });

            C.WriteLine(json);

            return(View("Dashboard"));
        }
        public void VariableIsNotSerializedAndMustBeConvertedToClrType()
        {
            // arrange
            ISchema schema = Schema.Create(
                c => c.RegisterQueryType <QueryType>());

            var time = new DateTime(2018, 01, 01, 12, 10, 10, DateTimeKind.Utc);

            var variables = new Dictionary <string, object>
            {
                { "time", time }
            };

            // act
            IExecutionResult result = schema.Execute(@"
                query foo($time: DateTime) {
                    time(time: $time)
                }", variables);

            // assert
            result.Snapshot();
        }
Exemple #25
0
        public void SchemaFirst_TitleTest_GetSingleTitle()
        {
            string titleId = "PC1035";

            ISchema schema = PubsSchema.GetSchema();

            // The id parameter here has to match the id parameter in the C# GetAuthor method.
            string json = schema.Execute(_ =>
            {
                _.Query =
                    @"query TitleQuery 
                    {
                        title(id: """ + titleId + @""") 
                        { 
                            titleId title price pubId type pubDate notes advance royalty ytdSales 
                            authors { authorId lastName firstName phone address city state zip contract }
                            publisher { pubId name city state country }
                        } 
                    }";
            });

            var d = JsonConvert.DeserializeObject <dynamic>(json);

            Assert.IsNull(d.errors);

            Title expectedTitle = PubsData.Titles.FirstOrDefault(t => t.TitleId == titleId);

            Assert.IsNotNull(expectedTitle);

            Assert.AreEqual(expectedTitle.TitleId, (string)d.data.title.titleId);
            Assert.AreEqual(expectedTitle.title, (string)d.data.title.title);
            Assert.AreEqual(expectedTitle.Price, (decimal?)d.data.title.price);
            Assert.AreEqual(expectedTitle.PubId, (string)d.data.title.pubId);
            Assert.AreEqual(expectedTitle.Type, (string)d.data.title.type);
            Assert.AreEqual(ToDate(expectedTitle.PubDate), (string)d.data.title.pubDate);
            Assert.AreEqual(expectedTitle.Notes, (string)d.data.title.notes);
            Assert.AreEqual(expectedTitle.Advance, (decimal?)d.data.title.advance);
            Assert.AreEqual(expectedTitle.Royalty, (int?)d.data.title.royalty);
            Assert.AreEqual(expectedTitle.YTDSales, (int?)d.data.title.ytdSales);

            json = JsonConvert.SerializeObject(d.data.title);
            Title title = JsonConvert.DeserializeObject <Title>(json);

            Assert.AreEqual(expectedTitle.TitleId, title.TitleId);
            Assert.AreEqual(expectedTitle.title, title.title);
            Assert.AreEqual(expectedTitle.Price, title.Price);
            Assert.AreEqual(expectedTitle.PubId, title.PubId);
            Assert.AreEqual(expectedTitle.Type, title.Type);
            Assert.AreEqual(expectedTitle.PubDate, title.PubDate);
            Assert.AreEqual(expectedTitle.Notes, title.Notes);
            Assert.AreEqual(expectedTitle.Advance, title.Advance);
            Assert.AreEqual(expectedTitle.Royalty, title.Royalty);
            Assert.AreEqual(expectedTitle.YTDSales, title.YTDSales);

            Assert.IsNotNull(title.Authors);
            Assert.AreEqual(1, title.Authors.Length);
            Author author = title.Authors[0];

            Assert.IsNotNull(author);
            Author expected = PubsData.Authors.FirstOrDefault(a => a.AuthorId == author.AuthorId);

            Assert.IsNotNull(expected);

            Assert.AreEqual(expected.AuthorId, author.AuthorId);
            Assert.AreEqual(expected.LastName, author.LastName);
            Assert.AreEqual(expected.FirstName, author.FirstName);
            Assert.AreEqual(expected.Phone, author.Phone);
            Assert.AreEqual(expected.Address, author.Address);
            Assert.AreEqual(expected.City, author.City);
            Assert.AreEqual(expected.State, author.State);
            Assert.AreEqual(expected.Zip, author.Zip);
            Assert.AreEqual(expected.Contract, author.Contract);

            Publisher publisher         = title.Publisher;
            Publisher expectedPublisher = PubsData.Publishers.FirstOrDefault(p => p.PubId == title.PubId);

            // Note: there is one title that has no publisher. So the data is bad. But that doesn't mean the unit test should fail.
            Assert.IsNotNull(title.Publisher);
            Assert.IsNotNull(expectedPublisher);
            if (!((publisher == null) && (expectedPublisher == null)))
            {
                Assert.IsNotNull(title.Publisher);
                Assert.IsNotNull(expectedPublisher);

                Assert.AreEqual(expectedPublisher.PubId, publisher.PubId);
                Assert.AreEqual(expectedPublisher.Name, publisher.Name);
                Assert.AreEqual(expectedPublisher.City, publisher.City);
                Assert.AreEqual(expectedPublisher.State, publisher.State);
                Assert.AreEqual(expectedPublisher.Country, publisher.Country);
            }
        }
Exemple #26
0
        public void SchemaFirst_AuthorTest_GetSingleAuthor()
        {
            string authorId = "899-46-2035";

            ISchema schema = PubsSchema.GetSchema();

            // The id parameter here has to match the id parameter in the C# GetAuthor method.
            string json = schema.Execute(_ =>
            {
                //_.Query = query;
                _.Query =
                    @"query AuthorQuery 
                    {
                        author(id: """ + authorId + @""") 
                        { 
                            authorId lastName firstName phone address city state zip contract 
                            titles
                            {
                                titleId title price pubId type pubDate notes advance royalty ytdSales 
                            }
                        } 
                    }";
            });

            var d = JsonConvert.DeserializeObject <dynamic>(json);

            Assert.IsNull(d.errors);

            json = JsonConvert.SerializeObject(d.data.author);
            Author author = JsonConvert.DeserializeObject <Author>(json);

            Author expectedAuthor = PubsData.Authors.FirstOrDefault(a => a.AuthorId == authorId);

            Assert.IsNotNull(expectedAuthor);
            Assert.IsNotNull(author);

            Assert.AreEqual(expectedAuthor.AuthorId, author.AuthorId);
            Assert.AreEqual(expectedAuthor.LastName, author.LastName);
            Assert.AreEqual(expectedAuthor.FirstName, author.FirstName);
            Assert.AreEqual(expectedAuthor.Phone, author.Phone);
            Assert.AreEqual(expectedAuthor.Address, author.Address);
            Assert.AreEqual(expectedAuthor.City, author.City);
            Assert.AreEqual(expectedAuthor.State, author.State);
            Assert.AreEqual(expectedAuthor.Zip, author.Zip);
            Assert.AreEqual(expectedAuthor.Contract, author.Contract);

            Assert.IsNotNull(author.Titles);
            Assert.AreEqual(2, author.Titles.Length);

            string[] titleIds       = PubsData.TitleAuthors.Where(ta => ta.AuthorId == authorId).Select(ta => ta.TitleId).ToArray();
            Title[]  expectedTitles = PubsData.Titles.Where(t => titleIds.Contains(t.TitleId)).ToArray();

            foreach (Title title in author.Titles)
            {
                Title expectedTitle = expectedTitles.FirstOrDefault(et => et.TitleId == title.TitleId);
                Assert.IsNotNull(expectedTitle);
                Assert.AreEqual(expectedTitle.TitleId, title.TitleId);
                Assert.AreEqual(expectedTitle.title, title.title);
                Assert.AreEqual(expectedTitle.Price, title.Price);
                Assert.AreEqual(expectedTitle.PubId, title.PubId);
                Assert.AreEqual(expectedTitle.Type, title.Type);
                Assert.AreEqual(expectedTitle.PubDate, title.PubDate);
                Assert.AreEqual(expectedTitle.Notes, title.Notes);
                Assert.AreEqual(expectedTitle.Advance, title.Advance);
                Assert.AreEqual(expectedTitle.Royalty, title.Royalty);
                Assert.AreEqual(expectedTitle.YTDSales, title.YTDSales);
            }

            foreach (Title expectedTitle in expectedTitles)
            {
                Title title = author.Titles.FirstOrDefault(t => t.TitleId == expectedTitle.TitleId);
                Assert.IsNotNull(title);
            }
        }