首页 > 解决方案 > “ApolloServer”类型上不存在属性“start”

问题描述

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

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

谁能看到我错过了什么?它通过调用listen服务器上的常用方法来工作,但我正在尝试添加中间件,这是apollo 官方文档中记录的流程。

tsconfig

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

索引.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 })
}

标签: visual-studio-codeapollo-servertsconfig

解决方案


更新

这个问题是关于 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}`);
});

这是在文档中确认的新 URL

FWIW,我在发布问题后立即找到了它。这是一种已弃用的模式。(旧链接已删除)


推荐阅读