【问题标题】:Apollo GraphQL Schema Stitching issue with Resolvers when using Typescript使用 Typescript 时解析器出现 Apollo GraphQL Schema Stitching 问题
【发布时间】:2019-11-05 19:40:18
【问题描述】:

上下文

我在 typescript express 应用程序中使用模式拼接(如 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

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

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

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

【问题讨论】:

    标签: typescript graphql apollo-server


    【解决方案1】:

    Typescript 在这里无关紧要。要么使用 JS 传播,要么单独写入 Object。喜欢:

    export default x;
    
    queries['Type'] = x;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2020-01-18
      • 2019-04-06
      • 2020-08-12
      • 2021-01-25
      • 2020-01-28
      • 1970-01-01
      相关资源
      最近更新 更多