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(); }); }
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(); }); }
public void Parse_Apollo_AQP_FullRequest_And_Verify_Hash() { // arrange byte[] source = Encoding.UTF8.GetBytes( FileResource.Open("Apollo_AQP_FullRequest.json") .NormalizeLineBreaks()); // act var parserOptions = new ParserOptions(); var requestParser = new Utf8GraphQLRequestParser( source, parserOptions, new DocumentCache(), new Sha256DocumentHashProvider(HashFormat.Hex)); IReadOnlyList <GraphQLRequest> batch = requestParser.Parse(); // assert Assert.Collection(batch, r => { Assert.Null(r.OperationName); Assert.Empty(r.Variables); Assert.True(r.Extensions.ContainsKey("persistedQuery")); Assert.NotNull(r.Query); if (r.Extensions.TryGetValue("persistedQuery", out object o) && o is IReadOnlyDictionary <string, object> persistedQuery && persistedQuery.TryGetValue("sha256Hash", out o) && o is string hash) { Assert.Equal(hash, r.QueryHash); } });
public void Utf8GraphQLRequestParser_Parse() { // arrange var source = Encoding.UTF8.GetBytes( JsonConvert.SerializeObject( new GraphQLRequestDto { Query = FileResource.Open("kitchen-sink.graphql") .NormalizeLineBreaks() }).NormalizeLineBreaks()); // act IReadOnlyList <GraphQLRequest> batch = Utf8GraphQLRequestParser.Parse(source); // assert Assert.Collection(batch, r => { Assert.Null(r.OperationName); Assert.Null(r.QueryId); Assert.Null(r.Variables); Assert.Null(r.Extensions); r.Query !.ToString(true).MatchSnapshot(); });
public void Parse_Apollo_AQP_SignatureQuery_Variables_Without_Values() { // arrange byte[] source = Encoding.UTF8.GetBytes( FileResource.Open("Apollo_AQP_QuerySignature_2.json") .NormalizeLineBreaks()); // act var parserOptions = new ParserOptions(); var requestParser = new Utf8GraphQLRequestParser( source, parserOptions, new DocumentCache(), new Sha256DocumentHashProvider()); IReadOnlyList <GraphQLRequest> batch = requestParser.Parse(); // assert Assert.Collection(batch, r => { Assert.Null(r.OperationName); Assert.Equal("hashOfQuery", r.QueryName); Assert.Empty(r.Variables); Assert.True(r.Extensions.ContainsKey("persistedQuery")); Assert.Null(r.Query); Assert.Null(r.QueryHash); }); }
public static unsafe IReadOnlyList <GraphQLRequest> Parse( string sourceText, ParserOptions?options = null, IDocumentCache?cache = null, IDocumentHashProvider?hashProvider = null) { if (string.IsNullOrEmpty(sourceText)) { throw new ArgumentException(SourceText_Empty, nameof(sourceText)); } var length = checked (sourceText.Length * 4); byte[]? source = null; Span <byte> sourceSpan = length <= GraphQLConstants.StackallocThreshold ? stackalloc byte[length] : source = ArrayPool <byte> .Shared.Rent(length); try { Utf8GraphQLParser.ConvertToBytes(sourceText, ref sourceSpan); var parser = new Utf8GraphQLRequestParser(sourceSpan, options, cache, hashProvider); return(parser.Parse()); } finally { if (source != null) { sourceSpan.Clear(); ArrayPool <byte> .Shared.Return(source); } } }
public void Parse_Json() { // 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" }, } } }, } }).NormalizeLineBreaks()); // act var parsed = Utf8GraphQLRequestParser.ParseJson(source); // assert parsed.MatchSnapshot(); }
public static IReadOnlyDictionary <string, object?>?ParseJsonObject( ReadOnlySpan <byte> sourceText, ParserOptions?options = null) { options ??= ParserOptions.Default; var parser = new Utf8GraphQLRequestParser(sourceText, options); parser._reader.Expect(TokenKind.StartOfFile); return(parser.ParseObjectOrNull()); }
public void Parse_Socket_Message() { // arrange byte[] source = Encoding.UTF8.GetBytes( JsonConvert.SerializeObject( new Dictionary <string, object> { { "payload", new Dictionary <string, object> { { "a", "b" }, { "b", new Dictionary <string, object> { { "a", "b" }, { "b", true }, { "c", 1 }, { "d", 1.1 }, { "e", false }, { "f", null } } }, { "c", new List <object> { new Dictionary <string, object> { { "a", "b" }, } } }, } }, { "type", "foo" }, { "id", "bar" } }).NormalizeLineBreaks()); // act GraphQLSocketMessage message = Utf8GraphQLRequestParser.ParseMessage(source); // assert Assert.Equal("foo", message.Type); Assert.Equal("bar", message.Id); File.WriteAllBytes("Foo.json", message.Payload.ToArray()); Utf8GraphQLRequestParser.ParseJson(message.Payload).MatchSnapshot(); }
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(); }); }
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 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")); }); }