Пример #1
0
        protected ComplexOutputTypeModel CreateClassModel(
            IDocumentAnalyzerContext context,
            IFragmentNode returnTypeFragment,
            ComplexOutputTypeModel returnType,
            SelectionInfo selection,
            Path path)
        {
            var fieldNames = new HashSet <string>(
                selection.Fields.Select(t => GetPropertyName(t.ResponseName)));

            string className = context.GetOrCreateName(
                returnTypeFragment.Fragment.SelectionSet,
                GetClassName(returnTypeFragment.Name),
                fieldNames);

            var modelClass = new ComplexOutputTypeModel(
                className,
                returnTypeFragment.Fragment.TypeCondition.Description,
                false,
                returnTypeFragment.Fragment.TypeCondition,
                returnTypeFragment.Fragment.SelectionSet,
                new[] { returnType },
                CreateFields(
                    (IComplexOutputType)returnTypeFragment.Fragment.TypeCondition,
                    selection.SelectionSet.Selections,
                    n => true,
                    path));

            context.Register(modelClass);

            return(modelClass);
        }
Пример #2
0
        private static void RegisterOperationModel(
            IDocumentAnalyzerContext context,
            DocumentNode document,
            OperationDefinitionNode operationDefinition)
        {
            ComplexOutputTypeModel returnType =
                context.Types.OfType <ComplexOutputTypeModel>()
                .First(t => t.SelectionSet == operationDefinition.SelectionSet && t.IsInterface);

            var parser = new ParserModel(
                context.GetOrCreateName(
                    GetClassName(operationDefinition.Name !.Value, "ResultParser")),
                operationDefinition,
                returnType,
                context.FieldParsers.Where(t => t.Operation == operationDefinition).ToList());

            var operation = new OperationModel(
                returnType.Name,
                context.Schema.GetOperationType(operationDefinition.Operation),
                document,
                operationDefinition,
                parser,
                CreateOperationArguments(context, operationDefinition));

            context.Register(operation);
        }
        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);
        }
Пример #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);
        }
Пример #5
0
        private ComplexOutputTypeModel CreateInterfaceModel(
            IDocumentAnalyzerContext context,
            IFragmentNode fragmentNode,
            Path path,
            Stack <ISet <string> > levels)
        {
            NameString name = context.GetOrCreateName(
                fragmentNode.Fragment.SelectionSet,
                GetInterfaceName(fragmentNode.Name));

            if (context.TryGetModel(name, out ComplexOutputTypeModel? typeModel))
            {
                return(typeModel);
            }

            ISet <string> implementedFields = levels.Peek();
            IReadOnlyList <OutputFieldModel> fieldModels = Array.Empty <OutputFieldModel>();

            IReadOnlyList <ComplexOutputTypeModel> implements =
                CreateChildInterfaceModels(
                    context,
                    fragmentNode,
                    path,
                    levels,
                    implementedFields);

            if (fragmentNode.Fragment.TypeCondition is IComplexOutputType type)
            {
                fieldModels = CreateFields(
                    type,
                    fragmentNode.Fragment.SelectionSet.Selections,
                    name =>
                {
                    if (implementedFields.Add(name))
                    {
                        return(true);
                    }
                    return(false);
                },
                    path);
            }

            typeModel = new ComplexOutputTypeModel(
                name,
                fragmentNode.Fragment.TypeCondition.Description,
                true,
                fragmentNode.Fragment.TypeCondition,
                fragmentNode.Fragment.SelectionSet,
                implements,
                fieldModels);

            context.Register(typeModel);

            return(typeModel);
        }
Пример #6
0
 protected static FieldParserModel CreateFieldParserModel(
     IDocumentAnalyzerContext context,
     OperationDefinitionNode operation,
     FieldNode fieldSelection,
     Path path,
     ComplexOutputTypeModel returnType,
     IType fieldType,
     ComplexOutputTypeModel modelType) =>
 CreateFieldParserModel(
     context, operation, fieldSelection,
     path, returnType, fieldType, new[] { modelType });
Пример #7
0
        protected static FieldParserModel CreateFieldParserModel(
            IDocumentAnalyzerContext context,
            OperationDefinitionNode operation,
            FieldNode fieldSelection,
            Path path,
            ComplexOutputTypeModel returnType,
            IType fieldType,
            IReadOnlyList <ComplexOutputTypeModel> possibleTypes)
        {
            var parserModel = new FieldParserModel(
                operation,
                fieldSelection,
                path,
                returnType,
                fieldType,
                possibleTypes);

            context.Register(parserModel);

            return(parserModel);
        }
Пример #8
0
        protected IReadOnlyList <ComplexOutputTypeModel> CreateClassModels(
            IDocumentAnalyzerContext context,
            ComplexOutputTypeModel returnType,
            FieldNode fieldSelection,
            IReadOnlyCollection <SelectionInfo> selections,
            Path path)
        {
            var possibleModelTypes = new List <ComplexOutputTypeModel>();

            foreach (SelectionInfo selection in selections)
            {
                IFragmentNode modelType = ResolveReturnType(
                    selection.Type, fieldSelection, selection);

                var interfaces = new List <ComplexOutputTypeModel>();

                foreach (IFragmentNode fragment in ShedNonMatchingFragments(
                             selection.Type, modelType))
                {
                    interfaces.Add(CreateInterfaceModel(context, fragment, path));
                }

                interfaces.Insert(0, returnType);

                NameString typeName = HoistName(selection.Type, modelType);

                if (typeName.IsEmpty)
                {
                    typeName = selection.Type.Name;
                }

                bool update = false;

                var fieldNames = new HashSet <string>(
                    selection.Fields.Select(t => GetPropertyName(t.ResponseName)));

                string className = context.GetOrCreateName(
                    modelType.Fragment.SelectionSet,
                    GetClassName(typeName),
                    fieldNames);

                if (context.TryGetModel(className, out ComplexOutputTypeModel? model))
                {
                    var interfaceNames = new HashSet <string>(interfaces.Select(t => t.Name));
                    foreach (ComplexOutputTypeModel type in model.Types.Reverse())
                    {
                        if (interfaceNames.Add(type.Name))
                        {
                            interfaces.Insert(0, type);
                        }
                    }
                    update = true;
                }

                model = new ComplexOutputTypeModel(
                    className,
                    modelType.Fragment.TypeCondition.Description,
                    false,
                    modelType.Fragment.TypeCondition,
                    selection.SelectionSet,
                    interfaces,
                    CreateFields(
                        (IComplexOutputType)modelType.Fragment.TypeCondition,
                        selection.SelectionSet.Selections,
                        n => true,
                        path));

                context.Register(model, update);

                possibleModelTypes.Add(model);
            }

            return(possibleModelTypes);
        }