{"id":20218265,"url":"https://github.com/cannercms/graphql-rbac","last_synced_at":"2025-04-10T15:46:52.490Z","repository":{"id":144737504,"uuid":"158179819","full_name":"CannerCMS/graphql-rbac","owner":"CannerCMS","description":"GraphQL Role-based access control (RBAC) middleware","archived":false,"fork":false,"pushed_at":"2018-11-27T02:14:45.000Z","size":44,"stargazers_count":37,"open_issues_count":1,"forks_count":4,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-02-24T14:38:06.621Z","etag":null,"topics":["graphql","graphql-rbac","graphql-role","graphql-shield","middleware","rbac","schema"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CannerCMS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-11-19T07:30:49.000Z","updated_at":"2023-08-18T07:49:43.000Z","dependencies_parsed_at":"2023-05-04T22:27:33.915Z","dependency_job_id":null,"html_url":"https://github.com/CannerCMS/graphql-rbac","commit_stats":null,"previous_names":["cannercms/graphql-rbac"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CannerCMS%2Fgraphql-rbac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CannerCMS%2Fgraphql-rbac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CannerCMS%2Fgraphql-rbac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CannerCMS%2Fgraphql-rbac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CannerCMS","download_url":"https://codeload.github.com/CannerCMS/graphql-rbac/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224580204,"owners_count":17334730,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","graphql-rbac","graphql-role","graphql-shield","middleware","rbac","schema"],"created_at":"2024-11-14T06:37:48.644Z","updated_at":"2024-11-14T06:37:49.590Z","avatar_url":"https://github.com/CannerCMS.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GraphQL Role-based access control (RBAC) middleware\n\n[![CircleCI](https://circleci.com/gh/Canner/graphql-rbac/tree/master.svg?style=shield)](https://circleci.com/gh/Canner/graphql-rbac/tree/master)\n[![npm version](https://badge.fury.io/js/graphql-rbac.svg)](https://badge.fury.io/js/graphql-rbac)\n\n\ngraphql-rbac provides you a simple way to use Role-based access control in GraphQL. This package integrates with [graphql-shield](https://github.com/maticzav/graphql-shield) which helps you create a permission layer for your application. Using a schema with array of role, graphql-rbac can help you generate rule functions in graphql-shield. So you can easily use RBAC in your application by providing a schema.\n\n## Why graphql-rbac?\n\n* Easy to specify rule permissions for each field in GraphQL.\n* Don't need to write rule function by yourself.\n\n## Installation\n\n```bash\nyarn add graphql-rbac\n```\n\n## How to use\n\n```js\nimport { RBAC } from 'graphql-rbac'\n\nconst roles = ['ADMIN', 'DEVELOPER']\n\nconst schema = {\n  Query: {\n    users: ['ADMIN', 'DEVELOPER']\n  },\n  Mutation: {\n    createUser: ['ADMIN', 'DEVELOPER'],\n    updateUser: ['ADMIN', 'DEVELOPER'],\n    deleteUser: ['ADMIN']\n  },\n  User: {\n    password: ['ADMIN']\n  }\n}\n\nconst typeDefs = `\n  type Query {\n    users: [User!]!\n  }\n\n  type Mutation {\n    createUser: User!\n    updateUser: User!\n    deleteUser: User\n  }\n\n  type User {\n    username: String!\n    password: String!\n  }\n`\n\nconst resolvers = {\n  Query: {\n    users: () =\u003e [\n      { username: 'Tom', password: '****' },\n      { username: 'John', password: '****' },\n    ]\n  },\n  Mutation: {\n    createUser: () =\u003e { username: 'Tom', password: '****' },\n    updateUser: () =\u003e { username: 'John', password: '****' },\n    deleteUser: () =\u003e null\n  }\n}\n\nconst users = {\n  admin: { role: 'ADMIN' },\n  developer: { role: 'DEVELOPER' }\n}\n\nconst getUser = async (req) =\u003e {\n  const auth = req.request.headers.authorization\n  let user = {}\n  if (users[auth]) {\n    user = users[auth]\n  }\n\n  return user\n}\n\nconst rbac = new RBAC({roles, schema, getUser})\n\nconst server = new GraphQLServer({\n  typeDefs,\n  resolvers,\n  middlewares: [rbac.middleware()],\n  context: req =\u003e ({\n    user: rbac.context(req)\n  }),\n})\n```\n\n## Run test\n\n```\nnpm run test\n```\n\n## License\n\nApache-2.0\n\n![footer banner](https://user-images.githubusercontent.com/26116324/37811196-a437d930-2e93-11e8-97d8-0653ace2a46d.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcannercms%2Fgraphql-rbac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcannercms%2Fgraphql-rbac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcannercms%2Fgraphql-rbac/lists"}