websocket apollo explorer say“isTrusted”:true when i try use Subscription

gmxoilav  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(143)

伙计们,我有一个问题,当我想使用订阅我面临这个问题,我没有找到一个解决方案在任何地方,我是一个GQL用户,我决定使用订阅,使实时网站,但我面临着一个这个问题,野兔是代码
我试图显示apollo文档,但我面临着另一个问题(graphql找不到我的解析器),所以我试图使用这种速度的代码在我的脑海中它的工作,但问题是它说无法连接wss://localhost:4001我也试图使用无法连接wss://localhost:4001/graphql无法连接wss://localhost:4001/subscription,我也尝试使用ws这三种方式

// my resolver
const somethingChanged = () => {
  // subscribe: () => {
  console.log("subscribe")
  pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC)
  // }
}

const makeId = () => {
  // make id generator 36 symbols
  let id = Math.random().toString(36).split(".")[1]
  pubsub.publish(SOMETHING_CHANGED_TOPIC, {
    somethingChanged: {
      id,
    },
  })
  return id
}

const resolvers = {
  Subscription: {
    somethingChanged,
  },
  Query: {
    hello: () => "Hello world!",
  },
  Mutation: {
    makeId,
  },
}

个字符
我只是想测试在阿波罗探险家,但它不工作

zy1mlcev

zy1mlcev1#

我想你错过了一些步骤,请按照这个代码

import { ApolloServer } from "apollo-server-express"
import { ApolloServerPluginDrainHttpServer } from "apollo-server-core"
import express from "express"
import graphSchema from "./graphql/schema"
import resolvers from "./graphql/resolvers"
import { createServer } from "http"
import connect from "./db/db"
import "colors"
import { makeExecutableSchema } from "@graphql-tools/schema"
import { execute, subscribe } from "graphql"
import { SubscriptionServer } from "subscriptions-transport-ws"
import getUser from "./helpers/getUser"
import { setUserData, userData, Req, setReq } from "./helpers/localData"
import cors from "cors"
require("dotenv").config()
const PORT = process.env.PORT || 4003
const startServer = async () => {
  const schema = makeExecutableSchema({ typeDefs: graphSchema, resolvers })
  const app = express()
  const httpServer = createServer(app)
  const subscriptionServer = SubscriptionServer.create(
    {
      schema,
      execute,
      subscribe,
    },
    { server: httpServer, path: "/graphql" }
  )

  setInterval(() => {
    console.log(
      `Server was work, on port ${PORT}, time ${
        new Date().toLocaleTimeString().blue
      } , process pid: ${process.pid}`.green
    )
  }, 2000)
  app.use(cors())
  // database connected
  connect()
  // apollo server was created
  const apolloServer = new ApolloServer({
    typeDefs: schema,
    resolvers,
    schema,
    // context
    context: async ({ req }) => {
      setReq(req)
      const token = req.headers.authorization || ""
      const user = (await getUser(token)) || ""
      setUserData(user)
      return { user, req }
    },
    // for subscriptions
    plugins: [
      ApolloServerPluginDrainHttpServer({ httpServer }),
      {
        async serverWillStart() {
          return {
            async drainServer() {
              subscriptionServer.close()
            },
            context: async (req: any) => {
              const token = req.headers.authorization || ""
              const user = await getUser(token)
              return { user, req }
            },
          }
        },
      },
    ],
  })
  // apollo server was started
  await apolloServer.start()

  app.use("/api", require("./routes"))

  apolloServer.applyMiddleware({
    app,
    path: "/graphql",
  })
  // apollo server was start with express server
  httpServer.listen(PORT, () =>
    console.log(
      Server listening on http://localhost:${PORT}${apolloServer.graphqlPath}
        .blue
    )
  )
}
startServer() // start server

字符串

相关问题