Skip to content

Commit

Permalink
Support with_context on ClientIDMutation.mutate_and_get_payload.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjtamlyn committed May 18, 2016
1 parent 398088a commit 61e7bee
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
55 changes: 54 additions & 1 deletion graphene/relay/tests/test_mutations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from graphql.type import GraphQLInputObjectField

import graphene
from graphene import relay
from graphene import relay, with_context
from graphene.core.schema import Schema

my_id = 0
my_id_context = 0


class Query(graphene.ObjectType):
Expand All @@ -25,8 +26,24 @@ def mutate_and_get_payload(cls, input, info):
return ChangeNumber(result=my_id)


class ChangeNumberContext(relay.ClientIDMutation):
'''Result mutation'''
class Input:
to = graphene.Int()

result = graphene.String()

@classmethod
@with_context
def mutate_and_get_payload(cls, input, context, info):
global my_id_context
my_id_context = input.get('to', my_id_context + context)
return ChangeNumber(result=my_id_context)


class MyResultMutation(graphene.ObjectType):
change_number = graphene.Field(ChangeNumber)
change_number_context = graphene.Field(ChangeNumberContext)


schema = Schema(query=Query, mutation=MyResultMutation)
Expand Down Expand Up @@ -79,3 +96,39 @@ def test_execute_mutations():
result = schema.execute(query, root_value=object())
assert not result.errors
assert result.data == expected


def test_context_mutations():
query = '''
mutation M{
first: changeNumberContext(input: {clientMutationId: "mutation1"}) {
clientMutationId
result
},
second: changeNumberContext(input: {clientMutationId: "mutation2"}) {
clientMutationId
result
}
third: changeNumberContext(input: {clientMutationId: "mutation3", to: 5}) {
result
clientMutationId
}
}
'''
expected = {
'first': {
'clientMutationId': 'mutation1',
'result': '-1',
},
'second': {
'clientMutationId': 'mutation2',
'result': '-2',
},
'third': {
'clientMutationId': 'mutation3',
'result': '5',
}
}
result = schema.execute(query, root_value=object(), context_value=-1)
assert not result.errors
assert result.data == expected
10 changes: 7 additions & 3 deletions graphene/relay/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..core.types.argument import ArgumentsGroup
from ..core.types.definitions import NonNull
from ..utils import memoize
from ..utils.wrap_resolver_function import has_context
from ..utils.wrap_resolver_function import has_context, with_context
from .fields import GlobalIDField


Expand Down Expand Up @@ -192,9 +192,13 @@ class Meta:
abstract = True

@classmethod
def mutate(cls, instance, args, info):
@with_context
def mutate(cls, instance, args, context, info):
input = args.get('input')
payload = cls.mutate_and_get_payload(input, info)
if has_context(cls.mutate_and_get_payload):
payload = cls.mutate_and_get_payload(input, context, info)
else:
payload = cls.mutate_and_get_payload(input, info)
client_mutation_id = input.get('clientMutationId') or input.get('client_mutation_id')
setattr(payload, 'clientMutationId', client_mutation_id)
return payload

0 comments on commit 61e7bee

Please sign in to comment.