相关疑难解决方法(0)

如何使用GraphQL构建经过身份验证的查询?

我正在考虑编写一个执行以下操作的API:

  • 注册和登录用户,为用户提供身份验证令牌
  • 创建映射(数据例如:{ name: “Quotes”, attributes: [“quote”, “author"] })
  • 创建地图项目(数据例如:{ quote: "...", author: "..." })

我会像这样构建查询:

// return the name and id of all the user's maps
maps(authToken="…") {
  name,
  id
}

// return all the items of a single map
maps(authToken="…") {
  map(name=“Quotes") {
    items
  }
}

// OR by using the map_id
maps(authToken="…") {
  map(id=“…") {
    items
  }
}
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是,这是正确的还是我需要以不同的方式构建它?

authentication api graphql

13
推荐指数
1
解决办法
3748
查看次数

如何在GraphQL查询中检查权限和其他条件?

如何检查用户是否有权查看查询内容?我不知道该怎么做.

  • args?怎么会这样呢?
  • resolve()?看看用户是否有权限并以某种方式消除/更改某些args?

例:

如果用户是"访客",他只能看到公开帖子,"管理员"可以看到一切.

const userRole = 'admin';  // Let's say this could be "admin" or "visitor"

const Query = new GraphQLObjectType({
    name: 'Query',
    fields: () => {
        return {
            posts: {
                type: new GraphQLList(Post),
                args: {
                    id: {
                        type: GraphQLString
                    },
                    title: {
                        type: GraphQLString
                    },
                    content: {
                        type: GraphQLString
                    },
                    status: {
                        type: GraphQLInt  // 0 means "private", 1 means "public"
                    },
                },

                // MongoDB / Mongoose magic happens …
Run Code Online (Sandbox Code Playgroud)

mongoose node.js graphql

11
推荐指数
2
解决办法
4117
查看次数

标签 统计

graphql ×2

api ×1

authentication ×1

mongoose ×1

node.js ×1