From af7e021bbaab2be40e0b90cdad4d86bab331c340 Mon Sep 17 00:00:00 2001 From: Robert Svallin Date: Thu, 12 Dec 2024 12:44:35 +0100 Subject: [PATCH] Allow TransformAction to clone options When the client is used to query different graph sources the transform action passed to CreateFromConfig can be used to manipulate the keys sent. However the changes are applied to the global options and doesnt reset after the client has finished its query. By introducing a behaviour it is possible to dictatate if the developer wants to be able to apply the transformation on a clone of the options. The default behaviour is that the current behavior where the options are global. --- .../Api/Querying/GraphQueryBuilder.cs | 8 +++++++ .../Configuration/OptiGraphOptions.cs | 9 +++++++- .../Configuration/TransformActionBehaviour.cs | 23 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 APIs/src/EpiServer.ContentGraph/Configuration/TransformActionBehaviour.cs diff --git a/APIs/src/EpiServer.ContentGraph/Api/Querying/GraphQueryBuilder.cs b/APIs/src/EpiServer.ContentGraph/Api/Querying/GraphQueryBuilder.cs index 7617cdb8..298a7206 100644 --- a/APIs/src/EpiServer.ContentGraph/Api/Querying/GraphQueryBuilder.cs +++ b/APIs/src/EpiServer.ContentGraph/Api/Querying/GraphQueryBuilder.cs @@ -42,6 +42,14 @@ public static GraphQueryBuilder CreateFromConfig(Action transf var httpClientFactory = ServiceLocator.Current.GetService(typeof(IHttpClientFactory)) as IHttpClientFactory; if (transformAction != null) { + if (options.TransformActionBehaviour == TransformActionBehaviour.Clone) + { + var clonedOptions = (OptiGraphOptions)options.Clone(); + transformAction(clonedOptions); + + return new GraphQueryBuilder(clonedOptions, httpClientFactory); + } + transformAction(options); } if (options != null) diff --git a/APIs/src/EpiServer.ContentGraph/Configuration/OptiGraphOptions.cs b/APIs/src/EpiServer.ContentGraph/Configuration/OptiGraphOptions.cs index 10201a01..4011bcf8 100644 --- a/APIs/src/EpiServer.ContentGraph/Configuration/OptiGraphOptions.cs +++ b/APIs/src/EpiServer.ContentGraph/Configuration/OptiGraphOptions.cs @@ -5,7 +5,7 @@ namespace EPiServer.ContentGraph.Configuration { [Options] - public class OptiGraphOptions + public class OptiGraphOptions : ICloneable { public const string ConfigSection = "Optimizely:ContentGraph"; @@ -57,5 +57,12 @@ public string Authorization return $"epi-single {SingleKey}"; } } + + public TransformActionBehaviour TransformActionBehaviour { get; set; } + + public object Clone() + { + return this.MemberwiseClone(); + } } } diff --git a/APIs/src/EpiServer.ContentGraph/Configuration/TransformActionBehaviour.cs b/APIs/src/EpiServer.ContentGraph/Configuration/TransformActionBehaviour.cs new file mode 100644 index 00000000..90490307 --- /dev/null +++ b/APIs/src/EpiServer.ContentGraph/Configuration/TransformActionBehaviour.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EPiServer.ContentGraph.Configuration +{ + /// + /// Configures how OptiGraphOptions should handle the output of TransformAction when creating options + /// + public enum TransformActionBehaviour + { + /// + /// Default, transform action changes the global options + /// + Default, + /// + /// Clone, transform action clones the options and applies transformation only on the clone + /// + Clone + } +}