Example #1
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
        }
Example #2
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);
             *          });
             *  });
             */
        }
        public void Simple_Object_Selection_With_Alias()
        {
            // arrange
            ISchema schema =
                SchemaBuilder.New()
                .AddQueryType <Query>()
                .Create();

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

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

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

            var context = new DocumentAnalyzerContext(schema);

            context.SetDocument(document);

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

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

            // act
            var analyzer = new ObjectTypeSelectionSetAnalyzer();

            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("B", 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("b"), field.Path);
                });
            });
        }