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-08 13:31:15 +09:00
|
|
|
mutation createMovie($input: CreateMovieInput) {
|
|
|
|
createMovie(input: $input) {
|
|
|
|
data {
|
|
|
|
_id
|
|
|
|
name
|
|
|
|
__typename
|
|
|
|
}
|
|
|
|
__typename
|
2016-11-26 11:17:43 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
- input
|
|
|
|
- input.data: the document to insert
|
2016-11-26 11:17:43 +09:00
|
|
|
|
|
|
|
Child Props:
|
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
- createMovie({ data })
|
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-08 13:31:15 +09:00
|
|
|
const 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-06-08 13:31:15 +09:00
|
|
|
const collection = options.collection || getCollection(collectionName);
|
|
|
|
const fragment = options.fragment || getFragment(options.fragmentName || `${collectionName}DefaultFragment`);
|
|
|
|
const fragmentName = getFragmentName(fragment);
|
|
|
|
const typeName = collection.options.typeName;
|
|
|
|
const 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
|
|
|
}
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export default withCreate;
|