我将发布大量代码用于上下文。我还没有看过显示客户端和服务器的教程,所以我试图把它们拼凑起来。
const subscriptionClient = new SubscriptionClient(`ws://localhost:8031/`, {
reconnect: true
});
const networkInterface = createNetworkInterface({
uri: 'http://localhost:8030/graphql'
});
const subscriptionInterface = addGraphQLSubscriptions(networkInterface, subscriptionClient);
const apolloClient = new ApolloClient({
networkInterface: subscriptionInterface
});
const SUBSCRIPTION_QUERY = gql`
subscription onBranchUpdated($id: UUID!) {
branchUpdated(id: $id) {
browser
crawlId
startDate
endDate
baseUrl
totalPages
currentPage
}
}
`;
class BranchPage extends React.PureComponent {
render() {
*snip*
}
componentDidMount() {
this.props.data.subscribeToMore({
document: SUBSCRIPTION_QUERY,
variables: {
id: this.props.match.params.id,
},
updateQuery: (prevResult, {subscriptionData}) => {
log('updateQuery',prevResult,subscriptionData);
}
});
}
}
export default graphql(gql`
query getBranch($id: UUID!) {
branch(id: $id) {
id
browser
crawlId
startDate
endDate
baseUrl
totalPages
currentPage
}
}
`, {
options: ({match: {params}}) => ({
variables: {
id: params.id,
}
})
})(BranchPage);
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
type Branch {
id: UUID!
*snip*
}
type Subscription {
branchUpdated(id: UUID!): Branch
}
const resolverMap = {
Query: {
async branch(obj, {id}, context, info) {
*snip*
return branch;
},
},
Mutation: {
async updateBranch(_, req) {
let {id, ...updates} = req;
let branch = await db::queryOne("select * from Branches where id=?", [id]);
updates = getUpdates(req);
Object.assign(branch, updates);
await db::mutate("update Branches set ? where id=?", [updates, id]);
pubSub.publish(topics.BRANCH_UPDATED, {branchUpdated: branch});
return branch;
},
},
Subscription: {
branchUpdated: {
resolve: (payload, args, context, info) => {
log('RESSSSSSSSSSSSSSSSoLVE',payload,args);
return payload;
},
subscribe: withFilter(() => pubSub.asyncIterator(topics.BRANCH_UPDATED), (payload, variables) => {
log('FILTERRRRRRRRRRRRRRRRRRRRRRR');
return payload.branchUpdated.id === variables.id;
}),
},
},
};
import { PubSub } from 'graphql-subscriptions';
const pubSub = new PubSub();
export default pubSub;
const wsServer = createServer((request, response) => {
response.writeHead(404);
response.end();
});
wsServer.listen(WS_PORT, () => {
console.log(`Websocket server: http://localhost:${WS_PORT}`)
});
const subscriptionServer = SubscriptionServer.create(
{
schema,
execute,
subscribe,
},
{
server: wsServer,
path: '/',
}
);
当我使用此组件加载页面时,它使用网络接口加载数据并正确呈现。之后,我想组件通过websocket订阅更新以使其保持最新。那是我遇到麻烦的部分。
在Chrome开发工具中,我可以看到“updateQuery”立即被点击,虽然我不知道为什么。目前还没有“公布”任何内容。 subscriptionData
出现为:
{
data: {
branchUpdated: null
}
}
在服务器上,我可以看到原因。在我的resolverMap
中,Subscription.branchUpdated.resolve
会立即被点击。 payload
可能是undefined
,可能是因为我尚未发布任何内容。 Subscription.branchUpdated.subscribe
永远不会被执行。
当我做向我的“分支”发布更新时,没有任何反应。即使我已调用branchUpdated
,也无法调用subscribe
和pubSub.publish
。没有任何东西返回给客户。
所以我的问题是:
答案 0 :(得分:0)
终于明白了。有一些问题。
graphql@0.9.6
没有{subscribe}
,您至少需要v0.10 withFilter
错了。 branch.id
是一个缓冲区 - 我需要使用.equals
来比较它们我更新了componentDidMount
:
componentDidMount() {
this.props.data.subscribeToMore({
document: SUBSCRIPTION_QUERY,
variables: {
id: this.props.match.params.id,
},
updateQuery: (prevResult, {subscriptionData}) => {
log('updateQuery',prevResult,subscriptionData.data.branchUpdated);
return {branch: subscriptionData.data.branchUpdated};
}
});
}