Vulcan/packages/vulcan-core/lib/modules/containers/withDocument.js

43 lines
1.4 KiB
JavaScript
Raw Normal View History

import React, { PropTypes, Component } from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
2017-03-23 16:27:59 +09:00
import { getFragment, getFragmentName } from 'meteor/vulcan:core';
2016-12-12 09:54:16 +09:00
export default function withDocument (options) {
const { collection, pollInterval = 20000 } = options,
queryName = options.queryName || `${collection.options.collectionName}SingleQuery`,
fragment = options.fragment || getFragment(options.fragmentName),
fragmentName = getFragmentName(fragment),
2017-02-22 18:04:59 +01:00
singleResolverName = collection.options.resolvers.single && collection.options.resolvers.single.name;
return graphql(gql`
query ${queryName}($documentId: String, $slug: String) {
${singleResolverName}(documentId: $documentId, slug: $slug) {
__typename
...${fragmentName}
}
}
${fragment}
`, {
2017-01-14 08:29:51 +09:00
alias: 'withDocument',
options(ownProps) {
return {
variables: { documentId: ownProps.documentId, slug: ownProps.slug },
2017-01-11 10:36:08 +01:00
pollInterval, // note: pollInterval can be set to 0 to disable polling (20s by default)
};
},
props: returnedProps => {
const { ownProps, data } = returnedProps;
return {
loading: data.networkStatus === 1,
// document: Utils.convertDates(collection, data[singleResolverName]),
document: data[singleResolverName],
2016-11-23 11:07:48 +09:00
fragmentName,
fragment,
};
},
});
}