public async Task Union_With_Fragment_Definition_Two_Models()
        {
            // arrange
            var schema =
                await new ServiceCollection()
                .AddStarWarsRepositories()
                .AddGraphQL()
                .AddStarWars()
                .BuildSchemaAsync();

            var document =
                Utf8GraphQLParser.Parse(@"
                    query GetHero {
                        search(text: ""hello"") {
                            ... Hero
                            ... Starship
                        }
                    }

                    fragment Hero on Character {
                        name
                        ... Human
                        ... Droid
                    }

                    fragment Human on Human {
                        homePlanet
                    }

                    fragment Droid on Droid {
                        primaryFunction
                    }

                    fragment Starship on Starship {
                        length
                    }");

            var context = new DocumentAnalyzerContext(schema, document);
            SelectionSetVariants selectionSetVariants = context.CollectFields();
            FieldSelection       fieldSelection       = selectionSetVariants.ReturnType.Fields.First();

            selectionSetVariants = context.CollectFields(fieldSelection);

            // act
            var analyzer = new InterfaceTypeSelectionSetAnalyzer();
            var result   = analyzer.Analyze(context, fieldSelection, selectionSetVariants);

            // assert
            Assert.Equal("IGetHero_Search", result.Name);

            Assert.Collection(
                context.GetImplementations(result),
                model => Assert.Equal("IGetHero_Search_Starship", model.Name),
                model => Assert.Equal("IGetHero_Search_Human", model.Name),
                model => Assert.Equal("IGetHero_Search_Droid", model.Name));

            Assert.Empty(result.Fields);
        }
        public async Task Interface_With_Default_Names_Two_Models()
        {
            // arrange
            var schema =
                await new ServiceCollection()
                .AddStarWarsRepositories()
                .AddGraphQL()
                .AddStarWars()
                .BuildSchemaAsync();

            var document =
                Utf8GraphQLParser.Parse(@"
                    query GetHero {
                        hero(episode: NEW_HOPE) {
                            name
                            ... on Droid {
                                primaryFunction
                            }
                        }
                    }");

            var context = new DocumentAnalyzerContext(schema, document);
            SelectionSetVariants selectionSetVariants = context.CollectFields();
            FieldSelection       fieldSelection       = selectionSetVariants.ReturnType.Fields.First();

            selectionSetVariants = context.CollectFields(fieldSelection);

            // act
            var analyzer = new InterfaceTypeSelectionSetAnalyzer();
            var result   = analyzer.Analyze(context, fieldSelection, selectionSetVariants);

            // assert
            Assert.Equal("IGetHero_Hero", result.Name);

            Assert.Collection(
                context.GetImplementations(result),
                model => Assert.Equal("IGetHero_Hero_Human", model.Name),
                model => Assert.Equal("IGetHero_Hero_Droid", model.Name));

            Assert.Collection(
                result.Fields,
                field => Assert.Equal("Name", field.Name));
        }
        public async Task Interface_With_Fragment_Definition_One_Model()
        {
            // arrange
            var schema =
                await new ServiceCollection()
                .AddStarWarsRepositories()
                .AddGraphQL()
                .AddStarWars()
                .BuildSchemaAsync();

            var document =
                Utf8GraphQLParser.Parse(@"
                    query GetHero {
                        hero(episode: NEW_HOPE) {
                            ... Hero
                        }
                    }

                    fragment Hero on Character {
                        name
                    }");

            var context = new DocumentAnalyzerContext(schema, document);
            SelectionSetVariants selectionSetVariants = context.CollectFields();
            FieldSelection       fieldSelection       = selectionSetVariants.ReturnType.Fields.First();

            selectionSetVariants = context.CollectFields(fieldSelection);

            // act
            var analyzer = new InterfaceTypeSelectionSetAnalyzer();
            var result   = analyzer.Analyze(context, fieldSelection, selectionSetVariants);

            // assert
            Assert.Equal("IGetHero_Hero", result.Name);

            Assert.Collection(
                context.GetImplementations(result).OrderBy(t => t.Name),
                model => Assert.Equal("IGetHero_Hero_Droid", model.Name),
                model => Assert.Equal("IGetHero_Hero_Human", model.Name));

            Assert.Empty(result.Fields);
        }
Exemplo n.º 4
0
        public void Interface_With_2_Selections_Per_Type()
        {
            // arrange
            ISchema schema =
                SchemaBuilder.New()
                .Use(next => context => Task.CompletedTask)
                .AddDocumentFromString(@"
                    type Query {
                      foo: Foo
                    }

                    interface Foo {
                      id: String
                      name: String
                    }

                    type Bar implements Foo {
                      id: String
                      name: String
                      bar: String
                      bar2: String
                    }

                    type Baz implements Foo {
                      id: String
                      name: String
                      baz: String
                      baz2: String
                    }")
                .Create();

            DocumentNode document =
                Utf8GraphQLParser.Parse(@"
                {
                  foo {
                    id
                    name
                    ... on Bar {
                      bar
                    }
                    ... on Bar {
                      bar2
                    }
                    ... on Baz {
                      baz
                    }
                    ... on Baz {
                      baz2
                    }
                  }
                }");

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

            FieldNode field =
                operation.SelectionSet.Selections.OfType <FieldNode>().Single();

            var context = new DocumentAnalyzerContext(schema);

            context.SetDocument(document);

            InterfaceType fooType  = schema.GetType <InterfaceType>("Foo");
            Path          rootPath = Path.New("foo");

            PossibleSelections possibleSelections =
                context.CollectFields(
                    fooType,
                    field.SelectionSet,
                    rootPath);

            // act
            var analyzer = new InterfaceTypeSelectionSetAnalyzer();

            analyzer.Analyze(
                context,
                operation,
                field,
                possibleSelections,
                schema.QueryType.Fields["foo"].Type,
                fooType,
                rootPath);

            // assert

            /*
             * Assert.Collection(context.Types.OfType<ComplexOutputTypeModel>(),
             *  type =>
             *  {
             *      Assert.Equal("Foo", type.Name);
             *      Assert.Null(type.Description);
             *      Assert.Equal(fooType, type.Type);
             *      Assert.Equal(field.SelectionSet, type.SelectionSet);
             *      Assert.Empty(type.Types);
             *      Assert.Collection(type.Fields,
             *          field =>
             *          {
             *              Assert.Equal("Bar", field.Name);
             *              Assert.Null(field.Description);
             *              Assert.Equal(fooType.Fields["bar"], field.Field);
             *              Assert.Equal(fooType.Fields["bar"].Type, field.Type);
             *              Assert.Equal(rootPath.Append("bar"), field.Path);
             *          });
             *  });
             */
        }
Exemplo n.º 5
0
        public void Interface_Two_Types_One_Inline_Fragment_Per_Type()
        {
            // arrange
            ISchema schema =
                SchemaBuilder.New()
                .Use(next => context => Task.CompletedTask)
                .AddDocumentFromString(@"
                    type Query {
                      foo: Foo
                    }

                    interface Foo {
                      id: String
                      name: String
                    }

                    type Bar implements Foo {
                      id: String
                      name: String
                      bar: String
                    }

                    type Baz implements Foo {
                      id: String
                      name: String
                      baz: String
                    }")
                .Create();

            DocumentNode document =
                Utf8GraphQLParser.Parse(@"
                {
                  foo {
                    id
                    name
                    ... on Bar {
                      bar
                    }
                    ... on Baz {
                      baz
                    }
                  }
                }");

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

            FieldNode field =
                operation.SelectionSet.Selections.OfType <FieldNode>().Single();

            var context = new DocumentAnalyzerContext(schema);

            context.SetDocument(document);

            InterfaceType fooType  = schema.GetType <InterfaceType>("Foo");
            Path          rootPath = Path.New("foo");

            PossibleSelections possibleSelections =
                context.CollectFields(
                    fooType,
                    field.SelectionSet,
                    rootPath);

            // act
            var analyzer = new InterfaceTypeSelectionSetAnalyzer();

            analyzer.Analyze(
                context,
                operation,
                field,
                possibleSelections,
                schema.QueryType.Fields["foo"].Type,
                fooType,
                rootPath);

            // assert
        }