예제 #1
0
        private IInterfaceDescriptor CreateInterfaceModel(
            IModelGeneratorContext context,
            IFragmentNode fragmentNode,
            Path path,
            Stack <ISet <string> > levels)
        {
            ISet <string> implementedFields = levels.Peek();
            IReadOnlyList <IFieldDescriptor> fieldDescriptors =
                Array.Empty <IFieldDescriptor>();

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

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

            NameString interfaceName = context.GetOrCreateName(
                fragmentNode.Fragment.SelectionSet,
                GetInterfaceName(fragmentNode.Name));

            var descriptor = new InterfaceDescriptor(
                interfaceName,
                context.Namespace,
                fragmentNode.Fragment.TypeCondition,
                fieldDescriptors,
                implements);

            context.Register(descriptor);
            return(descriptor);
        }
예제 #2
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));
            }
        }
예제 #3
0
        private IInputClassDescriptor GenerateInputObjectType(
            IModelGeneratorContext context,
            InputObjectType inputObjectType,
            IDictionary <string, IInputClassDescriptor> knownTypes)
        {
            if (knownTypes.TryGetValue(
                    inputObjectType.Name,
                    out IInputClassDescriptor? descriptor))
            {
                return(descriptor);
            }

            string typeName = context.GetOrCreateName(
                inputObjectType.SyntaxNode,
                GetClassName(inputObjectType.Name));

            var fields = new List <Descriptors.IInputFieldDescriptor>();

            descriptor = new InputClassDescriptor(
                typeName, context.Namespace, inputObjectType, fields);
            knownTypes[inputObjectType.Name] = descriptor;

            foreach (InputField field in inputObjectType.Fields)
            {
                if (field.Type.NamedType() is InputObjectType fieldType)
                {
                    fields.Add(new InputFieldDescriptor(
                                   field.Name, field.Type, field,
                                   GenerateInputObjectType(context, fieldType, knownTypes)));
                }
                else
                {
                    fields.Add(new InputFieldDescriptor(
                                   field.Name, field.Type, field, null));
                }
            }

            return(descriptor);
        }
        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));
            }
        }