mirror of
https://github.com/vale981/Vulcan
synced 2025-03-10 04:26:41 -04:00
66 lines
1.5 KiB
JavaScript
66 lines
1.5 KiB
JavaScript
/*
|
|
|
|
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 createMovie($data: CreateMovieData) {
|
|
createMovie(data: $data) {
|
|
data {
|
|
_id
|
|
name
|
|
__typename
|
|
}
|
|
__typename
|
|
}
|
|
}
|
|
|
|
Arguments:
|
|
|
|
- data: the document to insert
|
|
|
|
Child Props:
|
|
|
|
- createMovie({ data })
|
|
|
|
*/
|
|
|
|
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';
|
|
|
|
const withCreate = options => {
|
|
const { collectionName, collection } = extractCollectionInfo(options);
|
|
const { fragmentName, fragment } = extractFragmentInfo(options, collectionName);
|
|
|
|
const typeName = collection.options.typeName;
|
|
const query = gql`
|
|
${createClientTemplate({ typeName, fragmentName })}
|
|
${fragment}
|
|
`;
|
|
|
|
// wrap component with graphql HoC
|
|
return graphql(query, {
|
|
alias: `withCreate${typeName}`,
|
|
props: ({ ownProps, mutate }) => ({
|
|
[`create${typeName}`]: args => {
|
|
const { data } = args;
|
|
return mutate({
|
|
variables: { data }
|
|
});
|
|
},
|
|
// OpenCRUD backwards compatibility
|
|
newMutation: args => {
|
|
const { document } = args;
|
|
return mutate({
|
|
variables: { data: document }
|
|
});
|
|
}
|
|
})
|
|
});
|
|
};
|
|
|
|
export default withCreate;
|