Exemplo n.º 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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }