Apollo Subscriptions: Apollo Graphql is receiving updates on Playground but not on client
我正在对Apollo GraphQL订阅使用React,我可以在Apollo Playground上收到更新,但不能在Client上收到更新。这是在阿波罗游乐场上的回应:
Graphql服务器位于
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import ApolloClient from 'apollo-boost'; import { WebSocketLink } from 'apollo-link-ws'; import { HttpLink } from 'apollo-link-http'; import { split } from 'apollo-link'; import { getMainDefinition } from 'apollo-utilities'; const httpLink = new HttpLink({ uri: 'http://localhost:4000/graphql' }); export const wsLink = new WebSocketLink({ uri: `ws://localhost:4000/graphql`, options: { reconnect: false } }); export const link = split( // split based on operation type ({ query }) => { const definition = getMainDefinition(query); return ( definition.kind === 'OperationDefinition' && definition.operation === 'subscription' ); }, wsLink, httpLink, ); export const client = new ApolloClient({ uri: 'http://localhost:4000/', }); |
在我看来,我使用过
1 2 3 4 5 6 7 | const MESSAGE_SENT_SUBSCRIPTION = gql`subscription { messageSent { id message } }` const {data: newMessage, loading: newMessageLoading} = useSubscription(MESSAGE_SENT_SUBSCRIPTION, {}); |
在渲染时,我使用了:
1 | {!newMessageLoading && JSON.stringify(newMessage)} |
但是从客户端,它没有收到更新,但是我确定它与Graphql WebSockets服务器连接。
服务器端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | let database = require("./src/database.js") let schema = require("./src/schema.js"); let resolvers = require("./src/resolvers.js"); let {ApolloServer} = require("apollo-server"); // The ApolloServer constructor requires two parameters: your schema // definition and your set of resolvers. const server = new ApolloServer({ typeDefs: schema, resolvers: resolvers, context: { database } }); // The `listen` method launches a web server. server.listen().then(({ url,subscriptionsUrl ,subscriptionsPath}) => { console.log(`e??€ Server ready at ${url}`); console.log(`realtime here at ${subscriptionsUrl} and path ${subscriptionsPath}`) }); |
我在这里做错了什么,遇到这个问题的人吗?
我必须从
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import ApolloClient from 'apollo-client'; import { WebSocketLink } from 'apollo-link-ws'; import { HttpLink } from 'apollo-link-http'; import { split } from 'apollo-link'; import { onError } from 'apollo-link-error'; import { InMemoryCache } from 'apollo-cache-inmemory'; import { getMainDefinition } from 'apollo-utilities'; export const httpLink = new HttpLink({ uri:"http://localhost:4000/graphql", // use https for secure endpoint }); // Create a WebSocket link: export const wsLink = new WebSocketLink({ uri:"ws://localhost:4000/subscriptions", // use wss for a secure endpoint options: { reconnect: true } }); // using the ability to split links, you can send data to each link // depending on what kind of operation is being sent export const link = split( // split based on operation type ({ query }) => { const { kind, operation } = getMainDefinition(query); return kind === 'OperationDefinition' && operation === 'subscription'; }, wsLink, httpLink, ); // Instantiate client export const client = new ApolloClient({ link, uri:"http://localhost:4000/graphql", cache: new InMemoryCache() }) |
您需要将拆分的链接传递给ApolloClient构造函数。
尝试像这样通过它(客户端):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import ApolloClient from 'apollo-boost'; import { WebSocketLink } from 'apollo-link-ws'; import { HttpLink } from 'apollo-link-http'; import { split } from 'apollo-link'; import { onError } from 'apollo-link-error'; import { getMainDefinition } from 'apollo-utilities'; const httpLink = new HttpLink({ uri: 'http://localhost:4000/graphql' }); export const wsLink = new WebSocketLink({ uri: `ws://localhost:4000/subscriptions`, options: { reconnect: false } }); export const link = split( // split based on operation type ({ query }) => { const definition = getMainDefinition(query); return ( definition.kind === 'OperationDefinition' && definition.operation === 'subscription' ); }, wsLink, httpLink, ); export const graphqlServer = new ApolloClient({ link: ApolloLink.from([ onError(({ graphQLErrors, networkError }) => { if (graphQLErrors) { graphQLErrors.map(({ message, locations, path }) => console.log( `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}` ) ); } if (networkError) { console.log(`[Network error]: ${networkError}`); } }), link // YOUR LINK (NOW MATCHING YOUR CODE) ]) }); |
和服务器端:
1 2 3 4 5 6 7 8 9 10 11 12 | ... const server = new ApolloServer({ typeDefs: schema, resolvers: resolvers, subscriptions: { path: '/subscriptions' }, context: { database } }); ... |
请注意,