首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图形in在邮政文件中作为“makeProcessSchemaPlugin”的屏蔽

图形in在邮政文件中作为“makeProcessSchemaPlugin”的屏蔽
EN

Stack Overflow用户
提问于 2021-01-29 15:11:14
回答 1查看 457关注 0票数 1

试后恋示例,但不知道哪里弄错了?

我正在尝试将graphql-屏蔽作为插件来实现。

代码语言:javascript
复制
    middlewarePlugin = makeProcessSchemaPlugin((schema: typeof GraphQLSchema) => {
        return applyMiddleware(schema, permissions);
    });

permission.ts

代码语言:javascript
复制
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中:

代码语言:javascript
复制
  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)

EN

回答 1

Stack Overflow用户

发布于 2021-06-09 13:11:22

postgraphile所期望的中间件类型与graphql-中间件略有不同。我没有下载graphql-middlewaregraphql-shield并试图让他们使用postgraphile,而是用makeWrapResolversPlugin编写了自己的“盾牌”。示例:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65957023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档