试后恋示例,但不知道哪里弄错了?
我正在尝试将graphql-屏蔽作为插件来实现。
middlewarePlugin = makeProcessSchemaPlugin((schema: typeof GraphQLSchema) => {
return applyMiddleware(schema, permissions);
});permission.ts
const { rule, shield } = require("graphql-shield");
const isAuthenticated = rule()((parent: any, args: any, user: any ) => {
return user !== null;
});
const permissions = shield({
Query: {
viewer: isAuthenticated
}
});
export = permissions;我将middlewarePlugin作为其他插件导入到postgraphile中:
appendPlugins: [
myClass.myPlugin,
myClass.jsonPlace,
myClass.middlewarePlugin
],撞车日志:
生成初始架构时发生严重错误。退出是因为没有设置
retryOnInitFail。错误详细信息: graphql / graphql \x TypeError:无法读取未定义的石墨at的属性“片段”,即在(/home/node/app/node_modules/graphql-middleware/src/utils.ts:25:17)的isMiddlewareWithFragment上,在Object.validateMiddleware (/home/node/app/node_modules/graphql-middleware/src/validation.ts:9:上,Object.isMiddlewareFunction的Object.isMiddlewareFunction图形at7) (/home/node/app/node_modules/graphql-middleware/src/middleware.ts:33:27)的graphql \x{e 010} normalisedMiddlewares.reduceRight.schema.schema (/home/node/app/node_modules/graphql-middleware/src/middleware.ts:91:11)的Array.reduceRight ()的Array.reduceRight()的graphql /Array.reduceRight()的graphql /Array.reduceRight()@ at (/home/node/app/node_modules/graphql-middleware/src/middleware.ts:132:10) (/home/node/app/resolvers/test.resolver.ts:254:16) graphql _ at hook (/home/node/app/resolvers/test.resolver.ts:254:16) graphql = SchemaBuilder.applyHooks (/home/node/app/node_modules/graphile-build/src/SchemaBuilder.js:398:20)
发布于 2021-06-09 13:11:22
postgraphile所期望的中间件类型与graphql-中间件略有不同。我没有下载graphql-middleware和graphql-shield并试图让他们使用postgraphile,而是用makeWrapResolversPlugin编写了自己的“盾牌”。示例:
const filter = (ctx) => {
// Only attempting to do auth for non-root things, because
// with postgraphile, all the data gets fetched at the root
if (ctx.scope.isRootMutation || ctx.scope.isRootQuery) {
// return this to make it available to the wrapper function
return ctx.scope.fieldName
}
// return null to not wrap this non-root resolver
return null
}
const wrapper = (fieldName) =>
async (resolve, source, args, context) => {
// Unless we're doing createUser, do an auth check
//
if (fieldName === "createUser") return resolve()
const isAuthenticated = await getIsAuthenticated(context)
//
if (isAuthenticated) return resolve()
throw new Error("Unauthorized");
}
const Permissions = makeWrapResolversPlugin(filter, wrapper)https://stackoverflow.com/questions/65957023
复制相似问题