【问题标题】:Apollo Server Federation with graphql-shieldApollo 服务器联盟与 graphql-shield
【发布时间】:2021-12-01 21:05:45
【问题描述】:

我正在使用graphql-shield 来保护subgraph。

const isAuthenticated = rule({ cache: 'contextual' })(async (parent, args, ctx, info) => {
  return ctx.isAuthenticated
})

const permissions = shield({
  Query: {
    '*': and(isAuthenticated)
  },
  Mutation: {
    '*': and(isAuthenticated)
  }
})

const server = new ApolloServer({
    schema: applyMiddleware(buildSubgraphSchema([{ typeDefs, resolvers }]), permissions),
    introspection: global.configuration.ENVIRONMENT === 'development'
})

在我的构建过程中,我使用rover CLI 来更新 Apollo Studio 中的supergraph schema:

rover subgraph introspect http://localhost/graphql | rover subgraph publish super-graph@development --routing-url http://localhost/graphql --schema - --name persons

rover 更新失败,因为权限盾引发了 Not Authorised! 错误。

如何使用graphql-shield 保护subgraph 并允许SubgraphIntrospectQuery 操作?

我了解可以向流动站自省命令添加不记名令牌:

rover subgraph introspect http://localhost/graphql --header "Authorization: Bearer token"

但是,我无法在构建过程中生成访问令牌。

【问题讨论】:

    标签: node.js graphql graphql-federation graphql-shield


    【解决方案1】:

    我能够通过更改身份验证范围来解决此问题。

    而不是验证所有“*”

    const permissions = shield({
      Query: {
        '*': and(isAuthenticated)
      },
      Mutation: {
        '*': and(isAuthenticated)
      }
    })
    

    我改为对单个操作进行身份验证:

    const permissions = shield({
      Query: {
        'user': and(isAuthenticated),
        'users': and(isAuthenticated)
        ....
      },
      Mutation: {
        'createUser': and(isAuthenticated),
        'updateUser': and(isAuthenticated)
        ....
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 2019-12-09
      • 2021-10-04
      • 2017-11-07
      • 2012-11-03
      • 2020-03-27
      • 2021-03-29
      • 2019-02-03
      • 2018-06-23
      相关资源
      最近更新 更多