我遵循Ben Awad的教程,在这个项目中,我使用Typescript, micro - orm和GraphQL,对所有这些东西都很陌生。
我在GraphQL上得到以下错误跟踪:
{
"errors": [
{
"message": "Using global EntityManager instance methods for context specific actions is disallowed. If you need to work with the global instance's identity map, use `allowGlobalContext` configuration option or `fork()` instead.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"posts"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"name": "ValidationError",
"stacktrace": [
"ValidationError: Using global EntityManager instance methods for context specific actions is disallowed. If you need to work with the global instance's identity map, use `allowGlobalContext` configuration option or `fork()` instead.",
" at Function.cannotUseGlobalContext (C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\node_modules\@mikro-orm\core\errors.js:83:16)",
" at SqlEntityManager.getContext (C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\node_modules\@mikro-orm\core\EntityManager.js:1030:44)",
" at SqlEntityManager.find (C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\node_modules\@mikro-orm\core\EntityManager.js:93:25)",
" at PostResolver.posts (C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\dist\resolvers\post.js:20:19)",
" at C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\node_modules\type-graphql\dist\resolvers\create.js:34:68",
" at Object.applyMiddlewares (C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\node_modules\type-graphql\dist\resolvers\helpers.js:58:16)",
" at C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\node_modules\type-graphql\dist\resolvers\create.js:27:26",
" at field.resolve (C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\node_modules\apollo-server-core\dist\utils\schemaInstrumentation.js:56:26)",
" at resolveFieldValueOrError (C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\node_modules\graphql\execution\execute.js:502:18)",
" at resolveField (C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\kBlog\node_modules\graphql\execution\execute.js:460:16)"
]
}
}
}
],
"data": null
}
当我查询这个时:
{
posts {
id
createdAt
updatedAt
title
}
}
这是我的索引。ts文件:
const main = async () => {
const orm = await MikroORM.init(microConfig);
await orm.getMigrator().up();
const app = express();
await RequestContext.createAsync(orm.em, async () => {
})
const apolloServer = new ApolloServer({
schema: await buildSchema({
resolvers: [HelloResolver, PostResolver],
validate: false
}),
context: () => ({em: orm.em})
});
await apolloServer.start();
apolloServer.applyMiddleware({app});
app.listen(5000, () => {
console.log("server started on port 5000");
});
}
这是我的帖子。解析器文件:
@Resolver()
export class PostResolver{
@Query(()=> [Post])
posts(@Ctx() {em}: MyContext): Promise<Post[]>{
return em.find(Post, {});
}
}
最后是实体Post。ts文件(给出关于上面的GraphQL查询的上下文):
@ObjectType()
@Entity()
export class Post{
@Field(() => Int)
@PrimaryKey()
id! : number;
@Field(() => String)
@Property({type: 'date'})
createdAt = new Date();
@Field(()=> String)
@Property({type: 'date',onUpdate: () => new DataTransfer()})
updatedAt = new Date();
@Field()
@Property({type: 'text'})
title! : string;
}
我试着遵循这个问题的答案,这是一个非常相似的问题。
我的代码与该线程的OP有点不同,所以我不确定如何在我的代码中实现RequestContext。我想尽量避免简单地禁用验证,因为我将有服务器和中间件。
我希望有人能指给我正确的方向。
打开你的micro -orm.config。SRC中的Ts文件在微表单的配置变量中添加allowGlobalContext: true
。配置文件
import { Post } from "../entities/Post";
import { __prod__ } from "./constants";
import { Options } from "@mikro-orm/core";
import path from "path";
const config: Options = {
allowGlobalContext: true,
migrations:{
path: path.join(__dirname,'./migrations'),
pathTs: undefined,
glob: '!(*.d).{js,ts}'
},
entities: [Post],
dbName: 'lireddit',
user: 'postgres',
password: 'root',
type: 'postgresql',
debug: !__prod__,
};
export default config;
这个错误说你必须allowGlobalContext
,在本教程的时候,图形没有沙盒更新,它现在有了。所以我们必须设置allowGlobalContext
为真。