2018-01-28 22:24:33 -06:00
|
|
|
/*
|
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
Generic mutation wrapper to upsert a document in a collection.
|
2018-01-28 22:24:33 -06:00
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
Sample mutation:
|
2018-01-28 22:24:33 -06:00
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
mutation upsertMovie($input: UpsertMovieInput) {
|
|
|
|
upsertMovie(input: $input) {
|
|
|
|
data {
|
|
|
|
_id
|
|
|
|
name
|
|
|
|
__typename
|
|
|
|
}
|
|
|
|
__typename
|
2018-01-28 22:24:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
Arguments:
|
2018-01-28 22:24:33 -06:00
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
- input
|
|
|
|
- input.selector: a selector to indicate the document to update
|
|
|
|
- input.data: the document (set a field to `null` to delete it)
|
2018-01-28 22:24:33 -06:00
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
Child Props:
|
2018-01-28 22:24:33 -06:00
|
|
|
|
2018-06-08 13:31:15 +09:00
|
|
|
- upsertMovie({ selector, data })
|
|
|
|
|
|
|
|
*/
|
2018-01-28 22:24:33 -06:00
|
|
|
|
2018-09-05 17:45:03 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
|
|
|
import { upsertClientTemplate } from 'meteor/vulcan:core';
|
|
|
|
import clone from 'lodash/clone';
|
2018-01-28 22:24:33 -06:00
|
|
|
|
2018-10-29 22:08:41 +01:00
|
|
|
import { extractCollectionInfo, extractFragmentInfo } from 'meteor/vulcan:lib';
|
2018-09-05 16:00:07 +02:00
|
|
|
|
|
|
|
const withUpsert = options => {
|
|
|
|
const { collectionName, collection } = extractCollectionInfo(options);
|
|
|
|
const { fragmentName, fragment } = extractFragmentInfo(options, collectionName);
|
2018-01-28 22:24:33 -06: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`
|
|
|
|
${upsertClientTemplate({ typeName, fragmentName })}
|
|
|
|
${fragment}
|
|
|
|
`;
|
2018-01-28 22:24:33 -06:00
|
|
|
|
2018-06-05 16:07:38 +09:00
|
|
|
return graphql(query, {
|
2018-06-04 11:22:49 +09:00
|
|
|
alias: `withUpsert${typeName}`,
|
2018-01-28 22:24:33 -06:00
|
|
|
props: ({ ownProps, mutate }) => ({
|
2018-09-05 16:00:07 +02:00
|
|
|
[`upsert${typeName}`]: args => {
|
2018-06-05 16:07:38 +09:00
|
|
|
const { selector, data } = args;
|
2018-09-05 16:00:07 +02:00
|
|
|
return mutate({
|
2018-08-07 16:20:24 +09:00
|
|
|
variables: { selector, data }
|
2018-06-05 16:07:38 +09:00
|
|
|
// note: updateQueries is not needed for editing documents
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// OpenCRUD backwards compatibility
|
2018-09-05 16:00:07 +02:00
|
|
|
upsertMutation: args => {
|
2018-06-14 23:00:28 +09:00
|
|
|
const { selector, set, unset } = args;
|
2018-06-05 16:07:38 +09:00
|
|
|
const data = clone(set);
|
2018-09-05 16:00:07 +02:00
|
|
|
unset &&
|
|
|
|
Object.keys(unset).forEach(fieldName => {
|
|
|
|
data[fieldName] = null;
|
|
|
|
});
|
|
|
|
return mutate({
|
2018-08-07 16:20:24 +09:00
|
|
|
variables: { selector, data }
|
2018-06-05 16:07:38 +09:00
|
|
|
// note: updateQueries is not needed for editing documents
|
2018-01-28 22:24:33 -06:00
|
|
|
});
|
|
|
|
}
|
2018-09-05 16:00:07 +02:00
|
|
|
})
|
2018-01-28 22:24:33 -06:00
|
|
|
});
|
2018-09-05 16:00:07 +02:00
|
|
|
};
|
2018-01-28 22:24:33 -06:00
|
|
|
|
2018-09-05 16:00:07 +02:00
|
|
|
export default withUpsert;
|