Skip to content

class Input not being used on mutation with query variables #532

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

Closed
mvdwaeter opened this issue Aug 22, 2017 · 1 comment
Closed

class Input not being used on mutation with query variables #532

mvdwaeter opened this issue Aug 22, 2017 · 1 comment

Comments

@mvdwaeter
Copy link

mvdwaeter commented Aug 22, 2017

I wrote a mutation that can update the email field on a Checkout object for given checkoutId & email.

The input fields are checkoutId (custom scalar) and email (string).
The custom scalar takes the checkoutId and returns a Checkout object.

Here is the mutation:

class CheckoutEmailUpdate(Mutation):
    """
    Updates the email on an existing Checkout.
    """
    class Input:
        checkout_id = NodeID(required=True)
        email = graphene.String(required=True)

    checkout = graphene.Field(Checkout)

    @staticmethod
    def mutate(root, args, context, info):
        input = args.get('input')
        checkout = input.get('checkout_id')
        checkout.email = input.get('email')
        checkout.save(update_fields=['email'])
        return CheckoutEmailUpdate(checkout=checkout)

The NodeID is a custom scalar I created so that when you enter the mutate function; args.get('input').get('checkout_id') returns a Checkout object instead of a string.

I get the Checkout object when execute the following query:

mutation{
checkoutEmailUpdate(
  input: {
    checkoutId: "Q2hlY2tvdXQ6NWVhYTNjNTItZTMyZC00MmI4LWFkYjUtYTQ4MTZhMWQzNWJm",
    email: "[email protected]"
  }
){
checkout{
  id
  email
}
}}

But the Checkout object is not given (just a plain string), because the Input class is being ignored, when using the following query (note that I used query variables now):

mutation CheckoutEmailUpdate($input: CheckoutEmailUpdateInput!) {
        checkoutEmailUpdate(input: $input) {
            checkout {
                id
                email
            }
            userErrors {
                field
                message
            }
        }
    }

and query variables:

{
    "checkoutId": "Q2hlY2tvdXQ6NWVhYTNjNTItZTMyZC00MmI4LWFkYjUtYTQ4MTZhMWQzNWJm",
    "email": "[email protected]"
}

I'm using graphene==1.4.1

@mvdwaeter
Copy link
Author

mvdwaeter commented Aug 22, 2017

I think it relates to: graphql/graphql-js#433

And based on that ticket I managed to fix it, by adding a parse_value to NodeID:

class NodeID(ID):
    @staticmethod
    def parse_literal(node):
        """
        :return: Object or None
        """
        if isinstance(node, StringValue):
            return get_from_global_id(node.value)


    @staticmethod
    def parse_value(s):
        """
        :return: Object or None
        """
        if isinstance(s, string_types):
            return get_from_global_id(s)

The mutation query without query parameters is using NodeID.parse_literal
And the second mutation query, with query parameters is using NodeID.parse_value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant