我第一次尝试使用ExpressQL。我已经声明了一个架构:
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;
但我无法弄清楚出了什么问题。有任何想法吗?谢谢!
答案 0 :(得分:0)
这不是解决方案的答案......但是如果你尝试使用GraphQL js标准实现及其类型来定义模式......它是否有效?
答案 1 :(得分:0)
It looks like you need to use a Type Asserting to let the TypeScript compiler know that executableSchema
is of type GraphQLSchema
. Try the following:
Try this:
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
}
};
}));
答案 2 :(得分:0)
另一个解决方案是导入to_date()
定义并对您的对象进行类型转换。与James提出的解决方案类似,但是使用这种方法,您可以使用ES6对象的简写语法。
GraphQLServerOptions