gpt4 book ai didi

next.js - 在 next.js api 中使用订阅的 Apollo Server 路由 : websockets trouble

转载 作者:行者123 更新时间:2023-12-04 00:56:13 24 4
gpt4 key购买 nike

我尝试在 next.js 9.x 应用程序中设置 GraphQL 订阅。该应用程序完全是假的,仅用于尝试 Apollo Server 订阅。 “数据库”只是一个数组,我将新用户推送到它。

这是我到目前为止得到的代码。

import { ApolloServer, gql, makeExecutableSchema } from "apollo-server-micro"
import { PubSub } from "apollo-server"

const typeDefs = gql`
type User {
id: ID!
name: String
status: String
}


type Query {
users: [User!]!
user(id: ID!): User
}

type Mutation {
addUser(id: String, name: String, status: String): User
}

type Subscription {
newUser: User!
}
`

const fakedb = [
{
id: "1",
name: "myname",
status: "active",
},
]

const NEW_USER = "NEW_USER"

const resolvers = {
Subscription: {
newUser: {
subscribe: (_, __, { pubsub }) => pubsub.asyncIterator(NEW_USER),
},
},

Query: {
users: (parent, args, context) => {
console.log(context)

return fakedb
},
user: (_, { id }) => {
console.log(id)
console.log(fakedb)

return fakedb.find((user) => user.id == id)
},
},
Mutation: {
addUser(_, { id, name, status }, { pubsub }) {
console.log(pubsub)

const newUser = {
id,
name,
status,
}

pubsub.publish(NEW_USER, { newUser: newUser })

fakedb.push(newUser)
return newUser
},
},
}

export const schema = makeExecutableSchema({
typeDefs,
resolvers,
})

const pubsub = new PubSub()
const apolloServer = new ApolloServer({
// typeDefs,
// resolvers,
schema,
context: ({ req, res }) => {
return { req, res, pubsub }
},
introspection: true,
subscriptions: {
path: "/api/graphql",
// keepAlive: 15000,
onConnect: () => console.log("connected"),
onDisconnect: () => console.log("disconnected"),
},
})

export const config = {
api: {
bodyParser: false,
},
}

export default apolloServer.createHandler({ path: "/api/graphql" })

我在 localhost:3000/api/graphql 中运行此订阅:

订阅{
新用户 {
ID
姓名
}
}

我收到这个错误。我不确定在哪里以及如何解决这个问题,因为我找不到任何关于此的文档。

{
"error": "无法连接到 websocket 端点 ws://localhost:3000/api/graphql。请检查端点 url 是否正确。"
}

我发现了如何添加订阅路径,因为它之前提示过(之前是/graphql)。但仍然无法正常工作。

最佳答案

这就是我让它工作的方式。

import { ApolloServer } from 'apollo-server-micro';
import schema from './src/schema';

const apolloServer = new ApolloServer({
schema,
context: async ({ req, connection }) => {
if (connection) {
// check connection for metadata
return connection.context;
}
// get the user from the request
return {
user: req.user,
useragent: req.useragent,
};
},

subscriptions: {
path: '/api/graphqlSubscriptions',
keepAlive: 9000,
onConnect: console.log('connected'),
onDisconnect: () => console.log('disconnected'),
},
playground: {
subscriptionEndpoint: '/api/graphqlSubscriptions',

settings: {
'request.credentials': 'same-origin',
},
},
});

export const config = {
api: {
bodyParser: false,
},
};

const graphqlWithSubscriptionHandler = (req, res, next) => {
if (!res.socket.server.apolloServer) {
console.log(`* apolloServer first use *`);

apolloServer.installSubscriptionHandlers(res.socket.server);
const handler = apolloServer.createHandler({ path: '/api/graphql' });
res.socket.server.apolloServer = handler;
}

return res.socket.server.apolloServer(req, res, next);
};

export default graphqlWithSubscriptionHandler;
只要确保 websocket 路径有效。 https://www.websocket.org/echo.html

关于next.js - 在 next.js api 中使用订阅的 Apollo Server 路由 : websockets trouble,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62371284/

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