Vulcan/packages/vulcan-core/lib/modules/containers/withSingle.js
Eric Burel 17f96712ff cleaned up the options management in hocs
Will fix issues when not specifying a fragmentName in withDelete
2018-09-05 16:53:51 +02:00

64 lines
2 KiB
JavaScript

import React, { Component } from "react";
import PropTypes from "prop-types";
import { graphql } from "react-apollo";
import gql from "graphql-tag";
import { getSetting, singleClientTemplate, Utils } from "meteor/vulcan:lib";
import { extractCollectionInfo, extractFragmentInfo } from "./handleOptions";
export default function withSingle(options) {
const { pollInterval = getSetting("pollInterval", 20000), enableCache = false, extraQueries } = options;
const { collectionName, collection } = extractCollectionInfo(options);
const { fragmentName, fragment } = extractFragmentInfo(options, collectionName);
const typeName = collection.options.typeName;
const resolverName = Utils.camelCaseify(typeName);
const query = gql`
${singleClientTemplate({ typeName, fragmentName, extraQueries })}
${fragment}
`;
return graphql(query, {
alias: `with${typeName}`,
options({ documentId, slug, selector = { documentId, slug } }) {
// OpenCrud backwards compatibility
const graphQLOptions = {
variables: {
input: {
selector,
enableCache
}
},
pollInterval // note: pollInterval can be set to 0 to disable polling (20s by default)
};
if (options.fetchPolicy) {
graphQLOptions.fetchPolicy = options.fetchPolicy;
}
return graphQLOptions;
},
props: returnedProps => {
const { /* ownProps, */ data } = returnedProps;
const propertyName = options.propertyName || "document";
const props = {
loading: data.loading,
// document: Utils.convertDates(collection, data[singleResolverName]),
[propertyName]: data[resolverName] && data[resolverName].result,
fragmentName,
fragment,
data
};
if (data.error) {
// get graphQL error (see https://github.com/thebigredgeek/apollo-errors/issues/12)
props.error = data.error.graphQLErrors[0];
}
return props;
}
});
}