Exemplo n.º 1
0
        /// <summary>
        /// Inspects the given type and, in accordance with the rules of this maker, will
        /// generate a complete set of necessary graph types required to support it.
        /// </summary>
        /// <param name="concreteType">The concrete type to incorporate into the schema.</param>
        /// <returns>GraphTypeCreationResult.</returns>
        public virtual GraphTypeCreationResult CreateGraphType(Type concreteType)
        {
            if (concreteType == null)
            {
                return(null);
            }

            var template = GraphQLProviders.TemplateProvider.ParseType(concreteType, TypeKind.OBJECT) as IObjectGraphTypeTemplate;

            if (template == null)
            {
                return(null);
            }

            var result    = new GraphTypeCreationResult();
            var formatter = _schema.Configuration.DeclarationOptions.GraphNamingFormatter;

            var fieldSet   = new List <IGraphField>();
            var fieldMaker = new GraphFieldMaker(_schema);

            foreach (var fieldTemplate in ObjectGraphTypeMaker.GatherFieldTemplates(template, _schema))
            {
                var fieldResult = fieldMaker.CreateField(fieldTemplate);
                fieldSet.Add(fieldResult.Field);

                result.MergeDependents(fieldResult);
            }

            var objectType = new ObjectGraphType(
                formatter.FormatGraphTypeName(template.Name),
                concreteType,
                fieldSet)
            {
                Description = template.Description,
                Publish     = template.Publish,
            };

            // add in declared interfaces by name
            foreach (var iface in template.DeclaredInterfaces)
            {
                objectType.InterfaceNames.Add(formatter.FormatGraphTypeName(GraphTypeNames.ParseName(iface, TypeKind.OBJECT)));
            }

            result.GraphType    = objectType;
            result.ConcreteType = concreteType;
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Inspects the given type and, in accordance with the rules of this maker, will
        /// generate a complete set of necessary graph types required to support it.
        /// </summary>
        /// <param name="concreteType">The concrete type to incorporate into the schema.</param>
        /// <returns>GraphTypeCreationResult.</returns>
        public GraphTypeCreationResult CreateGraphType(Type concreteType)
        {
            if (concreteType == null)
            {
                return(null);
            }

            var template = GraphQLProviders.TemplateProvider.ParseType(concreteType, TypeKind.INTERFACE) as IInterfaceGraphTypeTemplate;

            if (template == null)
            {
                return(null);
            }

            var result    = new GraphTypeCreationResult();
            var formatter = _schema.Configuration.DeclarationOptions.GraphNamingFormatter;

            var fieldSet   = new List <IGraphField>();
            var fieldMaker = new GraphFieldMaker(_schema);

            foreach (var fieldTemplate in ObjectGraphTypeMaker.GatherFieldTemplates(template, _schema))
            {
                var fieldResult = fieldMaker.CreateField(fieldTemplate);
                fieldSet.Add(fieldResult.Field);

                result.MergeDependents(fieldResult);
            }

            var interfaceType = new InterfaceGraphType(
                formatter.FormatGraphTypeName(template.Name),
                template.ObjectType,
                fieldSet)
            {
                Description = template.Description,
                Publish     = template.Publish,
            };

            result.GraphType    = interfaceType;
            result.ConcreteType = concreteType;
            return(result);
        }