首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apollo Server Federation with graphql-shield

Apollo Server Federation with graphql-shield
EN

Stack Overflow用户
提问于 2021-10-13 11:20:09
回答 1查看 195关注 0票数 1

我在用graphql-shield保护一个subgraph

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

代码语言:javascript
复制
rover subgraph introspect http://localhost/graphql | rover subgraph publish super-graph@development --routing-url http://localhost/graphql --schema - --name persons

由于权限屏蔽抛出Not Authorised!错误,rover更新失败。

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

我知道可以在rover introspect命令中添加一个持有者令牌:

代码语言:javascript
复制
rover subgraph introspect http://localhost/graphql --header "Authorization: Bearer token"

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-16 09:40:56

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

而不是验证所有的"*“

代码语言:javascript
复制
const permissions = shield({
  Query: {
    '*': and(isAuthenticated)
  },
  Mutation: {
    '*': and(isAuthenticated)
  }
})

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

代码语言:javascript
复制
const permissions = shield({
  Query: {
    'user': and(isAuthenticated),
    'users': and(isAuthenticated)
    ....
  },
  Mutation: {
    'createUser': and(isAuthenticated),
    'updateUser': and(isAuthenticated)
    ....
  }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69554576

复制
相关文章

相似问题

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