我可能错过了一个明显的设置或其他东西,但由于某种原因 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/