From f3353e851948c2db257993b9b41b7907b50f591f Mon Sep 17 00:00:00 2001 From: Angus Dippenaar Date: Sun, 21 Jan 2018 09:59:03 +0200 Subject: [PATCH] Use input_class as arguments if dict For two of my GraphQL mutations, I want to have my create and update functions use the same input parameters, but update with an additional "id" field. I strongly believe in "don't repeat yourself", so I would like to create one dict or class that they can both use/inherit, and be able to add the "id" to the update function. If I decide to add new parameters, I want both mutations to be affected with minimal code updates. I tried a few things, but none worked. After looking at the code, I saw that there was a simple fix that would allow me to use a dictionary and be able to do this: ```UserParameters = { "name": graphene.String(), "email": graphene.String(), ... } UserCreate(graphene.Mutation): Arguments = { **UserParameters, "password": graphene.String() } ... UserUpdate(graphene.Mutation): Arguments = { **UserParameters, "id": graphene.ID(required=True) } ... ``` I don't know if there was another way to achieve this result with the current method of creating the argument inputs. If anyone has a more Pythonic or elegant way to achieve this result, I'm open to ideas. --- graphene/types/mutation.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/graphene/types/mutation.py b/graphene/types/mutation.py index 25794d47..ecdcb457 100644 --- a/graphene/types/mutation.py +++ b/graphene/types/mutation.py @@ -54,7 +54,10 @@ class Mutation(ObjectType): ).format(name=cls.__name__)) if input_class: - arguments = props(input_class) + if isinstance(input_class, dict): + arguments = input_class + else: + arguments = props(input_class) else: arguments = {}