示例#1
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);
        }
示例#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));
        }
示例#3
0
 public ResultParserMethodDescriptor(
     string name,
     OperationDefinitionNode operation,
     IType resultType,
     FieldNode resultSelection,
     Path path,
     IInterfaceDescriptor resultDescriptor,
     IReadOnlyList <IResultParserTypeDescriptor> possibleTypes,
     IResultParserTypeDescriptor unknownType)
 {
     Name = name
            ?? throw new ArgumentNullException(nameof(name));
     Operation = operation
                 ?? throw new ArgumentNullException(nameof(operation));
     ResultType = resultType
                  ?? throw new ArgumentNullException(nameof(resultType));
     ResultSelection = resultSelection;
     Path            = path
                       ?? throw new ArgumentNullException(nameof(path));
     ResultDescriptor = resultDescriptor
                        ?? throw new ArgumentNullException(nameof(resultDescriptor));
     PossibleTypes = possibleTypes
                     ?? throw new ArgumentNullException(nameof(possibleTypes));
     UnknownType = unknownType;
 }
示例#4
0
 public ClassDescriptor(
     string name,
     string ns,
     INamedType type,
     IInterfaceDescriptor implements)
     : this(name, ns, type, new[] { implements })
 {
 }
        private ICodeDescriptor GenerateObjectSelectionSet(
            OperationDefinitionNode operation,
            ObjectType objectType,
            IType fieldType,
            WithDirectives fieldOrOperation,
            FieldCollectionResult typeCase,
            Path path)
        {
            IFragmentNode returnType = HoistFragment(
                objectType, typeCase.SelectionSet, typeCase.Fragments);

            IReadOnlyList <IFragmentNode> fragments;
            string className;

            if (returnType is null)
            {
                fragments = typeCase.Fragments;
                className = CreateName(fieldOrOperation, objectType, GetClassName);
            }
            else
            {
                fragments = returnType.Children;
                className = CreateName(GetClassName(returnType.Fragment.Name));
            }

            var modelSelectionSet = new SelectionSetNode(
                typeCase.Fields.Select(t => t.Selection).ToList());

            var modelFragment = new FragmentNode(new Fragment(
                                                     className, objectType, modelSelectionSet));

            modelFragment.Children.AddRange(fragments);

            IInterfaceDescriptor modelInterface =
                CreateInterface(modelFragment, path);

            var modelClass = new ClassDescriptor(
                className, _namespace, typeCase.Type, modelInterface);

            RegisterDescriptor(modelInterface);
            RegisterDescriptor(modelClass);

            RegisterDescriptor(
                new ResultParserMethodDescriptor(
                    GetPathName(path),
                    operation,
                    fieldType,
                    fieldOrOperation as FieldNode,
                    path,
                    modelInterface,
                    new[] { new ResultParserTypeDescriptor(modelClass) }));

            return(modelInterface);
        }
示例#6
0
 public ResultParserMethodDescriptor(
     string name,
     OperationDefinitionNode operation,
     IType resultType,
     FieldNode resultSelection,
     Path path,
     IInterfaceDescriptor resultDescriptor,
     IReadOnlyList <IResultParserTypeDescriptor> possibleTypes)
     : this(name, operation, resultType, resultSelection,
            path, resultDescriptor, possibleTypes, null)
 {
 }
        private void GenerateInterfaceTypeCaseModel(
            FieldCollectionResult typeCase,
            IFragmentNode returnType,
            ICollection <ResultParserTypeDescriptor> resultParser,
            Path path)
        {
            string className;
            IReadOnlyList <IFragmentNode> fragments;

            IFragmentNode modelType = HoistFragment(
                (ObjectType)typeCase.Type,
                typeCase.SelectionSet,
                typeCase.Fragments);

            if (modelType is null)
            {
                fragments = typeCase.Fragments;
                className = CreateName(GetClassName(typeCase.Type.Name));
            }
            else
            {
                fragments = modelType.Children;
                className = CreateName(GetClassName(modelType.Fragment.Name));
            }

            var modelSelectionSet = new SelectionSetNode(
                typeCase.Fields.Select(t => t.Selection).ToList());

            var modelFragment = new FragmentNode(new Fragment(
                                                     className, typeCase.Type, modelSelectionSet));

            modelFragment.Children.AddRange(fragments);
            if (modelFragment.Children.All(t =>
                                           t.Fragment.SelectionSet != returnType.Fragment.SelectionSet))
            {
                modelFragment.Children.Add(returnType);
            }

            IInterfaceDescriptor modelInterface =
                CreateInterface(modelFragment, path);

            var modelClass = new ClassDescriptor(
                className, _namespace, typeCase.Type, modelInterface);


            RegisterDescriptor(modelInterface);
            RegisterDescriptor(modelClass);

            resultParser.Add(new ResultParserTypeDescriptor(modelClass));
        }
示例#8
0
        protected void CreateClassModel(
            IModelGeneratorContext context,
            IFragmentNode returnType,
            IInterfaceDescriptor interfaceDescriptor,
            SelectionInfo selection,
            List <ResultParserTypeDescriptor> resultParserTypes)
        {
            var modelClass = new ClassDescriptor(
                GetClassName(returnType.Name),
                context.Namespace,
                selection.Type,
                new[] { interfaceDescriptor });

            context.Register(modelClass);
            resultParserTypes.Add(new ResultParserTypeDescriptor(modelClass));
        }
        public InterfaceDescriptor TryAddImplements(IInterfaceDescriptor descriptor)
        {
            var implements = new Dictionary <string, IInterfaceDescriptor>();

            foreach (IInterfaceDescriptor d in Implements)
            {
                implements[d.Name] = d;
            }

            implements[descriptor.Name] = descriptor;

            return(new InterfaceDescriptor(
                       Name,
                       Namespace,
                       Type,
                       Fields,
                       implements.Values.ToList()));
        }
示例#10
0
        protected void CreateClassModels(
            IModelGeneratorContext context,
            FieldNode fieldSelection,
            IFragmentNode returnType,
            IInterfaceDescriptor interfaceDescriptor,
            IReadOnlyCollection <SelectionInfo> selections,
            List <ResultParserTypeDescriptor> resultParserTypes,
            Path path)
        {
            foreach (SelectionInfo selection in selections)
            {
                IFragmentNode modelType = ResolveReturnType(
                    context,
                    selection.Type,
                    fieldSelection,
                    selection);

                var interfaces = new List <IInterfaceDescriptor>();

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

                interfaces.Insert(0, interfaceDescriptor);

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

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

                var modelClass = new ClassDescriptor(
                    className,
                    context.Namespace,
                    selection.Type,
                    interfaces);

                context.Register(modelClass);
                resultParserTypes.Add(new ResultParserTypeDescriptor(modelClass));
            }
        }
示例#11
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);
        }
示例#12
0
        private static IReadOnlyList <IFieldDescriptor> CreateFields(
            INamedType type,
            IReadOnlyList <IInterfaceDescriptor> implements)
        {
            var handled      = new HashSet <IInterfaceDescriptor>();
            var handledField = new HashSet <string>();
            var queue        = new Queue <IInterfaceDescriptor>(implements);
            var list         = new List <IFieldDescriptor>();

            if (type is IComplexOutputType complexType)
            {
                while (queue.Count > 0)
                {
                    IInterfaceDescriptor current = queue.Dequeue();
                    if (handled.Add(current))
                    {
                        foreach (IFieldDescriptor descriptor in current.Fields)
                        {
                            if (handledField.Add(descriptor.ResponseName) &&
                                complexType.Fields.TryGetField(
                                    descriptor.Field.Name, out IOutputField field))
                            {
                                list.Add(new FieldDescriptor(
                                             field,
                                             descriptor.Selection,
                                             descriptor.Type,
                                             descriptor.Path));
                            }
                        }

                        foreach (IInterfaceDescriptor child in current.Implements)
                        {
                            queue.Enqueue(child);
                        }
                    }
                }
            }

            return(list);
        }
        protected void CreateClassModel(
            IModelGeneratorContext context,
            IFragmentNode returnType,
            IInterfaceDescriptor interfaceDescriptor,
            SelectionInfo selection,
            List <ResultParserTypeDescriptor> resultParserTypes)
        {
            var fieldNames = new HashSet <string>(
                selection.Fields.Select(t => GetPropertyName(t.ResponseName)));

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

            var modelClass = new ClassDescriptor(
                className,
                context.Namespace,
                selection.Type,
                new[] { interfaceDescriptor });

            context.Register(modelClass);
            resultParserTypes.Add(new ResultParserTypeDescriptor(modelClass));
        }
        protected void CreateClassModels(
            IModelGeneratorContext context,
            FieldNode fieldSelection,
            IFragmentNode returnType,
            IInterfaceDescriptor interfaceDescriptor,
            IReadOnlyCollection <SelectionInfo> selections,
            List <ResultParserTypeDescriptor> resultParserTypes,
            Path path)
        {
            foreach (SelectionInfo selection in selections)
            {
                IFragmentNode modelType = ResolveReturnType(
                    context,
                    selection.Type,
                    fieldSelection,
                    selection);

                var interfaces = new List <IInterfaceDescriptor>();

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

                interfaces.Insert(0, interfaceDescriptor);

                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.TryGetDescriptor(className, out ClassDescriptor? modelClass))
                {
                    var interfaceNames = new HashSet <string>(interfaces.Select(t => t.Name));
                    foreach (IInterfaceDescriptor item in modelClass !.Implements.Reverse())
                    {
                        if (!interfaceNames.Contains(item.Name))
                        {
                            interfaces.Insert(0, item);
                        }
                    }
                    update = true;
                }

                modelClass = new ClassDescriptor(
                    className,
                    context.Namespace,
                    selection.Type,
                    interfaces);

                context.Register(modelClass, update);
                resultParserTypes.Add(new ResultParserTypeDescriptor(modelClass));
            }
        }
 IInterfaceDescriptor IInterfaceDescriptor.TryAddImplements(
     IInterfaceDescriptor descriptor) =>
 TryAddImplements(descriptor);