使用Typescript时解析器的Apollo GraphQL Schema Stitching问题

时间:2019-06-24 02:08:26

标签: typescript graphql apollo-server

上下文

我在打字稿快递应用中使用模式拼接(如here所示)。

问题

在不同文件中创建解析器似乎会导致生成问题。但是,可以在app.ts中创建解析器(如下所示)。

目录结构

app
├── app.ts
├── resolvers
│   ├── graphql-user-resolver.ts
│   └── index.ts
└── schema
    ├── graphql-user-schema.ts
    └── index.ts

app / app.ts

import express = require('express');
import { ApolloServer } from 'apollo-server-express';
import schema from './schema';

const app: express.Application = express();

import resolvers from './resolvers';

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

server.applyMiddleware({app, path: '/graphql'});
app.listen({port: 8000}, () => {
  console.log('http://localhost:8000/graphql');
});

app / resolvers / graphql-user-resolver.ts

export default {
  Query: {
    me: () => {
      return {
        username: 'username',
      };
    },

  },
};

app / resolvers / index.ts

import graphqlUserResolver from './graphql-user-resolver';

export default [
  graphqlUserResolver,
];

构建问题

app/app.ts:11:3 - error TS2322: Type '{ Query: { me: () => { username: string; }; }; }[]' is not assignable to type 'IResolvers<any, any>'.
  Index signature is missing in type '{ Query: { me: () => { username: string; }; }; }[]'.

11   resolvers,
     ~~~~~~~~~

  node_modules/apollo-server-core/dist/types.d.ts:31:5
    31     resolvers?: IResolvers;
           ~~~~~~~~~
    The expected type comes from property 'resolvers' which is declared here on type 'ApolloServerExpressConfig'


Found 1 error.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! server@0.0.1 tsc: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the server@0.0.1 tsc script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/.../.npm/_logs/2019-06-24T02_05_04_497Z-debug.log

resolvers中定义app.ts可以正常工作:

const resolvers = {
  Query: {
    me: () => {
      return {
        username: 'username',
      };
    },
  },
};

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

1 个答案:

答案 0 :(得分:0)

打字稿在这里无关紧要。使用JS传播或分别写入对象。喜欢:

export default x;

queries['Type'] = x;
相关问题