Ejemplo n.º 1
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();
        }
Ejemplo n.º 2
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);

            // assert
            operation.Print().MatchSnapshot();
        }
Ejemplo n.º 3
0
        public void Defer_Inline_Fragment()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

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

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                    hero(episode: EMPIRE) {
                        name
                        ... @defer {
                            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.InlineFragment, f.SyntaxNode.Kind);
                Assert.Collection(
                    f.SelectionSet.Selections,
                    s => Assert.Equal("id", s.ResponseName));
            });

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

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