umbraco-community / umbraco-graphql

An implementation of GraphQL for Umbraco 8 using GraphQL for .NET.
MIT License
64 stars 32 forks source link

Cannot access Rich Text field with Macro via GraphQL #73

Open rangler2 opened 3 years ago

rangler2 commented 3 years ago

Hi,

Just raising this on the off chance that someone has already fixed this... Am running a customised version of the Umbraco 7 version of GraphQL based upon the 0.1.0 version found on nuget which I think was from the master branch..

If I add a simple macro (with no parameters, using a .cshtml that just outputs some text) into a rich text editor and then try to retrieve this via GraphQL then the value is null and following error is included in the response:

image

If I attach the debugger then it throws on this line because it can't get the Value -

https://github.com/umbraco-community/umbraco-graphql/blob/65dcaabca2573807a2d397106b6ded39f662ac9b/src/Our.Umbraco.GraphQL/Types/ComplexGraphTypeOfIPublishedContentExtensions.cs#L176

The exception is -

System.InvalidOperationException
  HResult=0x80131509
  Message=Cannot render a macro when there is no current PublishedContentRequest.
  Source=umbraco
  StackTrace:
   at Umbraco.Web.UmbracoComponentRenderer.RenderMacro(String alias, IDictionary`2 parameters)
   at Umbraco.Web.PropertyEditors.ValueConverters.RteMacroRenderingValueConverter.<>c__DisplayClass0_1.<RenderRteMacros>b__1(String macroAlias, Dictionary`2 macroAttributes)
   at Umbraco.Core.Macros.MacroTagParser.ParseMacros(String text, Action`1 textFoundCallback, Action`2 macroFoundCallback)
   at Umbraco.Web.PropertyEditors.ValueConverters.RteMacroRenderingValueConverter.RenderRteMacros(String source, Boolean preview)
   at Umbraco.Web.PropertyEditors.ValueConverters.RteMacroRenderingValueConverter.ConvertDataToSource(PublishedPropertyType propertyType, Object source, Boolean preview)
   at Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty.get_Value()
   at Our.Umbraco.GraphQL.Types.ComplexGraphTypeOfIPublishedContentExtensions.<>c__DisplayClass2_1.<AddUmbracoContentPropeties>b__0(ResolveFieldContext`1 context) in C:\working_git\my-navy-umbraco\Our.Umbraco.GraphQL\Types\ComplexGraphTypeOfIPublishedContentExtensions.cs:line 115
   at GraphQL.Resolvers.FuncFieldResolver`2.GraphQL.Resolvers.IFieldResolver.Resolve(ResolveFieldContext context)
   at GraphQL.Instrumentation.MiddlewareResolver.Resolve(ResolveFieldContext context)
   at GraphQL.Resolvers.FuncFieldResolver`1.GraphQL.Resolvers.IFieldResolver.Resolve(ResolveFieldContext context)
   at GraphQL.Execution.ExecutionStrategy.<ExecuteNodeAsync>d__7.MoveNext()

I am now trying to work out how to artificially add an Umbraco Context into this class but raising in case someone has solved this previously , and if not I can update if/when I figure it out.....

Thanks, Andy

rangler2 commented 3 years ago

Hi,

I fixed it by adding the following to the start of the method to trick it into using the context of the homepage -

UmbracoContext.Current.PublishedContentRequest = new PublishedContentRequest(new Uri("https://mysite.localhost/"), UmbracoContext.Current.RoutingContext);
UmbracoContext.Current.PublishedContentRequest.Prepare();

Cheers Andy

rangler2 commented 3 years ago

Actually it sometimes reverted back to null by having it before the foreach loop so I had to move it into the loop and change it so it only does this when a macro is found otherwise it makes the request take way longer (over a minute) just from repeatedly setting the context.