2016-11-26 11:17:43 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Generic mutation wrapper to insert a new document in a collection and update
|
|
|
|
a related query on the client with the new item and a new total item count.
|
|
|
|
|
|
|
|
Sample mutation:
|
|
|
|
|
|
|
|
mutation moviesNew($document: MoviesInput) {
|
|
|
|
moviesNew(document: $document) {
|
|
|
|
...MoviesNewFormFragment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
- document: the document to insert
|
|
|
|
|
|
|
|
Child Props:
|
|
|
|
|
|
|
|
- newMutation(document)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
|
|
|
|
|
|
|
export default function withNew(options) {
|
|
|
|
|
|
|
|
// get options
|
2016-12-05 09:29:49 +09:00
|
|
|
const { collection, fragment } = options,
|
2016-12-01 12:13:27 +09:00
|
|
|
fragmentName = fragment.definitions[0].name.value,
|
2016-11-26 11:17:43 +09:00
|
|
|
collectionName = collection._name,
|
2016-12-05 09:29:49 +09:00
|
|
|
mutationName = collection.options.mutations.new.name;
|
2016-11-26 11:17:43 +09:00
|
|
|
|
|
|
|
// wrap component with graphql HoC
|
|
|
|
return graphql(gql`
|
|
|
|
mutation ${mutationName}($document: ${collectionName}Input) {
|
|
|
|
${mutationName}(document: $document) {
|
|
|
|
...${fragmentName}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${fragment}
|
|
|
|
`, {
|
|
|
|
props: ({ownProps, mutate}) => ({
|
|
|
|
newMutation: ({document}) => {
|
|
|
|
return mutate({
|
|
|
|
variables: { document },
|
2016-11-30 10:31:19 +01:00
|
|
|
});
|
2016-11-26 11:17:43 +09:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2016-11-27 08:39:25 +09:00
|
|
|
}
|