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-08-07 16:20:24 +09:00
|
|
|
mutation createMovie($data: CreateMovieData) {
|
|
|
|
createMovie(data: $data) {
|
2018-06-08 13:31:15 +09:00
|
|
|
data {
|
|
|
|
_id
|
|
|
|
name
|
|
|
|
__typename
|
|
|
|
}
|
|
|
|
__typename
|
2016-11-26 11:17:43 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
2018-08-07 16:20:24 +09:00
|
|
|
- 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
|
|
|
|
|
|
|
*/
|
|
|
|
|
2018-09-05 16:00:07 +02:00
|
|
|
import React, { Component } from "react";
|
|
|
|
import { graphql } from "react-apollo";
|
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { createClientTemplate } from "meteor/vulcan:core";
|
|
|
|
import { extractCollectionInfo, extractFragmentInfo } from "./handleOptions";
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-09-05 16:00:07 +02:00
|
|
|
const withCreate = options => {
|
|
|
|
const { collectionName, collection } = extractCollectionInfo(options);
|
|
|
|
const { fragmentName, fragment } = extractFragmentInfo(options, collectionName);
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
const typeName = collection.options.typeName;
|
2018-09-05 16:00:07 +02:00
|
|
|
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}`,
|
2018-09-05 16:00:07 +02:00
|
|
|
props: ({ ownProps, mutate }) => ({
|
|
|
|
[`create${typeName}`]: args => {
|
2018-06-05 16:07:38 +09:00
|
|
|
const { data } = args;
|
2018-09-05 16:00:07 +02:00
|
|
|
return mutate({
|
|
|
|
variables: { data }
|
2018-06-02 18:49:53 +09:00
|
|
|
});
|
|
|
|
},
|
|
|
|
// OpenCRUD backwards compatibility
|
2018-09-05 16:00:07 +02:00
|
|
|
newMutation: args => {
|
2018-06-05 16:07:38 +09:00
|
|
|
const { document } = args;
|
2018-09-05 16:00:07 +02:00
|
|
|
return mutate({
|
|
|
|
variables: { data: document }
|
2016-11-30 10:31:19 +01:00
|
|
|
});
|
2016-11-26 11:17:43 +09:00
|
|
|
}
|
2018-09-05 16:00:07 +02:00
|
|
|
})
|
2016-11-26 11:17:43 +09:00
|
|
|
});
|
2018-09-05 16:00:07 +02:00
|
|
|
};
|
2016-11-26 11:17:43 +09:00
|
|
|
|
2018-09-05 16:00:07 +02:00
|
|
|
export default withCreate;
|