Esempio n. 1
0
        public static IEnumerable <IGraphType> CreateGraphTypes(
            IEnumerable <IContentTypeComposition> contentTypes,
            PublishedItemType publishedItemType,
            Func <IContentTypeBase, string> resolveName = null)
        {
            if (resolveName == null)
            {
                resolveName = Conventions.NameResolvers.PascalCase;
            }

            var interfaceGraphTypes = new Dictionary <string, IInterfaceGraphType>();

            //TODO: Whitelist/blacklist content types

            var contentTypeList = contentTypes.ToList();
            var compositions    = contentTypeList.SelectMany(x => x.ContentTypeComposition).Distinct().ToList();

            foreach (var contentType in compositions)
            {
                var graphType = new InterfaceGraphType <IPublishedContent>
                {
                    Name        = resolveName(contentType),
                    Description = contentType.Description,
                    Metadata    =
                    {
                        ["documentTypeAlias"] = contentType.Alias,
                    }
                };

                graphType.AddUmbracoContentPropeties(contentType, publishedItemType);

                yield return(graphType);

                interfaceGraphTypes.Add(contentType.Alias, graphType);
            }

            foreach (var contentType in contentTypeList.Except(compositions))
            {
                var graphType = new ObjectGraphType <IPublishedContent>
                {
                    Name        = resolveName(contentType),
                    Description = contentType.Description,
                    IsTypeOf    = content => ((IPublishedContent)content).DocumentTypeAlias == contentType.Alias,
                    Metadata    =
                    {
                        ["documentTypeAlias"] = contentType.Alias,
                        ["allowedAtRoot"]     = contentType.AllowedAsRoot,
                        ["allowedChildren"]   = contentType.AllowedContentTypes.Select(x => x.Alias).ToArray(),
                    }
                };

                graphType.Interface <PublishedContentGraphType>();
                foreach (var composition in contentType.ContentTypeComposition)
                {
                    if (interfaceGraphTypes.TryGetValue(composition.Alias, out IInterfaceGraphType interfaceType))
                    {
                        graphType.AddResolvedInterface(interfaceType);
                    }
                }

                graphType.AddUmbracoBuiltInProperties();
                graphType.AddUmbracoContentPropeties(contentType, publishedItemType);

                yield return(graphType);
            }
        }