gpt4 book ai didi

reactjs - Apollo 订阅 : Apollo Graphql is receiving updates on Playground but not on client

转载 作者:行者123 更新时间:2023-12-03 13:46:52 25 4
gpt4 key购买 nike

我在 Apollo GraphQL 订阅上使用 React,我可以在 Apollo Playground 上接收更新,但不能在客户端上接收更新。以下是 Apollo Playground 上的回应:

enter image description here

Graphql Server 位于 http://localhost:4000/ 上,订阅位于 ws://localhost:4000/graphql 上。但是,它可以在 Playground 上运行,但不能在客户端运行。我已经以这种方式设置 Apollo 客户端来接收来自服务器的更新:

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/',
});

在我看来,我使用了useSubscriptions:

const MESSAGE_SENT_SUBSCRIPTION =  gql`subscription {
messageSent {
id
message
}
}`
const {data: newMessage, loading: newMessageLoading} = useSubscription(MESSAGE_SENT_SUBSCRIPTION, {});

在渲染时,我使用了:

{!newMessageLoading && JSON.stringify(newMessage)}

但是从客户端,它没有收到更新,但我确信它与 Graphql WebSockets 服务器连接。

oc

服务器端:

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(`🚀 Server ready at ${url}`);
console.log(`realtime here at ${subscriptionsUrl} and path ${subscriptionsPath}`)
});

我在这里做错了什么,有人遇到过这样的问题吗?

最佳答案

您需要将分割链接传递给 ApolloClient 构造函数。尝试像这样传递它(客户端):

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)
])
});

服务器端:

...
const server = new ApolloServer({
typeDefs: schema,
resolvers: resolvers,
subscriptions: {
path: '/subscriptions'
},
context: {
database
}
});
...

请注意,/subscriptions 也传递给 ApolloClient

关于reactjs - Apollo 订阅 : Apollo Graphql is receiving updates on Playground but not on client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58286011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com