Esempio n. 1
0
        public void Prepare_Empty_Operation_SelectionSet()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(c => c
                                           .Name("Query")
                                           .Field("foo")
                                           .Type <StringType>()
                                           .Resolve("foo"))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse("{ }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            void Action() =>
            OperationCompiler.Compile(
                "a",
                document,
                operationDefinition,
                schema,
                schema.QueryType,
                new(new DefaultTypeConverter()));

            // assert
            Assert.Throws <GraphQLException>(Action);
        }
Esempio n. 2
0
        public void Field_Does_Not_Exist()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(c => c
                                           .Name("Query")
                                           .Field("foo")
                                           .Type <StringType>()
                                           .Resolver("foo"))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse("{ foo bar }");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            // act
            Action action = () =>
                            OperationCompiler.Compile(schema, fragments, operation);

            // assert
            Assert.Equal(
                "Field `bar` does not exist on type `Query`.",
                Assert.Throws <GraphQLException>(action).Message);
        }
Esempio n. 3
0
        public void Field_Does_Not_Exist()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(c => c
                                           .Name("Query")
                                           .Field("foo")
                                           .Type <StringType>()
                                           .Resolve("foo"))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse("{ foo bar }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            void Action() =>
            OperationCompiler.Compile(
                "a",
                document,
                operationDefinition,
                schema,
                schema.QueryType,
                new(new DefaultTypeConverter()));

            // assert
            Assert.Equal(
                "Field `bar` does not exist on type `Query`.",
                Assert.Throws <GraphQLException>(Action).Message);
        }
Esempio n. 4
0
        public void Object_Field_Visibility_Is_Correctly_Inherited_2()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) => name.Equals("v"));

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean, $q: Boolean) {
                    hero(episode: EMPIRE) @include(if: $v) {
                        name @include(if: $q)
                    }
                    ... on Query {
                        hero(episode: EMPIRE) {
                            id
                        }
                    }
                    ... @include(if: $v) {
                        hero(episode: EMPIRE) {
                            height
                        }
                    }
                }");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, SelectionVariants> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            var op = new Operation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);
            ISelectionSet rootSelections =
                op.RootSelectionVariants.GetSelectionSet(
                    op.RootSelectionVariants.GetPossibleTypes().First());
            ISelectionSet droidSelections =
                op.GetSelectionSet(rootSelections.Selections[0].SelectionSet !, droid);

            Assert.Collection(
                droidSelections.Selections.Where(t => t.IsIncluded(variables.Object)),
                t => Assert.Equal("id", t.ResponseName),
                t => Assert.Equal("height", t.ResponseName));

            op.Print().MatchSnapshot();
        }
Esempio n. 5
0
        public void Prepare_Duplicate_Field()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(c => c
                                           .Name("Query")
                                           .Field("foo")
                                           .Type <StringType>()
                                           .Resolver("foo"))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse("{ foo foo }");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, SelectionVariants> selectionVariants =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            Assert.Collection(
                selectionVariants.Values,
                selectionSet =>
            {
                Assert.Equal(operation.SelectionSet, selectionSet.SelectionSet);
                Assert.Collection(
                    selectionSet.GetSelectionSet(schema.QueryType).Selections,
                    selection => Assert.Equal("foo", selection.ResponseName));
            });
        }
Esempio n. 6
0
        public void Compile_Operation_Null()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(c => c
                                           .Name("Query")
                                           .Field("foo")
                                           .Type <StringType>()
                                           .Resolve("foo"))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse("{ foo }");

            // act
            void Action() =>
            OperationCompiler.Compile(
                "a",
                document,
                null !,
                schema,
                schema.QueryType,
                new(new DefaultTypeConverter()));

            // assert
            Assert.Throws <ArgumentNullException>(Action);
        }
Esempio n. 7
0
        public void Compile_Document_Null()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(c => c
                                           .Name("Query")
                                           .Field("foo")
                                           .Type <StringType>()
                                           .Resolver("foo"))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse("{ foo }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            void Action() =>
            OperationCompiler.Compile(
                "a",
                null !,
                operationDefinition,
                schema,
                schema.QueryType);

            // assert
            Assert.Throws <ArgumentNullException>(Action);
        }
Esempio n. 8
0
        public void CompositeType_SelectionsSet_Empty()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>())).Returns(false);

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean) {
                    hero(episode: EMPIRE) { }
                }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            void Action() =>
            OperationCompiler.Compile(
                "a",
                document,
                operationDefinition,
                schema,
                schema.QueryType,
                new(new DefaultTypeConverter()));

            // assert
            Assert.Throws <GraphQLException>(Action);
        }
Esempio n. 9
0
        public void Defer_Fragment_Spread()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                    hero(episode: EMPIRE) {
                        name
                        ... Foo @defer
                    }
                }
                
                fragment Foo on Droid {
                    id
                }
                ");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, SelectionVariants> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation, null);

            // assert
            var op = new Operation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);

            ISelection rootField = op.GetRootSelectionSet().Selections.Single();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            Assert.Collection(
                op.GetSelectionSet(rootField.SelectionSet, droid).Fragments,
                f =>
            {
                Assert.Equal(SyntaxKind.FragmentDefinition, f.SyntaxNode.Kind);
                Assert.Collection(
                    f.SelectionSet.Selections,
                    s => Assert.Equal("id", s.ResponseName));
            });

            ObjectType human = schema.GetType <ObjectType>("Human");

            Assert.Empty(op.GetSelectionSet(rootField.SelectionSet, human).Fragments);
        }
Esempio n. 10
0
        public void Defer_Inline_Fragment()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                    hero(episode: EMPIRE) {
                        name
                        ... @defer {
                            id
                        }
                    }
                }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            var operation =
                (Operation)OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // assert
            ISelection rootField = operation.GetRootSelectionSet().Selections.Single();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            Assert.Collection(
                operation.GetSelectionSet(rootField.SelectionSet !, droid).Fragments,
                f =>
            {
                Assert.Equal(SyntaxKind.InlineFragment, f.SyntaxNode.Kind);
                Assert.Collection(
                    f.SelectionSet.Selections,
                    s => Assert.Equal("id", s.ResponseName));
            });

            ObjectType human = schema.GetType <ObjectType>("Human");

            Assert.Collection(
                operation.GetSelectionSet(rootField.SelectionSet, human).Fragments,
                f =>
            {
                Assert.Equal(SyntaxKind.InlineFragment, f.SyntaxNode.Kind);
                Assert.Collection(
                    f.SelectionSet.Selections,
                    s => Assert.Equal("id", s.ResponseName));
            });
        }
Esempio n. 11
0
        public void Prepare_Inline_Fragment()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                hero(episode: EMPIRE) {
                    name
                    ... on Droid {
                        primaryFunction
                    }
                    ... on Human {
                        homePlanet
                    }
                }
             }");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, SelectionVariants> selectionVariants =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            ISelection hero = selectionVariants[operation.SelectionSet]
                              .GetSelectionSet(schema.QueryType).Selections.Single();

            Assert.Equal("hero", hero.ResponseName);

            Assert.Collection(
                selectionVariants[hero.SelectionSet]
                .GetSelectionSet(schema.GetType <ObjectType>("Droid")).Selections,
                selection => Assert.Equal("name", selection.ResponseName),
                selection => Assert.Equal("primaryFunction", selection.ResponseName));

            Assert.Collection(
                selectionVariants[hero.SelectionSet]
                .GetSelectionSet(schema.GetType <ObjectType>("Human")).Selections,
                selection => Assert.Equal("name", selection.ResponseName),
                selection => Assert.Equal("homePlanet", selection.ResponseName));

            var op = new Operation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionVariants);

            op.Print().MatchSnapshot();
        }
Esempio n. 12
0
        public void Field_Is_Visible_When_One_Selection_Is_Visible_4()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>())).Returns(false);

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean){
                    hero(episode: EMPIRE) {
                        name @include(if: $v)
                        ... abc
                    }
                }

                fragment abc on Droid {
                    name
                    ... {
                        name
                    }
                }");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, SelectionVariants> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            var op = new Operation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);
            ISelectionSet rootSelections =
                op.RootSelectionVariants.GetSelectionSet(
                    op.RootSelectionVariants.GetPossibleTypes().First());
            ISelectionSet droidSelections =
                op.GetSelectionSet(rootSelections.Selections[0].SelectionSet !, droid);

            Assert.Equal("name", droidSelections.Selections[0].ResponseName);
            Assert.False(droidSelections.Selections[0].IsConditional);
            Assert.True(droidSelections.Selections[0].IsIncluded(variables.Object));
            Assert.False(droidSelections.IsConditional);
        }
Esempio n. 13
0
        public void Prepare_Inline_Fragment()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                hero(episode: EMPIRE) {
                    name
                    ... on Droid {
                        primaryFunction
                    }
                    ... on Human {
                        homePlanet
                    }
                }
             }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // assert
            ISelection hero = operation.GetRootSelectionSet().Selections.Single();

            Assert.Equal("hero", hero.ResponseName);

            Assert.Collection(
                operation.GetSelectionSet(hero.SelectionSet !, schema.GetType <ObjectType>("Droid"))
                .Selections,
                selection => Assert.Equal("name", selection.ResponseName),
                selection => Assert.Equal("primaryFunction", selection.ResponseName));

            Assert.Collection(
                operation.GetSelectionSet(hero.SelectionSet, schema.GetType <ObjectType>("Human"))
                .Selections,
                selection => Assert.Equal("name", selection.ResponseName),
                selection => Assert.Equal("homePlanet", selection.ResponseName));

            operation.Print().MatchSnapshot();
        }
Esempio n. 14
0
        public void Field_Is_Visible_When_One_Selection_Is_Visible_3()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) => name.Equals("q"));

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean, $q: Boolean){
                    hero(episode: EMPIRE) {
                        name @include(if: $v)
                        ... abc @include(if: $q)
                    }
                }

                fragment abc on Droid {
                    name
                }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            var operation =
                (Operation)OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // assert
            ISelectionSet rootSelections =
                operation.RootSelectionVariants.GetSelectionSet(
                    operation.RootSelectionVariants.GetPossibleTypes().First());
            ISelectionSet droidSelections =
                operation.GetSelectionSet(rootSelections.Selections[0].SelectionSet !, droid);

            Assert.Equal("name", droidSelections.Selections[0].ResponseName);
            Assert.True(droidSelections.Selections[0].IsConditional);
            Assert.True(droidSelections.Selections[0].IsIncluded(variables.Object));
            Assert.True(droidSelections.IsConditional);
        }
Esempio n. 15
0
        public void Field_Based_Optimizers()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) => name.Equals("v"));

            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(d => d
                                           .Name("Query")
                                           .Field("root")
                                           .Resolve(new Foo())
                                           .UseOptimizer(new SimpleOptimizer()))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                    root {
                        bar {
                            text
                        }
                    }
                }");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            var optimizers = new List <NoopOptimizer> {
                new NoopOptimizer()
            };

            // act
            IReadOnlyDictionary <SelectionSetNode, SelectionVariants> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation, optimizers);

            // assert
            var op = new Operation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);

            op.Print().MatchSnapshot();
        }
Esempio n. 16
0
        public void Field_Based_Optimizers()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) => name.Equals("v"));

            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(d => d
                                           .Name("Query")
                                           .Field("root")
                                           .Resolve(new Foo())
                                           .UseOptimizer(new SimpleOptimizer()))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                    root {
                        bar {
                            text
                        }
                    }
                }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var optimizers = new List <NoopOptimizer> {
                new NoopOptimizer()
            };

            // act
            var operation =
                (Operation)OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()),
                    optimizers);

            // assert
            operation.Print().MatchSnapshot();
        }
Esempio n. 17
0
        public void Object_Field_Visibility_Is_Correctly_Inherited_3()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) => name.Equals("v"));

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean, $q: Boolean) {
                    hero(episode: EMPIRE) @include(if: $v) {
                        name @include(if: $q)
                    }
                }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            var operation =
                (Operation)OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // assert
            ISelectionSet rootSelections =
                operation.RootSelectionVariants.GetSelectionSet(
                    operation.RootSelectionVariants.GetPossibleTypes().First());
            ISelectionSet droidSelections =
                operation.GetSelectionSet(rootSelections.Selections[0].SelectionSet !, droid);

            Assert.Empty(droidSelections.Selections.Where(t => t.IsIncluded(variables.Object)));

            operation.Print().MatchSnapshot();
        }
Esempio n. 18
0
        public void Prepare_Fragment_Definition()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                hero(episode: EMPIRE) {
                    name
                    ... abc
                    ... def
                }
              }

              fragment abc on Droid {
                  primaryFunction
              }

              fragment def on Human {
                  homePlanet
              }
             ");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, SelectionVariants> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            var op = new Operation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);

            op.Print().MatchSnapshot();
        }
Esempio n. 19
0
        public void Prepare_Fragment_Definition()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                hero(episode: EMPIRE) {
                    name
                    ... abc
                    ... def
                }
              }

              fragment abc on Droid {
                  primaryFunction
              }

              fragment def on Human {
                  homePlanet
              }
             ");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // assert
            operation.Print().MatchSnapshot();
        }
Esempio n. 20
0
        public void Reuse_Selection()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query Hero($episode: Episode, $withFriends: Boolean!) {
                    hero(episode: $episode) {
                        name
                        friends @include(if: $withFriends) {
                            nodes {
                                id
                            }
                        }
                    }
                }");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, SelectionVariants> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation, null);

            // assert
            new Operation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets)
            .Print()
            .MatchSnapshot();
        }
Esempio n. 21
0
        public void Prepare_Duplicate_Field_With_Skip()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(c => c
                                           .Name("Query")
                                           .Field("foo")
                                           .Type <StringType>()
                                           .Resolve("foo"))
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse("{ foo @skip(if: true) foo @skip(if: false) }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // assert
            Assert.Collection(
                operation.SelectionVariants,
                selectionSet =>
            {
                Assert.Equal(operationDefinition.SelectionSet, selectionSet.SelectionSet);
                Assert.Collection(
                    selectionSet.GetSelectionSet(schema.QueryType).Selections,
                    selection => Assert.Equal("foo", selection.ResponseName));
            });
        }
Esempio n. 22
0
        public void Reuse_Selection()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query Hero($episode: Episode, $withFriends: Boolean!) {
                    hero(episode: $episode) {
                        name
                        friends @include(if: $withFriends) {
                            nodes {
                                id
                            }
                        }
                    }
                }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // assert
            operation
            .Print()
            .MatchSnapshot();
        }
Esempio n. 23
0
        public void Nested_Fragments()
        {
            // arrange
            var vFalse = new Mock <IVariableValueCollection>();

            vFalse.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>())).Returns(false);

            var vTrue = new Mock <IVariableValueCollection>();

            vTrue.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>())).Returns(true);

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"
                query ($if: Boolean!) {
                    human(id: ""1000"") {
                        ... Human1 @include(if: $if)
                        ... Human2 @skip(if: $if)
                    }
                }
                fragment Human1 on Human {
                    friends {
                        edges {
                            ... FriendEdge1
                        }
                    }
                }
                fragment FriendEdge1 on CharacterEdge {
                    node {
                        __typename
                        friends {
                            nodes {
                                __typename
                                ... Human3
                            }
                        }
                    }
                }
                fragment Human2 on Human {
                    friends {
                        edges {
                            node {
                                __typename
                                ... Human3
                            }
                        }
                    }
                }
                fragment Human3 on Human {
                    # This works
                    name

                    # This is returned as an empty object but should be populated
                    otherHuman {
                      __typename
                      name
                    }
                }
                ");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            var operation =
                (Operation)OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // assert
            operation.Print().MatchSnapshot();
        }
Esempio n. 24
0
        public void Object_Field_Visibility_Is_Correctly_Inherited()
        {
            // arrange
            var vFalse = new Mock <IVariableValueCollection>();

            vFalse.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>())).Returns(false);

            var vTrue = new Mock <IVariableValueCollection>();

            vTrue.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>())).Returns(true);

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean) {
                    hero(episode: EMPIRE) @include(if: $v) {
                        name
                    }
                    ... on Query {
                        hero(episode: EMPIRE) {
                            id
                        }
                    }
                    ... @include(if: $v) {
                        hero(episode: EMPIRE) {
                            height
                        }
                    }
                }");

            OperationDefinitionNode operationDefinition =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            // act
            var operation =
                (Operation)OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType);

            // assert
            ISelectionSet rootSelections =
                operation.RootSelectionVariants.GetSelectionSet(
                    operation.RootSelectionVariants.GetPossibleTypes().First());
            ISelectionSet droidSelections =
                operation.GetSelectionSet(rootSelections.Selections[0].SelectionSet !, droid);

            Assert.Collection(
                droidSelections.Selections.Where(t => t.IsIncluded(vFalse.Object)),
                t => Assert.Equal("id", t.ResponseName));

            Assert.Collection(
                droidSelections.Selections.Where(t => t.IsIncluded(vTrue.Object)),
                t => Assert.Equal("name", t.ResponseName),
                t => Assert.Equal("id", t.ResponseName),
                t => Assert.Equal("height", t.ResponseName));

            operation.Print().MatchSnapshot();
        }