public void ParseDirectiveOnVariableDefinition() { // arrange byte[] sourceText = Encoding.UTF8.GetBytes( "query queryName($foo: ComplexType @foo) { bar }"); // act var parser = new Utf8GraphQLParser( sourceText, ParserOptions.Default); DocumentNode document = parser.Parse(); // assert Assert.Collection( document.Definitions.OfType <OperationDefinitionNode>().First() .VariableDefinitions.First() .Directives, d => Assert.Equal("foo", d.Name.Value)); }
public void Parser_ImplementsInterfacesAndDirectives() { // arrange string sourceText = "interface a implements e & f" + "@foo(a: \"123\") @foo(b: \"321\") { b: String } "; var parser = new Utf8GraphQLParser( Encoding.UTF8.GetBytes(sourceText)); // act DocumentNode document = parser.Parse(); // assert var def = (document.Definitions[0] as InterfaceTypeDefinitionNode); Assert.NotNull(def); Assert.Equal("a", def.Name.Value); Assert.Null(def.Description); Assert.Single(def.Fields); Assert.Equal("b", def.Fields[0].Name.Value); Assert.Collection(def.Interfaces, i => { Assert.Equal("e", i.Name.Value); }, i => { Assert.Equal("f", i.Name.Value); }); Assert.Collection(def.Directives, d => { Assert.Equal("foo", d.Name.Value); Assert.Equal("a", d.Arguments[0].Name.Value); Assert.Equal("123", d.Arguments[0].Value.Value); }, d => { Assert.Equal("foo", d.Name.Value); Assert.Equal("b", d.Arguments[0].Name.Value); Assert.Equal("321", d.Arguments[0].Value.Value); }); Assert.Equal(NodeKind.InterfaceTypeDefinition, def.Kind); }
public void Serialize_UnionTypeDefWithDirectiveNoIndent_InOutShouldBeTheSame() { // arrange string query = "union A @a = B | C union A @a @b = B | C"; var serializer = new SchemaSyntaxSerializer(); var content = new StringBuilder(); var writer = new StringWriter(content); DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void Serialize_ObjectTypeDefWithDirectivesWithIndent_OutHasIndentation() { // arrange string query = "type Foo @a(x: \"y\") { bar: String baz: [Int] } " + "type Foo @a @b { bar: String @foo " + "baz(a: String = \"abc\"): [Int] @foo @bar }"; var serializer = new SchemaSyntaxSerializer(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 Serialize_ObjectTypeDefWithArgsNoIndent_InOutShouldBeTheSame() { // arrange string query = "type Foo { bar(a: Int = 1 b: Int): String }"; var serializer = new SchemaSyntaxSerializer(); var content = new StringBuilder(); var writer = new StringWriter(content); DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void Serialize_InputObjectTypeDefWithDescriptionNoIndent_InOutShouldBeTheSame() { // arrange string query = "\"abc\" input A { \"abc\" b: String }"; var serializer = new SchemaSyntaxSerializer(); var content = new StringBuilder(); var writer = new StringWriter(content); DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void Serialize_SchemaDefWithOpAndDirecNoIndent_InOutShouldBeTheSame() { // arrange string query = "schema @a @b(c: 1) { query: A }"; var serializer = new SchemaSyntaxSerializer(); var content = new StringBuilder(); var writer = new StringWriter(content); DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void StringArg() { // arrange byte[] sourceText = Encoding.UTF8.GetBytes( "{ a(b:\"Q3VzdG9tZXIteDE=\") }"); // act var parser = new Utf8GraphQLParser( sourceText, ParserOptions.Default); DocumentNode document = parser.Parse(); // assert StringValueNode value = Assert.IsType <StringValueNode>( document.Definitions.OfType <OperationDefinitionNode>().First() .SelectionSet.Selections.OfType <FieldNode>().First() .Arguments.First().Value); Assert.Equal("Q3VzdG9tZXIteDE=", value.Value); }
public void EnumArg(string arg) { // arrange byte[] sourceText = Encoding.UTF8.GetBytes( "{ a(b:" + arg + ") }"); // acts var parser = new Utf8GraphQLParser( sourceText, ParserOptions.Default); DocumentNode document = parser.Parse(); // assert EnumValueNode value = Assert.IsType <EnumValueNode>( document.Definitions.OfType <OperationDefinitionNode>().First() .SelectionSet.Selections.OfType <FieldNode>().First() .Arguments.First().Value); Assert.Equal(arg, value.Value); }
public void Serialize_EnumTypeDefNoIndent_InOutShouldBeTheSame() { // arrange string query = "enum A { B C }"; var serializer = new SchemaSyntaxSerializer(); var content = new StringBuilder(); var writer = new StringWriter(content); DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
private static unsafe T Parse <T>( string sourceText, ParseSyntax <T> parse, bool moveNext = true) where T : ISyntaxNode { if (string.IsNullOrEmpty(sourceText)) { throw new ArgumentException( LangResources.SourceText_Empty, nameof(sourceText)); } int length = checked (sourceText.Length * 4); bool useStackalloc = length <= GraphQLConstants.StackallocThreshold; byte[]? source = null; Span <byte> sourceSpan = useStackalloc ? stackalloc byte[length] : (source = ArrayPool <byte> .Shared.Rent(length)); try { ConvertToBytes(sourceText, ref sourceSpan); var parser = new Utf8GraphQLParser(sourceSpan); if (moveNext) { parser.MoveNext(); } return(parse(parser)); } finally { if (source != null) { sourceSpan.Clear(); ArrayPool <byte> .Shared.Return(source); } } }
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 = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void QueryWithComments() { // arrange string query = @"{ hero { name # Queries can have comments! friends { name } } }"; // act DocumentNode document = Utf8GraphQLParser.Parse(query, new ParserOptions(noLocations: true)); // assert document.MatchSnapshot(); }
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(); }
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 = Utf8GraphQLParser.Parse(query, new ParserOptions(allowFragmentVariables: true)); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void Serialize_ObjectTypeImplementsXYZ_InOutShouldBeTheSame() { // arrange string query = "type Foo implements X & Y & Z " + "{ bar: String baz: [Int] }"; var serializer = new SchemaSyntaxSerializer(); var content = new StringBuilder(); var writer = new StringWriter(content); DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void Serialize_InputObjectTypeDefWithDirectiveNoIndent_InOutShouldBeTheSame() { // arrange string query = "input A @a @b(c: 1) { b: String @a @b(c: 1) " + "c: [String!]! @a @b(c: 1) d: Int = 1 @a @b(c: 1) }"; var serializer = new SchemaSyntaxSerializer(); var content = new StringBuilder(); var writer = new StringWriter(content); DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void StringArgumentIsEmpty() { // arrange byte[] sourceText = Encoding.UTF8.GetBytes( "{ foo(bar: \"\") }"); // act var parser = new Utf8GraphQLParser( sourceText, ParserOptions.Default); DocumentNode document = parser.Parse(); // assert IValueNode value = document.Definitions.OfType <OperationDefinitionNode>().First() .SelectionSet.Selections.OfType <FieldNode>().First() .Arguments.First().Value; Assert.Equal(string.Empty, Assert.IsType <StringValueNode>(value).Value); }
public static unsafe DocumentNode Parse( string sourceText, ParserOptions options) { if (string.IsNullOrEmpty(sourceText)) { throw new ArgumentException( LangResources.SourceText_Empty, nameof(sourceText)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } int length = checked (sourceText.Length * 4); bool useStackalloc = length <= GraphQLConstants.StackallocThreshold; byte[] source = null; Span <byte> sourceSpan = useStackalloc ? stackalloc byte[length] : (source = ArrayPool <byte> .Shared.Rent(length)); try { ConvertToBytes(sourceText, ref sourceSpan); var parser = new Utf8GraphQLParser(sourceSpan, options); return(parser.Parse()); } finally { if (source != null) { sourceSpan.Clear(); ArrayPool <byte> .Shared.Return(source); } } }
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 = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void Serialize_ObjectTypeDefWithDirectivesNoIndent_InOutShouldBeTheSame() { // arrange string query = "type Foo @a(x: \"y\") { bar: String baz: [Int] } " + "type Foo @a @b { bar: String @foo " + "baz(a: String = \"abc\"): [Int] @foo @bar }"; var serializer = new SchemaSyntaxSerializer(); var content = new StringBuilder(); var writer = new StringWriter(content); DocumentNode queryDocument = Utf8GraphQLParser.Parse(query); // act serializer.Visit(queryDocument, new DocumentWriter(writer)); // assert Assert.Equal( query, content.ToString()); }
public void Visit_Kitchen_Sink_Query_Names_With_Delegate_OnlyLeave() { // arrange DocumentNode document = Utf8GraphQLParser.Parse( FileResource.Open("kitchen-sink.graphql")); var visitationMap = new BarVisitationMap(); var visitedNames = new List <string>(); // act document.Accept <NameNode>( null, (node, parent, path, ancestors) => { visitedNames.Add(node.Value); return(VisitorAction.Continue); }, node => VisitorAction.Continue); // assert visitedNames.MatchSnapshot(); }
public void Serialize_KitchenSinkWithoutIndentation_CanBeParsed() { // 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 string serializedQuery = content.ToString(); DocumentNode parsedQuery = Utf8GraphQLParser.Parse(serializedQuery); content.Clear(); serializer.Visit(parsedQuery, new DocumentWriter(writer)); Assert.Equal(serializedQuery, content.ToString()); }
public void Parser_Simple() { // arrange string sourceText = "interface a { b: String } "; var parser = new Utf8GraphQLParser( Encoding.UTF8.GetBytes(sourceText)); // act DocumentNode document = parser.Parse(); // assert var def = (document.Definitions[0] as InterfaceTypeDefinitionNode); Assert.NotNull(def); Assert.Equal("a", def.Name.Value); Assert.Null(def.Description); Assert.Single(def.Fields); Assert.Equal("b", def.Fields[0].Name.Value); Assert.Empty(def.Directives); Assert.Empty(def.Interfaces); Assert.Equal(NodeKind.InterfaceTypeDefinition, def.Kind); }
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")); }
public static unsafe IReadOnlyList <GraphQLRequest> Parse( string sourceText, ParserOptions?options = null, IDocumentCache?cache = null, IDocumentHashProvider?hashProvider = null) { if (string.IsNullOrEmpty(sourceText)) { throw new ArgumentException( LangResources.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); } } }