typescript 服务器出现问题,applyMiddleware({app})函数不接受任何参数

h9vpoimq  于 2022-11-30  发布在  TypeScript
关注(0)|答案(1)|浏览(103)

我正在尝试做的是订阅typeScript和apolloserver。我看过很多教程,其中大多数都是这样做的,这对他们很有效。

import { ApolloServer } from "apollo-server";
import { connectDB } from "./mongodb";
import { typeDefs } from "./schema";
import Query from "./resolvers/Query";
import Mutation from "./resolvers/mutation";
import dotenv from "dotenv";
//dotenv.config()
import { createServer } from "http";
import { ApolloServerPluginDrainHttpServer } from "apollo-server-core";
import { makeExecutableSchema } from "@graphql-tools/schema";
import { WebSocketServer } from "ws";
import { useServer } from "graphql-ws/lib/use/ws";
import express from "express";
import { SubscriptionServer } from "subscriptions-transport-ws";
import { PubSub } from 'graphql-subscriptions';
import { execute,subscribe } from "graphql";
const run = async () => {
  dotenv.config();
  const pubsub = new PubSub();
  const db = await connectDB();
  const port = process.env.PORT;

const app = express();
const httpServer=createServer(app)
const schema = makeExecutableSchema({
    typeDefs,
    resolvers: {
        Query,
        Mutation,
    },
    });

/*  
  const wsServer = new WebSocketServer({

    server: httpServer,
 
    path: "/graphql",
  });

  const serverCleanup = useServer({ schema }, wsServer);
   */

  const server = new ApolloServer({
    schema,
    typeDefs,
    context: ({ req, res }) => ({ req, res, pubsub }),
    plugins: [ApolloServerPluginDrainHttpServer({ httpServer }), {
        async serverWillStart() {
            return {
                async drainServer() {
                    subscriptionServer.close();
                }
            };
        }
    }],
    });
    const subscriptionServer = SubscriptionServer.create({
    
        schema,
      
        execute,
        subscribe,
        
        async onConnect(
            connectionParams:any,
            webSocket:any,
            context:any
        ){
            console.log('Connected!');  
            return {pubsub}    
        },
        onDisconnect() {
            console.log('Disconnected!')
        }
    }, {
        // This is the `httpServer` we created in a previous step.
        server: httpServer,
        // This `server` is the instance returned from `new ApolloServer`.
        path: server.graphqlPath,
    });



  /* server.listen(port).then((url) => {
    console.log("Server escuchando en el puerto 2000");
  }); */
  await server.start();

  server.applyMiddleware({app}) //Error 
  
    httpServer.listen(port, () => {
    console.log(
      `Server is now running on http://localhost:${port}${server.graphqlPath}`,
    );
  }); 
};
try {
  run();
} catch (error) {
  console.log(error);
}

[All我从这里收集的信息] https://www.apollographql.com/docs/apollo-server/data/subscriptions

相关问题