我第一次尝试使用Express的GraphQL。我声明了一个模式:
import { makeExecutableSchema } from "graphql-tools";
import { interfaces } from "inversify";
const schema = `
type Feature {
id: Int!
name: String
description: String
roles: [Feature]
}
type Role {
id: Int!
name: String
description: String
roles: [Feature]
}
type User {
id: Int!
fullname: String
email: String
profilePicUrl: String
status: String
roles: [Role]
}
type Query {
users: [User]
roles: [Role]
features: [Feature]
role(id: Int!): Author
}
schema {
query: Query
}
`;
const resolveFunctions = {
Query: {
users(obj: any, args: any, context: { container: interfaces.Container }) {
return [];
},
roles(obj: any, args: any, context: { container: interfaces.Container }) {
return [];
},
features(obj: any, args: any, context: { container: interfaces.Container }) {
return [];
}
},
User: {
roles(userId: number) {
return [];
}
},
Feature: {
roles(featureId: number) {
return [];
},
},
Role: {
users(roleId: number) {
return [];
},
features(roleId: number) {
return [];
}
},
};
const executableSchema = makeExecutableSchema({
typeDefs: schema,
resolvers: resolveFunctions,
});
export { executableSchema };我导入了executableSchema并尝试创建一个graphqlExpress实例,但得到一个编译错误:
import { executableSchema } from "./schema";
import { graphqlExpress } from "graphql-server-express";
// ...
app.use("/graphql", graphqlExpress((req) => { // Error!
return {
schema: executableSchema,
context: {
container: container
}
};
}));错误如下:
'Argument of type '(req: Request | undefined) => { schema: GraphQLSchema; context: { container: Container; }; }'
is not assignable to parameter of type
'GraphQLServerOptions | ExpressGraphQLOptionsFunction'我已经检查了dts文件:
import * as express from 'express';
import { GraphQLOptions } from 'graphql-server-core';
import * as GraphiQL from 'graphql-server-module-graphiql';
export interface ExpressGraphQLOptionsFunction {
(req?: express.Request, res?: express.Response): GraphQLOptions | Promise<GraphQLOptions>;
}
export interface ExpressHandler {
(req: express.Request, res: express.Response, next: any): void;
}
export declare function graphqlExpress(options: GraphQLOptions | ExpressGraphQLOptionsFunction): ExpressHandler;
export declare function graphiqlExpress(options: GraphiQL.GraphiQLData): (req: express.Request, res: express.Response) => void;但我还没能弄清楚哪里出了问题。有什么想法吗?谢谢!
发布于 2017-04-14 03:09:45
这不是一个解决方案..。但是,如果您尝试使用GraphQL js标准实现及其类型来定义模式...它起作用了吗?
发布于 2017-04-25 01:20:45
看起来您需要使用类型断言来让TypeScript编译器知道executableSchema是GraphQLSchema类型的。尝试以下操作:
试试这个:
import { executableSchema } from "./schema";
import { graphqlExpress } from "graphql-server-express";
import { GraphQLSchema } from "graphql-server-core/node_modules/@types/graphql";
// ...
app.use("/graphql", graphqlExpress((req) => { // Error!
return {
schema: executableSchema as GraphQLSchema,
context: {
container: container
}
};
}));发布于 2017-09-21 07:02:35
另一种解决方案是导入GraphQLServerOptions定义并对对象进行类型转换。类似于James提出的解决方案,但是使用这种方法,您可以使用ES6对象简写语法。
import { GraphQLServerOptions } from 'apollo-server-core/src/graphqlOptions';
import { graphqlExpress } from 'apollo-server-express';
import { executableSchema as schema } from "./schema";
// ...
app.use('/graphql', graphqlExpress({
schema,
context: { container },
} as GraphQLServerOptions));https://stackoverflow.com/questions/43396102
复制相似问题