-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any way to generate InputTypes without writing a mutation class? #131
Comments
Hi @alex-a-pereira. This is an interesting use case. In short: No, the library does not expose any way to generate the input types in a standalone manner. However, this is something that can easily be added. If you got some errors from the registry (either graphene-django's or graphene-django-cud's), could you share them here? It might help me in the direction of implementing this as a feature. |
@tOgg1 thanks for the quick response. Here's a minimal example. Our CustomFormMemberInputType = type(
"CustomFormMemberInput",
(graphene.InputObjectType,),
get_input_fields_for_model(Member, ("first_name",), exclude=tuple()),
)
CustomFormUserInputType = type(
"CustomFormUserInput",
(graphene.InputObjectType,),
get_input_fields_for_model(
User,
fields=("is_superuser",),
exclude=tuple(),
many_to_one_extras={"member_set": {"add": {"type": "CustomFormMemberInput"}}},
),
)
class CustomFormInputType(graphene.InputObjectType):
user = CustomFormUserInputType(required=True)
class CustomFormSubmitMutation(graphene.Mutation):
class Arguments:
input = CustomFormInputType(required=True)
was_created = graphene.Boolean()
@classmethod
def mutate(cls, root, info, input): # noqa VNE003
# custom mutate method handler - e.g. get_or_create() on both models, do something
# with the `additional_item` in the input, etc.
return CustomFormSubmitMutation(was_created=True) This prevents django from starting up and spits out the error:
My theory about it being related to the registry was correct it looks like - when I copy the code from Here's a minimal working example: registry = get_global_registry()
meta_registry = get_type_meta_registry()
def build_and_register_custom_form_input_type(model, fields, many_to_one_extras=None):
input_type_name = f"CustomForm{model.__name__}Input"
model_fields = get_input_fields_for_model(model, fields, exclude=tuple(), many_to_one_extras=many_to_one_extras)
InputType = type(input_type_name, (graphene.InputObjectType,), model_fields)
# Register meta-data
meta_registry.register(
input_type_name,
{
"auto_context_fields": {},
"optional_fields": {},
"required_fields": {},
"many_to_many_extras": {},
"many_to_one_extras": many_to_one_extras,
"foreign_key_extras": {},
"one_to_one_extras": {},
"field_types": {},
},
)
registry.register_converted_field(input_type_name, InputType)
return InputType
CustomFormMemberInputType = build_and_register_custom_form_input_type(Member, ("first_name",))
CustomFormUserInputType = build_and_register_custom_form_input_type(
User,
("is_superuser",),
many_to_one_extras={
"member_set": {"add": {"type": "CustomFormMemberInput"}},
},
)
class CustomFormInputType(graphene.InputObjectType):
user = CustomFormUserInputType(required=True)
class CustomFormSubmitMutation(graphene.Mutation):
class Arguments:
input = CustomFormInputType(required=True)
was_created = graphene.Boolean()
@classmethod
def mutate(cls, root, info, input): # noqa VNE003
# custom mutate method handler - e.g. get_or_create() on both models, do something
# with the `additional_item` in the input, etc.
return CustomFormSubmitMutation(was_created=True) Doing this takes advantage of |
Hi, I'm wondering if this library exposes any way to generate an
InputType
for a model in an ad-hoc way.I was able to get this working by subclassing
DjangoCreateMutation
to create the InputType and add it to the registry, then accessing it below. However don't want to actually expose these mutations - I'm concerned that the mutations created might be included in the schema by another developer on the team accidentally. We really only want the InputType generated by these mutations.In the docs it mentions
Is there another way to take advantage of the auto-generated
InputType
without creating unused subclasses ofDjangoCreateMutation
?Here's an example - I'm building a custom mutation that
Example payload from client
I was able to get that to work using the following python code. The types are what I expect - they're auto generated from the models and use the names that I assign.
I tried to manually call the utils that
DjangoCreateMutation
calls but got a few errors related to the registry. I probably needed to add the lines below, but I'm concerned with how fragile my approach here isThe text was updated successfully, but these errors were encountered: