public async Task Interface_With_Fragments()
        {
            // arrange
            var path = HotChocolate.Path.New("root");

            DocumentNode document = Utf8GraphQLParser.Parse(
                FileResource.Open("Multiple_Fragments_Query.graphql"));

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

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

            var query = new QueryDescriptor(
                "Simple_Query",
                "Foo.Bar.Ns",
                "1234",
                "12345",
                new byte[] { 1, 2, 3 },
                document);

            var schema = SchemaBuilder.New()
                         .AddDocumentFromString(FileResource.Open("StarWars.graphql"))
                         .Use(next => context => Task.CompletedTask)
                         .Create();

            var context = new ModelGeneratorContext(
                schema,
                query,
                "StarWarsClient",
                "Foo.Bar.Ns");

            var character = schema.GetType <InterfaceType>("Character");

            // act
            var generator = new InterfaceModelGenerator();

            generator.Generate(
                context,
                operation,
                character,
                character,
                field,
                context.CollectFields(character, field.SelectionSet, path),
                path);

            // assert
            var typeLookup = new TypeLookup(
                LanguageVersion.CSharp_8_0,
                CollectFieldsVisitor.MockLookup(document, context.FieldTypes),
                "Foo.Bar");

            string output = await WriteAllAsync(context.Descriptors, typeLookup);

            output.MatchSnapshot();
        }
        public async Task Union_Fragment_Definition()
        {
            // arrange
            var path = HotChocolate.Path.New("root");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"
                query search {
                    search(text: ""foo"") {
                        ... SomeHuman
                        ... SomeDroid
                        ... SomeStarship
                    }
                }

                fragment SomeHuman on Human {
                    homePlanet
                }

                fragment SomeDroid on Droid {
                    primaryFunction
                }

                fragment SomeStarship on Starship {
                    name
                }
                ");

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

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

            var query = new QueryDescriptor(
                "Simple_Query",
                "Foo.Bar.Ns",
                "1234",
                "12345",
                new byte[] { 1, 2, 3 },
                document);

            var schema = SchemaBuilder.New()
                         .AddDocumentFromString(FileResource.Open("StarWars.graphql"))
                         .Use(next => context => Task.CompletedTask)
                         .Create();

            var context = new ModelGeneratorContext(
                schema,
                query,
                "StarWarsClient",
                "Foo.Bar.Ns");

            var character = schema.GetType <UnionType>("SearchResult");

            // act
            var generator = new UnionModelGenerator();

            generator.Generate(
                context,
                operation,
                character,
                character,
                field,
                context.CollectFields(character, field.SelectionSet, path),
                path);

            // assert
            var typeLookup = new TypeLookup(
                LanguageVersion.CSharp_8_0,
                CollectFieldsVisitor.MockLookup(document, context.FieldTypes));

            string output = await WriteAllAsync(context.Descriptors, typeLookup);

            output.MatchSnapshot();
        }
Exemple #3
0
        public async Task Object_List_With_Fragment()
        {
            // arrange
            var path = HotChocolate.Path.New("root");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"
                query getBars {
                    foo {
                        bars {
                            baz
                        }
                    }
                }
                ");

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

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

            var query = new QueryDescriptor(
                "Simple_Query",
                "Foo.Bar.Ns",
                "1234",
                "12345",
                new byte[] { 1, 2, 3 },
                document);

            var schema = SchemaBuilder.New()
                         .AddDocumentFromString(
                @"
                    type Query {
                        foo: Foo
                    }

                    type Foo {
                        bars: [Bar]
                    }

                    type Bar {
                        baz: String
                    }
                    ")
                         .Use(next => context => Task.CompletedTask)
                         .Create();

            var context = new ModelGeneratorContext(
                schema,
                query,
                "StarWarsClient",
                "Foo.Bar.Ns");

            var bar = schema.GetType <ObjectType>("Foo");

            // act
            var generator = new ObjectModelGenerator();

            generator.Generate(
                context,
                operation,
                bar,
                bar,
                field,
                context.CollectFields(bar, field.SelectionSet, path),
                path);

            // assert
            var typeLookup = new TypeLookup(
                LanguageVersion.CSharp_8_0,
                CollectFieldsVisitor.MockLookup(document, context.FieldTypes));

            string output = await WriteAllAsync(context.Descriptors, typeLookup);

            output.MatchSnapshot();
        }