Example #1
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));
        }
        private IReadOnlyList <IInterfaceDescriptor> CreateChildInterfaceModels(
            IModelGeneratorContext context,
            IFragmentNode fragmentNode,
            Path path,
            Stack <ISet <string> > levels,
            ISet <string> implementedFields)
        {
            var implementedByChildren = new HashSet <string>();

            levels.Push(implementedByChildren);

            var implements = new List <IInterfaceDescriptor>();

            foreach (IFragmentNode child in fragmentNode.Children)
            {
                implements.Add(CreateInterfaceModel(context, child, path, levels));
            }

            levels.Pop();

            foreach (string fieldName in implementedByChildren)
            {
                implementedFields.Add(fieldName);
            }

            return(implements);
        }
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);
        }
        public ICodeDescriptor Generate(IModelGeneratorContext context, EnumType enumType)
        {
            var values = new List <EnumValueDescriptor>();

            foreach (EnumValue value in enumType.Values)
            {
                IDirective directive = value.Directives.FirstOrDefault(t =>
                                                                       t.Name.Equals(GeneratorDirectives.Name) &&
                                                                       t.GetArgument <string>(GeneratorDirectives.ValueArgument) != null);

                string name = directive is null
                    ? GetPropertyName(value.Name.ToLowerInvariant())
                    : directive.GetArgument <string>(GeneratorDirectives.ValueArgument);

                values.Add(new EnumValueDescriptor(name, value.Name));
            }

            NameString typeName = context.GetOrCreateName(
                enumType.SyntaxNode,
                GetClassName(enumType.Name));

            var descriptor = new EnumDescriptor(
                typeName,
                context.Namespace,
                values);

            context.Register(descriptor);

            return(descriptor);
        }
 public abstract ICodeDescriptor Generate(
     IModelGeneratorContext context,
     OperationDefinitionNode operation,
     T namedType,
     IType returnType,
     FieldNode fieldSelection,
     PossibleSelections possibleSelections,
     Path path);
Example #6
0
 private IInputClassDescriptor GenerateInputObjectType(
     IModelGeneratorContext context,
     InputObjectType inputObjectType)
 {
     return(GenerateInputObjectType(
                context,
                inputObjectType,
                new Dictionary <string, IInputClassDescriptor>()));
 }
Example #7
0
        public ICodeDescriptor Generate(
            IModelGeneratorContext context,
            ObjectType operationType,
            OperationDefinitionNode operation,
            ICodeDescriptor resultType)
        {
            var arguments = new List <Descriptors.IArgumentDescriptor>();

            foreach (VariableDefinitionNode variableDefinition in
                     operation.VariableDefinitions)
            {
                string typeName = variableDefinition.Type.NamedType().Name.Value;

                if (!context.Schema.TryGetType(typeName, out INamedType namedType))
                {
                    throw new InvalidOperationException(
                              $"The variable type `{typeName}` is not supported by the schema.");
                }

                IType type = variableDefinition.Type.ToType(namedType);
                IInputClassDescriptor?inputClassDescriptor = null;

                if (namedType is InputObjectType inputObjectType)
                {
                    inputClassDescriptor = GenerateInputObjectType(context, inputObjectType);
                }

                arguments.Add(new ArgumentDescriptor(
                                  variableDefinition.Variable.Name.Value,
                                  type,
                                  variableDefinition,
                                  inputClassDescriptor));
            }

            string operationName = context.GetOrCreateName(
                operation,
                GetClassName(operation.Name !.Value) + "Operation");

            var descriptor = new OperationDescriptor(
                operationName,
                context.Namespace,
                operationType,
                operation,
                arguments,
                context.Query,
                resultType);

            context.Register(descriptor);

            return(descriptor);
        }
        protected IInterfaceDescriptor CreateInterfaceModel(
            IModelGeneratorContext context,
            IFragmentNode fragmentNode,
            Path path)
        {
            var levels = new Stack <ISet <string> >();

            levels.Push(new HashSet <string>());
            return(CreateInterfaceModel(
                       context,
                       fragmentNode,
                       path,
                       levels));
        }
        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);
        }
Example #10
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));
        }
Example #11
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));
            }
        }
Example #12
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 #13
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);
        }
        public CodeModelGenerator(
            ISchema schema,
            IQueryDescriptor query,
            ISet <string> usedNames,
            string clientName,
            string ns)
        {
            _schema     = schema ?? throw new ArgumentNullException(nameof(schema));
            _query      = query ?? throw new ArgumentNullException(nameof(query));
            _usedNames  = usedNames ?? throw new ArgumentNullException(nameof(usedNames));
            _clientName = clientName ?? throw new ArgumentNullException(nameof(clientName));
            _namespace  = ns ?? throw new ArgumentNullException(nameof(ns));

            _document       = query.OriginalDocument;
            _fieldCollector = new FieldCollector(
                schema,
                new FragmentCollection(schema, query.OriginalDocument));

            Descriptors = Array.Empty <ICodeDescriptor>();
            FieldTypes  = new Dictionary <FieldNode, string>();

            _context = new ModelGeneratorContext(schema, query, clientName, ns);
        }
        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));
            }
        }