Esempio n. 1
0
        public void Parse_Kitchen_Sink_Query_No_Cache()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(
                    new GraphQLRequestDto
            {
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks()
            }).NormalizeLineBreaks());

            // act
            var parserOptions = new ParserOptions();
            var requestParser = new Utf8GraphQLRequestParser(
                source, parserOptions);
            IReadOnlyList <GraphQLRequest> batch = requestParser.Parse();

            // assert
            Assert.Collection(batch,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.QueryName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
Esempio n. 2
0
        public void Parse_Kitchen_Sink_Query_With_Russion_Escaped_Characters()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                FileResource.Open("russion_utf8_escape_characters.json")
                .NormalizeLineBreaks());

            // act
            var parserOptions = new ParserOptions();
            var requestParser = new Utf8GraphQLRequestParser(
                source, parserOptions);
            IReadOnlyList <GraphQLRequest> batch = requestParser.Parse();

            // assert
            Assert.Collection(batch,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.QueryName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
Esempio n. 3
0
 public override string?ToString()
 {
     if (_stringValue is null)
     {
         _stringValue = QuerySyntaxSerializer.Serialize(this, true);
     }
     return(_stringValue);
 }
        public void Parse_Kitchen_Sink_Query_With_Cache()
        {
            // arrange
            var request = new GraphQLRequestDto
            {
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks()
            };

            byte[] buffer       = Encoding.UTF8.GetBytes(request.Query);
            string expectedHash = Convert.ToBase64String(
                SHA1.Create().ComputeHash(buffer));

            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(request
                                            ).NormalizeLineBreaks());

            var cache = new DocumentCache();

            var requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            IReadOnlyList <GraphQLRequest> first = requestParser.Parse();

            cache.TryAddDocument(first[0].QueryName, first[0].Query);

            // act
            requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            IReadOnlyList <GraphQLRequest> second = requestParser.Parse();

            // assert
            Assert.Equal(first[0].Query, second[0].Query);
            Assert.Collection(second,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                Assert.Equal(expectedHash, r.QueryName);
                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
Esempio n. 5
0
        public void ParseFacebookKitchenSinkQuery()
        {
            // arrange
            string querySource = FileResource.Open("kitchen-sink.graphql");
            var    parser      = new Utf8GraphQLParser(
                Encoding.UTF8.GetBytes(querySource));

            // act
            DocumentNode document = parser.Parse();

            // assert
            QuerySyntaxSerializer.Serialize(document)
            .MatchSnapshot(new SnapshotNameExtension("sdl"));
            document.MatchSnapshot();
        }
Esempio n. 6
0
        public void Parse_Kitchen_Sink_Query_With_Cache()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(
                    new GraphQLRequestDto
            {
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks()
            }).NormalizeLineBreaks());

            var cache = new DocumentCache();

            var requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            IReadOnlyList <GraphQLRequest> first = requestParser.Parse();

            cache.Add(first[0].QueryName, first[0].Query);

            // act
            requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            IReadOnlyList <GraphQLRequest> second = requestParser.Parse();

            // assert
            Assert.Equal(first[0].Query, second[0].Query);
            Assert.Collection(second,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                Assert.Equal("KwPz8bJWrVDRrtFPjW2sh5CUQwE=", r.QueryName);
                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
        public void Parse_Id_As_Name()
        {
            // arrange
            var request = new RelayGraphQLRequestDto
            {
                Id    = "FooBar",
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks()
            };

            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(request
                                            ).NormalizeLineBreaks());

            byte[] buffer       = Encoding.UTF8.GetBytes(request.Query);
            string expectedHash = Convert.ToBase64String(
                SHA1.Create().ComputeHash(buffer));

            var cache = new DocumentCache();

            var requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            // act
            IReadOnlyList <GraphQLRequest> result = requestParser.Parse();

            // assert
            Assert.Collection(result,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                Assert.Equal("FooBar", r.QueryName);
                Assert.Equal(expectedHash, r.QueryHash);
                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
        public void Parse_Id_As_Name()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(
                    new RelayGraphQLRequestDto
            {
                Id    = "FooBar",
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks()
            }).NormalizeLineBreaks());

            var cache = new DocumentCache();

            var requestParser = new Utf8GraphQLRequestParser(
                source,
                new ParserOptions(),
                cache,
                new Sha1DocumentHashProvider());

            // act
            IReadOnlyList <GraphQLRequest> result = requestParser.Parse();

            // assert
            Assert.Collection(result,
                              r =>
            {
                Assert.Null(r.OperationName);
                Assert.Null(r.Variables);
                Assert.Null(r.Extensions);

                Assert.Equal("FooBar", r.QueryName);
                Assert.Equal("KwPz8bJWrVDRrtFPjW2sh5CUQwE=", r.QueryHash);
                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot();
            });
        }
        public void QueryWithComments()
        {
            // arrange
            byte[] sourceText = Encoding.UTF8.GetBytes(
                @"{
                hero {
                    name
                    # Queries can have comments!
                    friends(a:""foo"" b: 123456 c:null d:     true) {
                        name
                    }
                }
            }".NormalizeLineBreaks());

            // act
            var parser = new Utf8GraphQLParser(
                sourceText, ParserOptions.Default);
            DocumentNode document = parser.Parse();

            // assert
            document.MatchSnapshot();
            QuerySyntaxSerializer.Serialize(document)
            .MatchSnapshot(new SnapshotNameExtension("serialized"));
        }
Esempio n. 10
0
        public void Parse_Kitchen_Sink_Query_AllProps_No_Cache()
        {
            // arrange
            byte[] source = Encoding.UTF8.GetBytes(
                JsonConvert.SerializeObject(
                    new GraphQLRequestDto
            {
                Query = FileResource.Open("kitchen-sink.graphql")
                        .NormalizeLineBreaks(),
                NamedQuery    = "ABC",
                OperationName = "DEF",
                Variables     = new Dictionary <string, object>
                {
                    { "a", "b" },
                    { "b", new Dictionary <string, object>
                      {
                          { "a", "b" },
                          { "b", true },
                          { "c", 1 },
                          { "d", 1.1 },
                      } },
                    { "c", new List <object>
                      {
                          new Dictionary <string, object>
                          {
                              { "a", "b" },
                          }
                      } },
                },
                Extensions = new Dictionary <string, object>
                {
                    { "aa", "bb" },
                    { "bb", new Dictionary <string, object>
                      {
                          { "aa", "bb" },
                          { "bb", true },
                          { "cc", 1 },
                          { "df", 1.1 },
                      } },
                    { "cc", new List <object>
                      {
                          new Dictionary <string, object>
                          {
                              { "aa", "bb" },
                              { "ab", null },
                              { "ac", false },
                          }
                      } },
                }
            }).NormalizeLineBreaks());

            // act
            var parserOptions = new ParserOptions();
            var requestParser = new Utf8GraphQLRequestParser(
                source, parserOptions);
            IReadOnlyList <GraphQLRequest> batch = requestParser.Parse();

            // assert
            Assert.Collection(batch,
                              r =>
            {
                Assert.Equal("ABC", r.QueryName);
                Assert.Equal("DEF", r.OperationName);

                r.Variables.MatchSnapshot(
                    new SnapshotNameExtension("Variables"));
                r.Extensions.MatchSnapshot(
                    new SnapshotNameExtension("Extensions"));
                QuerySyntaxSerializer.Serialize(r.Query, true)
                .MatchSnapshot(new SnapshotNameExtension("Query"));
            });
        }