From 53aba57316825622269bf87cc9ee1e1801ea3829 Mon Sep 17 00:00:00 2001 From: Cyrille Date: Thu, 14 Jul 2022 21:48:34 +0200 Subject: [PATCH 1/3] Update Get object method --- src/GraphQL.Relay/Types/MutationInputs.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/GraphQL.Relay/Types/MutationInputs.cs b/src/GraphQL.Relay/Types/MutationInputs.cs index e66b97429..1f9a812a7 100644 --- a/src/GraphQL.Relay/Types/MutationInputs.cs +++ b/src/GraphQL.Relay/Types/MutationInputs.cs @@ -1,3 +1,5 @@ +using GraphQL.Execution; + namespace GraphQL.Relay.Types { public class MutationInputs : Dictionary @@ -15,9 +17,13 @@ public object Get(string key) return this[key]; } - public T Get(string key, T defaultValue = default) + public T Get(string key, T defaultValue = default) where T : class { - return TryGetValue(key, out object value) ? (T)value : defaultValue; + if (!TryGetValue(key, out object value)) + return defaultValue; + + var dictionary = value as Dictionary; + return (dictionary ?? throw new InvalidOperationError("The argument could not be determined as a valid dictionary.")).ToObject(); } } } From 30560e576eca22a5478a598abe7139d285171a2f Mon Sep 17 00:00:00 2001 From: Cyrille Date: Thu, 14 Jul 2022 21:55:49 +0200 Subject: [PATCH 2/3] Get param method, add defensive code --- src/GraphQL.Relay/Types/MutationInputs.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/GraphQL.Relay/Types/MutationInputs.cs b/src/GraphQL.Relay/Types/MutationInputs.cs index 1f9a812a7..08605ea46 100644 --- a/src/GraphQL.Relay/Types/MutationInputs.cs +++ b/src/GraphQL.Relay/Types/MutationInputs.cs @@ -21,9 +21,12 @@ public T Get(string key, T defaultValue = default) where T : class { if (!TryGetValue(key, out object value)) return defaultValue; + if (value is Dictionary dictionary) + { + return dictionary.ToObject(); + } - var dictionary = value as Dictionary; - return (dictionary ?? throw new InvalidOperationError("The argument could not be determined as a valid dictionary.")).ToObject(); + return (T)value; } } } From 55e45653ea02ff9e1e5cff8dc5ebe96925ca0132 Mon Sep 17 00:00:00 2001 From: Cyrille Date: Thu, 14 Jul 2022 21:57:50 +0200 Subject: [PATCH 3/3] Update syntax --- src/GraphQL.Relay/Types/MutationInputs.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/GraphQL.Relay/Types/MutationInputs.cs b/src/GraphQL.Relay/Types/MutationInputs.cs index 08605ea46..a3f4b17b6 100644 --- a/src/GraphQL.Relay/Types/MutationInputs.cs +++ b/src/GraphQL.Relay/Types/MutationInputs.cs @@ -21,12 +21,7 @@ public T Get(string key, T defaultValue = default) where T : class { if (!TryGetValue(key, out object value)) return defaultValue; - if (value is Dictionary dictionary) - { - return dictionary.ToObject(); - } - - return (T)value; + return value is Dictionary dictionary ? dictionary.ToObject() : (T)value; } } }