Mutation
I have been reading the docs for graphene, but I cannot see an example of how to make a mutation object with multiple inputs that adheres to the relay standard. Here is what I have.
class NewCatalogItem(graphene.Mutation):
catalog_item = graphene.Field(CatalogNode)
class Input:
id = graphene.String()
store = graphene.Int()
desc = graphene.String()
thumb = graphene.String()
price = graphene.Float()
created = DateTime()
start = DateTime()
end = DateTime()
@classmethod
def mutate(cls, input, args, info):
# be a hack user for now until I work out authentication
import ipdb; ipdb.set_trace()
user = User.objects.get_by_natural_key('graphene')
id = args.get('id')
if id is not None:
# update an existing item
rid = from_global_id(id)[1]
catalog_item = CatalogItem.objects.get(pk=rid)
for a,v in args.items():
if a == 'id':
continue
if hasattr(catalog_item, a):
setattr(catalog_item, a, v)
catalog_item.save()
catalog_item.refresh_from_db()
else:
# make a new item
catalog_item = CatalogItem(owner=user, **args)
catalog_item.save()
return NewCatalogItem(catalog_item=CatalogNode(catalog_item))
But when I ask relay to parse the schema I get.
-- Relay Transform Error -- Catalog --
Within RelayQLDocument /home/othane/workspace/cata-front/src/Catalog.js:13
>
> line 1 (approximate)
> mutation Catalog {catalogItem}
> ^^^
Error: Your schema defines a mutation field `catalogItem` that takes 8 arguments, but mutation fields must have exactly one argument named `input`.
Stack: Error
at new RelayTransformError (/home/othane/workspace/cata-front/node_modules/babel-relay-plugin/lib/RelayTransformError.js:23:16)
at validateMutationField (/home/othane/workspace/cata-front/node_modules/babel-relay-plugin/lib/RelayQLPrinter.js:644:13)
at RelayQLPrinter.printMutation (/home/othane/workspace/cata-front/node_modules/babel-relay-plugin/lib/RelayQLPrinter.js:242:9)
at RelayQLPrinter.print (/home/othane/workspace/cata-front/node_modules/babel-relay-plugin/lib/RelayQLPrinter.js:97:34)
at RelayQLTransformer.transform (/home/othane/workspace/cata-front/node_modules/babel-relay-plugin/lib/RelayQLTransformer.js:66:58)
at PluginPass.TaggedTemplateExpression (/home/othane/workspace/cata-front/node_modules/babel-relay-plugin/lib/getBabelRelayPlugin.js:119:36)
at newFn (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/visitors.js:276:21)
at NodePath._call (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/path/context.js:76:18)
at NodePath.call (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/path/context.js:48:17)
at NodePath.visit (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/path/context.js:105:12)
at TraversalContext.visitQueue (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/context.js:150:16)
at TraversalContext.visitSingle (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/context.js:108:19)
at TraversalContext.visit (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/context.js:192:19)
at Function.traverse.node (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/index.js:114:17)
at NodePath.visit (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/path/context.js:115:19)
at TraversalContext.visitQueue (/home/othane/workspace/cata-front/node_modules/babel-traverse/lib/context.js:150:16)
I am getting quite lost at this point, is there an example of a relay compatible mutation with more than 1 field somewhere? ... thanks
In Relay Mutations the input fields you have in the NewCatalogItem.Input are inside the input argument in the mutation.
It seems the issue is in the way the mutation is executed in the client side. So, for your example to work, you would need to execute the following mutation:
mutation Catalog {
newCatalogItem(input: {id: "", store: ""}, clientMutationId:"123") { # and all the other fields
catalogItem {
id
}
}
}
You can find a more detailed example here: https://facebook.github.io/relay/docs/graphql-mutations.html
The tests for graphene Relay mutations might help too: https://github.com/graphql-python/graphene/blob/master/graphene/relay/tests/test_mutation.py#L130
Let me know if that helps! :)
expanding on what @syrusakbary said, if you are using es6 and relay:
export default class NewCatalogItem extends Relay.Mutation {
static fragments = {
store: () => Relay.QL`fragment on Store { id }`
};
getMutation() {
return Relay.QL`mutation {
newCatalogItem(input:$input)
}`;
}
getFatQuery() {
return Relay.QL`
fragment on NewCatalogItemPayload @relay(pattern:true) {
catalogItem
store {
items
}
}
`;
}
getVariables() {
return {
id: this.props.id,
store: this.props.store.id,
desc: this.props.desc,
thumb: this.props.thumb,
price: this.props.price,
created: this.props.created
start: this.props.start,
end: this.props.end
};
}
getConfigs() {
return [{
type: 'RANGE_ADD',
parentName: 'store',
parentID: this.props.store.id,
connectionName: 'items',
edgeName: 'items',
rangeBehaviors: ({orderBy}) => {
return 'append';
}
}];
}
}
Note that you are missing a few output fields on the server side code that would let you do the RANGE_ADD on the client side. You would need to add a connection field called items and return an array containing your catalog item.
@yfilali yes I have the array of items in the server code which I left out for brevity ... What I don't get in your example is the $input variable in getMutation, where does that come from, ie how does it know what the mutation inputs are ? Or is it comming from getVariables ?
Yes, it's coming from getVariables. Anything you return there gets crammed into a single input object per the relay spec. The relay client side code handles the clientMutationId for you automatically. The query coming out of the client side will look like @syrusakbary's example.
This is where the relay conventions are documented: https://facebook.github.io/relay/docs/graphql-mutations.html
Awsome that must be my missunderstanding ... I'll give it a go ... Cheers @yfilali
@yfilali I just tried it, but relay gave
Error: Unknown argument "input" on field "newCatalogItem" of type "Mutation".
when compiling the schema ... and if I try it in graphiql it also does not recognise the input option, ie this works.
mutation {
newCatalogItem (id: "Q2F0YWxvZ05vZGU6MQ==", price: 3.11) {
catalogItem {
id
desc
price
}
}
}
It turns out my Graphene libs are probably quite old, could this be it?
$ cat requirements.txt
backports.shutil-get-terminal-size==1.0.0
decorator==4.0.10
Django==1.9.7
django-filter==0.13.0
django-graphiql==0.4.4
djangorestframework==3.4.0
graphene==0.10.2
graphql-core==0.5.3
graphql-django-view==1.3
graphql-relay==0.4.4
ipdb==0.10.1
ipython==5.0.0
ipython-genutils==0.1.0
iso8601==0.1.11
pathlib2==2.1.0
pexpect==4.2.0
pickleshare==0.7.3
Pillow==3.3.0
pkg-resources==0.0.0
promise==0.4.2
prompt-toolkit==1.0.3
ptyprocess==0.5.1
Pygments==2.1.3
simplegeneric==0.8.1
singledispatch==3.4.0.3
six==1.10.0
traitlets==4.2.2
wcwidth==0.1.7
BTW I just tried updating, but got a bunch of other problems ill have to work through first.
Cheers for the help
Since you want a relay mutation, try inheriting relay.ClientIDMutation instead of graphene.Mutation. change your mutate function to a mutate_and_get_payload function and you should be good to go: http://docs.graphene-python.org/en/latest/relay/mutations/
This should help you with the update: https://github.com/graphql-python/graphene/blob/master/UPGRADE-v1.0.md
Awsome !! ... haha I just realised my mistake with relay.ClientIDMutation just as I saw this comment ... sorry that should have been obvious :)
Still not quite right though do I still add the mutation like this
class Mutation(graphene.ObjectType):
new_catalog_item = graphene.Field(NewCatalogItem)
class Meta:
abstract = True
EDIT: when I run it this way I get back "message": "mutate_and_get_payload() takes exactly 4 arguments (3 given)",
that is with a mutation like this
mutation {
newCatalogItem (input: {clientMutationId: "a", id: "Q2F0YWxvZ05vZGU6MQ==", price: 12}) {
clientMutationId
catalogItem {
id
}
}
}
Great! Seems you were able to solve the issue. Closing this then, but feel free to reopen if you have any other question!