2016-11-22 16:15:00 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
|
|
|
|
2016-12-12 09:54:16 +09:00
|
|
|
export default function withDocument (options) {
|
2016-12-01 12:13:27 +09:00
|
|
|
|
2017-01-10 10:07:13 +01:00
|
|
|
const { queryName, collection, fragment, pollInterval } = options,
|
2016-12-01 12:13:27 +09:00
|
|
|
fragmentName = fragment.definitions[0].name.value,
|
2016-11-23 17:22:29 +09:00
|
|
|
singleResolverName = collection.options.resolvers.single.name;
|
2016-11-22 16:15:00 +09:00
|
|
|
|
|
|
|
return graphql(gql`
|
2016-12-07 15:30:40 +09:00
|
|
|
query ${queryName}($documentId: String, $slug: String) {
|
|
|
|
${singleResolverName}(documentId: $documentId, slug: $slug) {
|
2016-11-22 16:15:00 +09:00
|
|
|
...${fragmentName}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${fragment}
|
|
|
|
`, {
|
|
|
|
options(ownProps) {
|
|
|
|
return {
|
2016-12-07 15:30:40 +09:00
|
|
|
variables: { documentId: ownProps.documentId, slug: ownProps.slug },
|
2017-01-10 10:07:13 +01:00
|
|
|
pollInterval: pollInterval || 0, // pollInterval can be set to 0 to disable polling (disabled by default)
|
2016-11-22 16:15:00 +09:00
|
|
|
};
|
|
|
|
},
|
|
|
|
props: returnedProps => {
|
|
|
|
const { ownProps, data } = returnedProps;
|
|
|
|
return {
|
2016-12-21 18:14:13 +01:00
|
|
|
loading: data.networkStatus === 1,
|
2016-11-23 11:07:48 +09:00
|
|
|
document: data[singleResolverName],
|
|
|
|
fragmentName,
|
|
|
|
fragment,
|
2016-11-22 16:15:00 +09:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
2016-12-21 18:14:13 +01:00
|
|
|
}
|