没有导出成员'graphqlKoa',并尝试了所有配置选项

时间:2018-09-19 09:20:20

标签: typescript graphql koa

我一直在绕着这个问题奔跑,我试图用graphql设置一个打字稿koa后端,但一直出现此错误。

[nodemon] starting `ts-node --inspect -r tsconfig-paths/register ./src/server.ts`

/home/wouter/dev/project/node_modules/ts-node/src/index.ts:261
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
src/routes.ts(4,10): error TS2305: Module '"/home/wouter/dev/project/node_modules/apollo-server-koa/dist/index"' has no exported member 'graphqlKoa'.
src/routes.ts(4,22): error TS2305: Module '"/home/wouter/dev/project/node_modules/apollo-server-koa/dist/index"' has no exported member 'graphiqlKoa'.
src/routes.ts(6,24): error TS2307: Cannot find module 'graphql/schema'.

    at createTSError (/home/wouter/dev/project/node_modules/ts-node/src/index.ts:261:12)
    at getOutput (/home/wouter/dev/project/node_modules/ts-node/src/index.ts:367:40)
    at Object.compile (/home/wouter/dev/project/node_modules/ts-node/src/index.ts:558:11)
    at Module.m._compile (/home/wouter/dev/project/node_modules/ts-node/src/index.ts:439:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/home/wouter/dev/project/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)
    at Module.require (internal/modules/cjs/loader.js:598:17)
[nodemon] app crashed - waiting for file changes before starting...

这是route.ts文件

import Router from 'koa-router';
import koaBody from 'koa-bodyparser';

import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa';

import { schema } from 'graphql/schema';


export const routes = new Router();

// API entrypoint
const apiEntrypointPath = '/graphql';
const graphQlOpts = graphqlKoa({
  schema,
});

routes.get(apiEntrypointPath, graphQlOpts);
routes.post(apiEntrypointPath, koaBody(), graphQlOpts);

// GraphiQL entrypoint
routes.get('/graphiql', graphiqlKoa({ endpointURL: apiEntrypointPath }));

这是我的tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
      "target": "es6",
      "strict": false,
      "module": "commonjs",
      "moduleResolution": "node",
      "noImplicitAny": false,
      "emitDecoratorMetadata": true,
      "allowSyntheticDefaultImports": true,
      "experimentalDecorators": true,
      "esModuleInterop": true,
      "sourceMap": true,
      "baseUrl": "./src"

    },
    "exclude": [
      "node_modules",
      "client",
      "**/*.spec.ts"
    ]
}

我能找到的最多信息是我需要将“ esModuleInterop”更改为true,但这无济于事。我也尝试添加skipLibCheck,但仍然没有运气。据我所知,我确实安装了正确的类型,所以我想知道发生了什么事?

1 个答案:

答案 0 :(得分:2)

正如错误所暗示的,apollo-server-koa模块没有名为graphqlKoa的导出。从2.0版开始,Apollo Server的API发生了巨大变化。如果您使用的是apollo-server-koa的最新版本,则代码应看起来像这样:

import Koa from 'koa'
import { ApolloServer } from 'apollo-server-koa'
import { typeDefs, resolvers } from './somewhere' 

const server = new ApolloServer({ typeDefs, resolvers })
const app = new Koa()

server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
  console.log(`Server ready at http://localhost:4000${server.graphqlPath}`),
)

如果您不使用任何其他路由,也可以通过使用apollo-server软件包来设置“独立”服务器:

import { ApolloServer } from 'apollo-server'
import { typeDefs, resolvers } from './somewhere' 

const server = new ApolloServer({ typeDefs, resolvers })

server.listen().then(({ url }) => {
  console.log(` Server ready at ${url}`);
});