visual-studio-code - 类型 'start' 上不存在属性 'ApolloServer'

标签 visual-studio-code apollo-server tsconfig

我可能错过了一个明显的设置或其他东西,但由于某种原因 VS Code 没有看到 ApolloServer.start,并且我收到了一个内联错误:

Property 'start' does not exist on type 'ApolloServer'.

有人能看到我错过了什么吗?它通过调用服务器上常用的 listen 方法来工作,但我正在尝试添加中间件,这是 apollo's official docs 中记录的流程。 .

tsconfig

{
    "compilerOptions": {
        "baseUrl": ".",
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "types": ["node"],
        "esModuleInterop": true,
    },
    "include": [
        "apollo-server/*"
    ],
    "exclude": ["node_modules"]
}

index.ts

#!/usr/bin/env node

import express from 'express'
import { ApolloServer, gql } from 'apollo-server-express'
import { readFileSync } from 'fs'
import { resolvers } from './resolvers'

const typeDefs = gql`readFileSync('./schema.graphql').toString('utf-8')`

async function startServer() {
    const server = new ApolloServer({
        typeDefs,
        resolvers,
    })
    
    await server.start() // <---- VSCode complains here
    const app = express()
    server.applyMiddleware({ app })
}

最佳答案

更新

这个问题是关于 Apollo Server 2.0 的。此后链接已更改,Apollo 现已推出 3.0 版本。如果您不依赖于版本 2,我建议将您的依赖项更新为 v3。对于那些坚持这一点的人,以下是模式:

// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({ typeDefs, resolvers });

// The `listen` method launches a web server.
server.listen().then(({ url }) => {
  console.log(`🚀  Server ready at ${url}`);
});

Here's a fresh URL to confirm it in the docs

FWIW,我在发布问题后就发现了它。这是一个已弃用的模式。 (已删除过时的链接)

关于visual-studio-code - 类型 'start' 上不存在属性 'ApolloServer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66858790/

相关文章:

javascript - 如何使 "go to definition"适用于 vscode 中的 javascript 网页?

javascript - VSCode 未显示源代码控制 git 面板中的更改

javascript - 在发送之前修改 Graphql 的响应

javascript - 仅使用 typescript(没有 webpack 也没有 bable),我可以获得在浏览器中运行的多文件解决方案吗?

javascript - 有没有办法将 MSBuild 输出目录更改为自定义项目名称?

visual-studio-code - 如何在实时共享期间禁用 Visual Studio Code 中的同步滚动?

node.js - 如何将 Apollo 服务器错误发送给 Sentry?

graphql - Apollo graphql : makeExecutableSchema & playground

node.js - 如何强制 tsc 忽略 node_modules 文件夹?

typescript - Visual Studio Code 中的自动 JSDoc 生成坏了?