visual-studio-code - “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 })
}
解决方案
更新
这个问题是关于 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}`);
});
FWIW,我在发布问题后立即找到了它。这是一种已弃用的模式。(旧链接已删除)
推荐阅读
- mongodb - 如何在猫鼬中实现内连接
- ios - 为什么UILabel TapGesture 只能在标签初始化后工作?
- angular - 如何使用 fort-awesome 将 ngClass 与 fa-icon 元素一起使用
- javascript - 从数组对象中删除重复值
- c# - smtp.gmail.com:远程证书根据验证程序无效:其他服务器工作正常
- javascript - 使用 If 语句,如果变量的属性可能未定义,我如何跳过正在检查的变量?
- c - 在 C 中使用指针时“需要左值作为赋值的左操作数”
- sql - 将身份号码添加到特定列
- swift - iOS oneTimeCode 在某些设备上不起作用
- vue.js - vuetify 'v-app' 标签让一切消失