Exemplo n.º 1
0
        public void ParseQueryWithFragment()
        {
            // arrange
            var sourceText = "query a { x { ... y } } fragment y on Type { z }";

            // act
            var          parser   = new StringGraphQLParser(sourceText);
            DocumentNode document = parser.Parse();

            // assert
            Assert.Collection(document.Definitions,
                              t =>
            {
                Assert.IsType <OperationDefinitionNode>(t);
                var operationDefinition = (OperationDefinitionNode)t;
                Assert.Equal(NodeKind.OperationDefinition, operationDefinition.Kind);
                Assert.Equal("a", operationDefinition.Name.Value);
                Assert.Equal(OperationType.Query, operationDefinition.Operation);
                Assert.Empty(operationDefinition.VariableDefinitions);
                Assert.Empty(operationDefinition.Directives);

                Assert.Collection(operationDefinition.SelectionSet.Selections,
                                  s1 =>
                {
                    FieldNode field1 = Assert.IsType <FieldNode>(s1);
                    Assert.Null(field1.Alias);
                    Assert.Equal("x", field1.Name.Value);
                    Assert.Empty(field1.Arguments);
                    Assert.Empty(field1.Directives);

                    Assert.Collection(field1.SelectionSet.Selections,
                                      s2 =>
                    {
                        Assert.IsType <FragmentSpreadNode>(s2);

                        FragmentSpreadNode spread = (FragmentSpreadNode)s2;
                        Assert.Equal("y", spread.Name.Value);
                        Assert.Empty(spread.Directives);
                    });
                });
            },
                              t =>
            {
                FragmentDefinitionNode fragmentDefinition = Assert.IsType <FragmentDefinitionNode>(t);
                Assert.Equal("y", fragmentDefinition.Name.Value);
                Assert.Equal("Type", fragmentDefinition.TypeCondition.Name.Value);
                Assert.Empty(fragmentDefinition.Directives);

                ISelectionNode selectionNode = fragmentDefinition
                                               .SelectionSet.Selections.Single();
                Assert.IsType <FieldNode>(selectionNode);
                Assert.Equal("z", ((FieldNode)selectionNode).Name.Value);
            });
        }
Exemplo n.º 2
0
        public void ParserSimpleQuery(string operation, OperationType expectedOperation)
        {
            // arrange
            string sourceText = $"{operation } a($s : String = \"hello\") {{ x {{ y }} }}";

            // act
            var          parser   = new StringGraphQLParser(sourceText);
            DocumentNode document = parser.Parse();

            // assert
            Assert.Collection(document.Definitions,
                              t =>
            {
                OperationDefinitionNode operationDefinition = Assert.IsType <OperationDefinitionNode>(t);
                Assert.Equal(NodeKind.OperationDefinition, operationDefinition.Kind);
                Assert.Equal("a", operationDefinition.Name.Value);
                Assert.Equal(expectedOperation, operationDefinition.Operation);
                Assert.Empty(operationDefinition.Directives);

                Assert.Collection(operationDefinition.VariableDefinitions,
                                  v1 =>
                {
                    Assert.Equal("s", v1.Variable.Name.Value);
                    Assert.Equal("String", ((NamedTypeNode)v1.Type).Name.Value);
                    Assert.IsType <StringValueNode>(v1.DefaultValue);
                });

                Assert.Collection(operationDefinition.SelectionSet.Selections,
                                  s1 =>
                {
                    FieldNode field1 = Assert.IsType <FieldNode>(s1);
                    Assert.Null(field1.Alias);
                    Assert.Equal("x", field1.Name.Value);
                    Assert.Empty(field1.Arguments);
                    Assert.Empty(field1.Directives);

                    Assert.Collection(field1.SelectionSet.Selections,
                                      s2 =>
                    {
                        FieldNode field2 = Assert.IsType <FieldNode>(s2);
                        Assert.Null(field2.Alias);
                        Assert.Equal("y", field2.Name.Value);
                        Assert.Empty(field2.Arguments);
                        Assert.Empty(field2.Directives);
                        Assert.Null(field2.SelectionSet);
                    });
                });
            });
        }
Exemplo n.º 3
0
        public void ParseSimpleShortHandFormQuery()
        {
            // arrange
            string sourceText = "{ x { y } }";

            // act
            var          parser   = new StringGraphQLParser(sourceText);
            DocumentNode document = parser.Parse();

            // assert
            Assert.Collection(document.Definitions,
                              t =>
            {
                Assert.IsType <OperationDefinitionNode>(t);
                var operationDefinition = (OperationDefinitionNode)t;
                Assert.Equal(NodeKind.OperationDefinition, operationDefinition.Kind);
                Assert.Null(operationDefinition.Name);
                Assert.Equal(OperationType.Query, operationDefinition.Operation);
                Assert.Empty(operationDefinition.VariableDefinitions);
                Assert.Empty(operationDefinition.Directives);

                Assert.Collection(operationDefinition.SelectionSet.Selections,
                                  s1 =>
                {
                    Assert.IsType <FieldNode>(s1);

                    var field1 = (FieldNode)s1;
                    Assert.Null(field1.Alias);
                    Assert.Equal("x", field1.Name.Value);
                    Assert.Empty(field1.Arguments);
                    Assert.Empty(field1.Directives);

                    Assert.Collection(field1.SelectionSet.Selections,
                                      s2 =>
                    {
                        Assert.IsType <FieldNode>(s2);

                        var field2 = (FieldNode)s2;
                        Assert.Null(field2.Alias);
                        Assert.Equal("y", field2.Name.Value);
                        Assert.Empty(field2.Arguments);
                        Assert.Empty(field2.Directives);
                        Assert.Null(field2.SelectionSet);
                    });
                });
            });
        }
Exemplo n.º 4
0
        public void QueryWithComments()
        {
            // arrange
            string sourceText =
                @"{
                    hero {
                        name
                        # Queries can have comments!
                        friends(a:""foo"" b: 123456 c:null d: true) {
                            name
                        }
                    }
                }";

            // act
            var          parser   = new StringGraphQLParser(sourceText);
            DocumentNode document = parser.Parse();

            // assert
            document.MatchSnapshot();
        }