2018-09-05 17:45:03 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2018-10-29 22:08:41 +01:00
|
|
|
import { getSetting, singleClientTemplate, Utils, extractCollectionInfo, extractFragmentInfo } from 'meteor/vulcan:lib';
|
2018-06-05 11:51:25 +09:00
|
|
|
|
2018-09-05 16:00:07 +02:00
|
|
|
export default function withSingle(options) {
|
2018-09-05 17:45:03 +02:00
|
|
|
const { pollInterval = getSetting('pollInterval', 20000), enableCache = false, extraQueries } = options;
|
2016-11-22 16:15:00 +09:00
|
|
|
|
2018-09-05 16:00:07 +02:00
|
|
|
const { collectionName, collection } = extractCollectionInfo(options);
|
|
|
|
const { fragmentName, fragment } = extractFragmentInfo(options, collectionName);
|
2018-06-05 11:51:25 +09:00
|
|
|
const typeName = collection.options.typeName;
|
2018-06-05 12:12:04 +09:00
|
|
|
const resolverName = Utils.camelCaseify(typeName);
|
2018-06-05 11:51:25 +09:00
|
|
|
|
2018-09-05 16:00:07 +02:00
|
|
|
const query = gql`
|
|
|
|
${singleClientTemplate({ typeName, fragmentName, extraQueries })}
|
|
|
|
${fragment}
|
|
|
|
`;
|
2018-06-05 11:51:25 +09:00
|
|
|
|
|
|
|
return graphql(query, {
|
|
|
|
alias: `with${typeName}`,
|
2018-09-05 16:00:07 +02:00
|
|
|
|
|
|
|
options({ documentId, slug, selector = { documentId, slug } }) {
|
|
|
|
// OpenCrud backwards compatibility
|
2017-11-01 15:37:57 +09:00
|
|
|
const graphQLOptions = {
|
2018-06-05 11:51:25 +09:00
|
|
|
variables: {
|
2018-06-05 12:12:04 +09:00
|
|
|
input: {
|
2018-08-29 20:36:30 +09:00
|
|
|
selector,
|
2018-09-05 16:00:07 +02:00
|
|
|
enableCache
|
2018-06-05 12:12:04 +09:00
|
|
|
}
|
2018-02-18 18:00:07 +09:00
|
|
|
},
|
2018-09-05 16:00:07 +02:00
|
|
|
pollInterval // note: pollInterval can be set to 0 to disable polling (20s by default)
|
2016-11-22 16:15:00 +09:00
|
|
|
};
|
2017-11-01 15:37:57 +09:00
|
|
|
|
|
|
|
if (options.fetchPolicy) {
|
|
|
|
graphQLOptions.fetchPolicy = options.fetchPolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
return graphQLOptions;
|
2016-11-22 16:15:00 +09:00
|
|
|
},
|
|
|
|
props: returnedProps => {
|
2018-01-25 15:03:03 -06:00
|
|
|
const { /* ownProps, */ data } = returnedProps;
|
2018-06-05 11:51:25 +09:00
|
|
|
|
2018-09-05 17:45:03 +02:00
|
|
|
const propertyName = options.propertyName || 'document';
|
2017-12-28 11:30:13 +09:00
|
|
|
const props = {
|
2017-05-03 14:42:57 +09:00
|
|
|
loading: data.loading,
|
2017-02-03 15:16:48 +09:00
|
|
|
// document: Utils.convertDates(collection, data[singleResolverName]),
|
2018-06-05 12:12:04 +09:00
|
|
|
[propertyName]: data[resolverName] && data[resolverName].result,
|
2016-11-23 11:07:48 +09:00
|
|
|
fragmentName,
|
|
|
|
fragment,
|
2018-09-05 16:00:07 +02:00
|
|
|
data
|
2016-11-22 16:15:00 +09:00
|
|
|
};
|
2017-12-28 11:30:13 +09:00
|
|
|
|
2018-01-04 09:41:48 +09:00
|
|
|
if (data.error) {
|
|
|
|
// get graphQL error (see https://github.com/thebigredgeek/apollo-errors/issues/12)
|
|
|
|
props.error = data.error.graphQLErrors[0];
|
|
|
|
}
|
|
|
|
|
2017-12-28 11:30:13 +09:00
|
|
|
return props;
|
2018-09-05 16:00:07 +02:00
|
|
|
}
|
2016-11-22 16:15:00 +09:00
|
|
|
});
|
2016-12-21 18:14:13 +01:00
|
|
|
}
|