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 static IReadOnlyDictionary <FieldNode, string> MockLookup(
                DocumentNode query,
                IReadOnlyDictionary <FieldNode, string> knownFieldTypes)
            {
                var fieldTypes = knownFieldTypes.ToDictionary(t => t.Key, t => t.Value);
                var visitor    = new CollectFieldsVisitor();

                visitor.Visit(query, fieldTypes);
                return(fieldTypes);
            }
        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();
        }
Ejemplo n.º 4
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();
        }