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:
|
|
|
|
|
2018-06-02 18:49:53 +09:00
|
|
|
mutation updateMovie($document: MoviesInput) {
|
|
|
|
updateMovie(document: $document) {
|
|
|
|
...MovieFormFragment
|
2016-11-26 11:17:43 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
- document: the document to insert
|
|
|
|
|
|
|
|
Child Props:
|
|
|
|
|
2018-06-02 18:49:53 +09:00
|
|
|
- createMovie(document)
|
2016-11-26 11:17:43 +09:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2018-06-05 16:07:38 +09:00
|
|
|
import { getFragment, getFragmentName, getCollection, createClientTemplate } from 'meteor/vulcan:core';
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-06-02 18:49:53 +09:00
|
|
|
export default function withCreate(options) {
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-01-27 18:53:28 -07:00
|
|
|
const { collectionName } = options;
|
2016-11-26 11:17:43 +09:00
|
|
|
// get options
|
2018-01-27 18:53:28 -07:00
|
|
|
const collection = options.collection || getCollection(collectionName),
|
2018-06-05 16:07:38 +09:00
|
|
|
fragment = options.fragment || getFragment(options.fragmentName || `${collectionName}DefaultFragment`),
|
2017-02-03 15:16:48 +09:00
|
|
|
fragmentName = getFragmentName(fragment),
|
2018-06-05 16:07:38 +09:00
|
|
|
typeName = collection.options.typeName,
|
|
|
|
query = gql`${createClientTemplate({ typeName, fragmentName })}${fragment}`;
|
2016-11-26 11:17:43 +09:00
|
|
|
|
|
|
|
// wrap component with graphql HoC
|
2018-06-05 16:07:38 +09:00
|
|
|
return graphql(query, {
|
2018-06-02 18:49:53 +09:00
|
|
|
alias: `withCreate${typeName}`,
|
2016-11-26 11:17:43 +09:00
|
|
|
props: ({ownProps, mutate}) => ({
|
2018-06-05 16:07:38 +09:00
|
|
|
[`create${typeName}`]: (args) => {
|
|
|
|
const { data } = args;
|
2018-06-02 18:49:53 +09:00
|
|
|
return mutate({
|
2018-06-05 16:07:38 +09:00
|
|
|
variables: { input: { data } },
|
2018-06-02 18:49:53 +09:00
|
|
|
});
|
|
|
|
},
|
|
|
|
// OpenCRUD backwards compatibility
|
2018-06-05 16:07:38 +09:00
|
|
|
newMutation: (args) => {
|
|
|
|
const { document } = args;
|
2016-11-26 11:17:43 +09:00
|
|
|
return mutate({
|
2018-06-05 16:07:38 +09:00
|
|
|
variables: { input: { data: 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
|
|
|
}
|