我正在研究 Spring 服务器和 Next.js 客户端之间的 GraphQL 通信。最近,我更改了服务器的 GraphQL 依赖项(我正在转换为 WebFlux),此时我的订阅停止工作 - 服务器立即使用
4400
代码关闭 websocket 连接。
具有相同负载的相同订阅在
/graphiql
上运行得很好。
当我切换到此依赖项时,这种情况开始发生:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
<version>${spring.boot.version}</version>
</dependency>
和这段代码:
@Controller
public class PageController {
@Autowired
private PagePublisher pagePublisher;
@SubscriptionMapping("scrapesPerformed")
public Publisher<String> getScrapesPerformed() {
return pagePublisher.getScrapesPublisher();
}
}
在之前的工作实现中,我使用了此依赖项:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
并像这样曝光出版商:
@Component
public class PageSubscription implements GraphQLSubscriptionResolver {
@Autowired
private PagePublisher pagePublisher;
public Publisher<String> getScrapesPerformed() {
return pagePublisher.getScrapesPublisher();
}
}
如果有人感兴趣 - 我在前端使用
Apollo Client
,就像这样(这里没有任何改变):
const httpLink = new HttpLink({
uri: SPRING_SERVER_ENDPOINT,
});
const wsLink = process.browser ? new WebSocketLink({
uri: SPRING_SERVER_SUBSCRIPTIONS_ENDPOINT,
options: {
reconnect: true
},
}) : null;
const link = process.browser ? split(
({ query }) => {
const { kind, operation }: Definintion = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink,
) : httpLink;
const cache = new InMemoryCache();
const client = new ApolloClient({ link, cache });
export default client;
与
useSubscription
:
const SUBSCRIBE_TO_SCRAPES_PERFORMED = gql`
subscription Subscription {
scrapesPerformed
}
`;
export default function SpringServerHeartbeat() {
const { data, loading } = useSubscription(
SUBSCRIBE_TO_SCRAPES_PERFORMED
);
const subscription_data = (
loading ?
<div className='spinner-border mx-auto'/>
:
data ?
<h4>
{data.scrapesPerformed}
<br/>
</h4>
:
null
);
return subscription_data;
};
有一些 Websocket 知识的人(我没有:D)可以帮我至少解释一下这意味着什么吗?我用谷歌搜索了这条错误请求消息,但什么也没找到;而且我很确定有效负载是正确的...
因此,解决方案是从旧的
graphql-transport-ws
协议(在之前的实现中在我的服务器上使用)切换到客户端的 graphql-ws
。