使用 Typescript 时解析器的 Apollo GraphQL 模式拼接问题
Apollo GraphQL Schema Stitching issue with Resolvers when using Typescript
上下文
我在 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 在这里无关紧要。要么使用 JS 传播,要么单独写入一个对象。喜欢:
export default x;
queries['Type'] = x;
上下文
我在 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 在这里无关紧要。要么使用 JS 传播,要么单独写入一个对象。喜欢:
export default x;
queries['Type'] = x;