public override void Analyze(
            IDocumentAnalyzerContext context,
            OperationDefinitionNode operation,
            FieldNode fieldSelection,
            PossibleSelections possibleSelections,
            IType fieldType,
            InterfaceType namedType,
            Path path)
        {
            IFragmentNode returnTypeFragment =
                ResolveReturnType(
                    namedType,
                    fieldSelection,
                    possibleSelections.ReturnType);

            ComplexOutputTypeModel returnType =
                CreateInterfaceModel(
                    context,
                    returnTypeFragment,
                    path);

            CreateClassModels(
                context,
                operation,
                fieldSelection,
                possibleSelections,
                returnTypeFragment,
                returnType,
                fieldType,
                path);
        }
Example #2
0
        private void CreateClassModels(
            IModelGeneratorContext context,
            OperationDefinitionNode operation,
            IType fieldType,
            FieldNode fieldSelection,
            PossibleSelections possibleSelections,
            IFragmentNode returnType,
            IInterfaceDescriptor interfaceDescriptor,
            Path path)
        {
            var resultParserTypes = new List <ResultParserTypeDescriptor>();
            IReadOnlyCollection <SelectionInfo> selections = possibleSelections.Variants;

            CreateClassModels(
                context,
                fieldSelection,
                returnType,
                interfaceDescriptor,
                selections,
                resultParserTypes,
                path);

            context.Register(
                new ResultParserMethodDescriptor(
                    GetPathName(path),
                    operation,
                    fieldType,
                    fieldSelection,
                    path,
                    interfaceDescriptor,
                    resultParserTypes));
        }
Example #3
0
        public override ICodeDescriptor Generate(
            IModelGeneratorContext context,
            OperationDefinitionNode operation,
            UnionType namedType,
            IType fieldType,
            FieldNode fieldSelection,
            PossibleSelections possibleSelections,
            Path path)
        {
            IFragmentNode returnType = ResolveReturnType(
                context,
                namedType,
                fieldSelection,
                possibleSelections.ReturnType);

            IInterfaceDescriptor interfaceDescriptor = CreateInterfaceModel(
                context, returnType, path);

            context.Register(fieldSelection, interfaceDescriptor);

            CreateClassModels(
                context,
                operation,
                fieldType,
                fieldSelection,
                possibleSelections,
                returnType,
                interfaceDescriptor,
                path);

            return(interfaceDescriptor);
        }
Example #4
0
        private void CreateClassModels(
            IDocumentAnalyzerContext context,
            OperationDefinitionNode operation,
            FieldNode fieldSelection,
            PossibleSelections possibleSelections,
            ComplexOutputTypeModel returnType,
            IType fieldType,
            Path path)
        {
            IReadOnlyCollection <SelectionInfo> selections = possibleSelections.Variants;

            IReadOnlyList <ComplexOutputTypeModel> modelTypes =
                CreateClassModels(
                    context,
                    returnType,
                    fieldSelection,
                    selections,
                    path);

            CreateFieldParserModel(
                context,
                operation,
                fieldSelection,
                path,
                returnType,
                fieldType,
                modelTypes);
        }
Example #5
0
 public abstract void Analyze(
     IDocumentAnalyzerContext context,
     OperationDefinitionNode operation,
     FieldNode fieldSelection,
     PossibleSelections possibleSelections,
     IType fieldType,
     T namedType,
     Path path);
 public abstract ICodeDescriptor Generate(
     IModelGeneratorContext context,
     OperationDefinitionNode operation,
     T namedType,
     IType returnType,
     FieldNode fieldSelection,
     PossibleSelections possibleSelections,
     Path path);
Example #7
0
        private static void VisitFieldSelectionSet(
            IDocumentAnalyzerContext context,
            OperationDefinitionNode operation,
            FieldNode fieldSelection,
            IType fieldType,
            Path path,
            Queue <FieldSelection> backlog)
        {
            var namedType = (INamedOutputType)fieldType.NamedType();

            PossibleSelections possibleSelections =
                context.CollectFields(
                    namedType,
                    fieldSelection.SelectionSet !,
                    path);

            foreach (SelectionInfo selectionInfo in possibleSelections.Variants)
            {
                EnqueueFields(backlog, selectionInfo.Fields, path);
            }

            if (namedType is UnionType unionType)
            {
                _unionTypeSelectionSetAnalyzer.Analyze(
                    context,
                    operation,
                    fieldSelection,
                    possibleSelections,
                    fieldType,
                    unionType,
                    path);
            }
            else if (namedType is InterfaceType interfaceType)
            {
                _interfaceTypeSelectionSetAnalyzer.Analyze(
                    context,
                    operation,
                    fieldSelection,
                    possibleSelections,
                    fieldType,
                    interfaceType,
                    path);
            }
            else if (namedType is ObjectType objectType)
            {
                _objectTypeSelectionSetAnalyzer.Analyze(
                    context,
                    operation,
                    fieldSelection,
                    possibleSelections,
                    fieldType,
                    objectType,
                    path);
            }
        }
        private void GenerateFieldSelectionSet(
            OperationDefinitionNode operation,
            IType fieldType,
            FieldNode fieldSelection,
            Path path,
            Queue <FieldSelection> backlog)
        {
            var namedType = (INamedOutputType)fieldType.NamedType();

            PossibleSelections possibleSelections =
                _fieldCollector.CollectFields(
                    namedType,
                    fieldSelection.SelectionSet !,
                    path);

            foreach (SelectionInfo selectionInfo in possibleSelections.Variants)
            {
                EnqueueFields(backlog, selectionInfo.Fields, path);
            }

            if (namedType is UnionType unionType)
            {
                _unionModelGenerator.Generate(
                    _context,
                    operation,
                    unionType,
                    fieldType,
                    fieldSelection,
                    possibleSelections,
                    path);
            }
            else if (namedType is InterfaceType interfaceType)
            {
                _interfaceModelGenerator.Generate(
                    _context,
                    operation,
                    interfaceType,
                    fieldType,
                    fieldSelection,
                    possibleSelections,
                    path);
            }
            else if (namedType is ObjectType objectType)
            {
                _objectModelGenerator.Generate(
                    _context,
                    operation,
                    objectType,
                    fieldType,
                    fieldSelection,
                    possibleSelections,
                    path);
            }
        }
        private ICodeDescriptor GenerateOperationSelectionSet(
            ObjectType operationType,
            OperationDefinitionNode operation,
            Path path,
            Queue <FieldSelection> backlog)
        {
            PossibleSelections possibleSelections =
                _fieldCollector.CollectFields(
                    operationType,
                    operation.SelectionSet,
                    path);

            EnqueueFields(backlog, possibleSelections.ReturnType.Fields, path);

            ICodeDescriptor resultDescriptor = _objectModelGenerator.Generate(
                _context,
                operation,
                operationType,
                new NonNullType(operationType),
                new FieldNode(
                    null,
                    new NameNode(operation.Name !.Value),
                    null,
                    new[]
            {
                new DirectiveNode(
                    GeneratorDirectives.Type,
                    new ArgumentNode("name", operation.Name.Value)),
                new DirectiveNode(GeneratorDirectives.Operation)
            },
                    Array.Empty <ArgumentNode>(),
                    null),
                possibleSelections,
                path);

            _operationModelGenerator.Generate(
                _context,
                operationType,
                operation,
                resultDescriptor);

            return(resultDescriptor);
        }
Example #10
0
        public override ICodeDescriptor Generate(
            IModelGeneratorContext context,
            OperationDefinitionNode operation,
            ObjectType namedType,
            IType fieldType,
            FieldNode fieldSelection,
            PossibleSelections possibleSelections,
            Path path)
        {
            IFragmentNode returnType = ResolveReturnType(
                context,
                namedType,
                fieldSelection,
                possibleSelections.ReturnType);

            IInterfaceDescriptor interfaceDescriptor = CreateInterfaceModel(
                context, returnType, path);

            context.Register(fieldSelection, interfaceDescriptor);

            var resultParserTypes = new List <ResultParserTypeDescriptor>();

            CreateClassModel(
                context,
                returnType,
                interfaceDescriptor,
                possibleSelections.ReturnType,
                resultParserTypes);

            context.Register(
                new ResultParserMethodDescriptor(
                    GetPathName(path),
                    operation,
                    fieldType,
                    fieldSelection,
                    path,
                    interfaceDescriptor,
                    resultParserTypes));

            return(interfaceDescriptor);
        }
Example #11
0
        private static void VisitOperationSelectionSet(
            IDocumentAnalyzerContext context,
            OperationDefinitionNode operation,
            ObjectType operationType,
            Path path,
            Queue <FieldSelection> backlog)
        {
            PossibleSelections possibleSelections =
                context.CollectFields(
                    operationType,
                    operation.SelectionSet,
                    path);

            EnqueueFields(backlog, possibleSelections.ReturnType.Fields, path);

            _objectTypeSelectionSetAnalyzer.Analyze(
                context,
                operation,
                new FieldNode(
                    null,
                    new NameNode(operation.Name !.Value),
                    null,
                    new[]
            {
                new DirectiveNode(
                    GeneratorDirectives.Type,
                    new ArgumentNode("name", operation.Name.Value)),
                new DirectiveNode(GeneratorDirectives.Operation)
            },
                    Array.Empty <ArgumentNode>(),
                    null),
                possibleSelections,
                new NonNullType(operationType),
                operationType,
                path);
        }
Example #12
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);
             *          });
             *  });
             */
        }
Example #13
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
        }
        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);
                });
            });
        }