声明: 转载请注明出处
1.依赖引入
import gql from 'graphql-tag';
import { graphql } from 'react-apollo';
import view from '...'; //导入视图界面
2. graphql查询语句
const recActionLogSql = gql `
query logListQuery($skip:Int,$limit:Int,$logMsg:String,$logTime:String){
getLogList(skip:$skip,limit:$limit,logMsg:$logMsg,logTime:$logTime){
id:_id,
logMsg,
logTime
}
getLogCount(logMsg:$logMsg,logTime:$logTime),
}
`;
3. 查询操作
const INIT_PAGE = 10;
const recActionLogQuery = graphql(recActionLogSql, {
options() {
return {
variables{
skip: 0,
limit: INIT_PAGE,
},
fetchPolicy: 'network-only',
};
},
props({ data: { loading, getLogList, getLogCount, fetchMore } }) {
return {
loading,
getLogList,
getLogCount,
loadMoreEntries(eventKey = 1, factor) {
const variables = { skip: ((eventKey - 1) * INIT_PAGE) || 0 };
if (factor.logMsg) {
variables.logMsg = factor.logMsg;
}
if (factor.logTime) {
variables.logTime = factor.logTime;
}
return fetchMore({
variables,
updateQuery: (previousResult, { fetchMoreResult }) => {
if (!fetchMoreResult) {
return previousResult;
}
return Object.assign({}, previousResult, {
getLogList: [...fetchMoreResult.getLogList],
getLogCount: fetchMoreResult.getLogCount,
});
},
});
},
};
},
})(view );
注: 当别的页面要引入view视图页面时,不能使用原来的导出的view页面,得使用加载数据导出后的变量,比如本例中为recActionLogQuery