Example #1
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();
            });
        }
Example #2
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();
            });
        }
Example #3
0
 public override string?ToString()
 {
     if (_stringValue is null)
     {
         _stringValue = QuerySyntaxSerializer.Serialize(this, true);
     }
     return(_stringValue);
 }
Example #4
0
        public static void Serialize(
            ISyntaxNode node,
            TextWriter writer,
            bool useIndentation)
        {
            var serializer = new QuerySyntaxSerializer(useIndentation);

            serializer.Visit(node, new DocumentWriter(writer));
        }
        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();
            });
        }
Example #6
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();
        }
Example #7
0
        public void Serialize_ShortHandQueryWithIndentation_LineBetweenFields()
        {
            // arrange
            string query = "{ foo { foo bar { foo @foo @bar bar @bar baz } } }";

            var serializer = new QuerySyntaxSerializer(true);
            var content    = new StringBuilder();
            var writer     = new StringWriter(content);

            DocumentNode queryDocument = Parser.Default.Parse(query);

            // act
            serializer.Visit(queryDocument, new DocumentWriter(writer));

            // assert
            content.ToString().Snapshot();
        }
Example #8
0
        public void Serialize_KitchenSinkWithoutIndentation_OutputIsOneLine()
        {
            // arrange
            string query = FileResource.Open("kitchen-sink.graphql");

            var serializer = new QuerySyntaxSerializer();
            var content    = new StringBuilder();
            var writer     = new StringWriter(content);

            DocumentNode queryDocument = Parser.Default.Parse(query);

            // act
            serializer.Visit(queryDocument, new DocumentWriter(writer));

            // assert
            content.ToString().Snapshot();
        }
Example #9
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();
            });
        }
Example #10
0
        public void Serialize_ShortHandQueryWithIndentation_OutputIsFormatted()
        {
            // arrange
            string query = "{ foo(s: \"String\") { bar @foo " +
                           "{ baz @foo @bar } } }";

            var serializer = new QuerySyntaxSerializer(true);
            var content    = new StringBuilder();
            var writer     = new StringWriter(content);

            DocumentNode queryDocument = Parser.Default.Parse(query);

            // act
            serializer.Visit(queryDocument, new DocumentWriter(writer));

            // assert
            content.ToString().Snapshot();
        }
        public void Serialize_KitchenSinkWithIndentation_OutputIsFormatted()
        {
            // arrange
            string query = FileResource.Open("kitchen-sink.graphql");

            var serializer = new QuerySyntaxSerializer(true);
            var content    = new StringBuilder();
            var writer     = new StringWriter(content);

            DocumentNode queryDocument = Utf8GraphQLParser.Parse(query);


            // act
            serializer.Visit(queryDocument, new DocumentWriter(writer));

            // assert
            content.ToString().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();
            });
        }
Example #13
0
        public void Serialize_FargmentWithVariableDefs_InOutShouldBeTheSame()
        {
            // arrange
            string query = "fragment Foo ($bar: [String!]!) on Bar { baz }";

            var serializer = new QuerySyntaxSerializer();
            var content    = new StringBuilder();
            var writer     = new StringWriter(content);

            DocumentNode queryDocument = Parser.Default.Parse(query,
                                                              new ParserOptions(allowFragmentVariables: true));

            // act
            serializer.Visit(queryDocument, new DocumentWriter(writer));

            // assert
            Assert.Equal(
                query,
                content.ToString());
        }
Example #14
0
        public void Serialize_ShortHandQueryNoIndentation_InOutShouldBeTheSame()
        {
            // arrange
            var query = "{ foo(s: \"String\") { bar @foo " +
                        "{ baz @foo @bar } } }";

            var serializer = new QuerySyntaxSerializer();
            var content    = new StringBuilder();
            var writer     = new StringWriter(content);

            DocumentNode queryDocument = Parser.Default.Parse(query);

            // act
            serializer.Visit(queryDocument, new DocumentWriter(writer));

            // assert
            Assert.Equal(
                query,
                content.ToString());
        }
Example #15
0
        public void RewriteEveryFieldOfTheQuery()
        {
            // arange
            DocumentNode document = Utf8GraphQLParser.Parse(
                "{ foo { bar { baz } } }");

            // act
            DocumentNode rewritten = document
                                     .Rewrite <DirectiveQuerySyntaxRewriter, DirectiveNode>(
                new DirectiveNode("upper"));

            // assert
            var content        = new StringBuilder();
            var stringWriter   = new StringWriter(content);
            var documentWriter = new DocumentWriter(stringWriter);
            var serializer     = new QuerySyntaxSerializer();

            serializer.Visit(rewritten, documentWriter);
            content.ToString().MatchSnapshot();
        }
Example #16
0
        public void Serialize_QueryWithVarDeclaration_InOutShouldBeTheSame()
        {
            // arrange
            string query =
                "query Foo($bar: [String!]!) { foo(s: \"String\") " +
                "{ bar @foo { baz @foo @bar } } }";

            var serializer = new QuerySyntaxSerializer();
            var content    = new StringBuilder();
            var writer     = new StringWriter(content);

            DocumentNode queryDocument = Parser.Default.Parse(query);

            // act
            serializer.Visit(queryDocument, new DocumentWriter(writer));

            // assert
            Assert.Equal(
                query,
                content.ToString());
        }
Example #17
0
        public void Serialize_KitchenSinkWithoutIndentation_CanBeParsed()
        {
            // arrange
            string query = FileResource.Open("kitchen-sink.graphql");

            var serializer = new QuerySyntaxSerializer();
            var content    = new StringBuilder();
            var writer     = new StringWriter(content);

            DocumentNode queryDocument = Parser.Default.Parse(query);

            // act
            serializer.Visit(queryDocument, new DocumentWriter(writer));

            // assert
            string       serializedQuery = content.ToString();
            DocumentNode parsedQuery     = Parser.Default.Parse(serializedQuery);

            content.Clear();
            serializer.Visit(parsedQuery, new DocumentWriter(writer));
            Assert.Equal(serializedQuery, content.ToString());
        }
        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"));
        }
Example #20
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"));
            });
        }