# graphql-acl

> GraphQL ACL - Allowed fields per role

Latest version **1.0.3** (published 2017-05-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphql-acl
pnpm add graphql-acl
yarn add graphql-acl
bun add graphql-acl
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2017-05-27 |
| First published | 2017-05-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Jakub Chadim |
| Maintainers | jakubchadim |
| Keywords | graphgq, acl, permissions, roles |

## Links

- npm: https://www.npmjs.com/package/graphql-acl
- Repository: https://github.com/jakubchadim/graphql-acl
- Homepage: https://github.com/jakubchadim/graphql-acl#readme
- Issues: https://github.com/jakubchadim/graphql-acl/issues
- npm.io page: https://npm.io/package/graphql-acl

## Dependencies (1)

- [graphql](https://npm.io/package/graphql.md) ^0.9.6

## Alternatives

- [d3-force-3d](https://npm.io/package/d3-force-3d.md) — 1.0M weekly downloads
- [ng2-charts](https://npm.io/package/ng2-charts.md) — 486.8K weekly downloads
- [@arcgis/core](https://npm.io/package/@arcgis/core.md) — 257.8K weekly downloads
- [react-sparklines](https://npm.io/package/react-sparklines.md) — 249.3K weekly downloads
- [react-native-gifted-charts](https://npm.io/package/react-native-gifted-charts.md) — 182.3K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2017-05-27
- 1.0.2 — 2017-05-27
- 1.0.1 — 2017-05-27
- 1.0.0 — 2017-05-27

## README

# GraphQL ACL

Specify allowed fields for roles. Create big scheme with all fields and allow only some fields to roles.

## Installation:

Using npm:
```bash
$ npm i --save graphql-acl
```

## Example:

#### ACL:
Configure object representing allowed fields.
```javascript
const acl = {
  user: true,
  articles: {
    title: true,
    author: {
      name: true
    }
  }
}
```
#### Scheme:
```graphql
{
  user: {
    name: String,
    surname: String,
    email: String
  },
  articles: {
    id: Number,
    title: String,
    perex: String,
    content: String,
    views: Number,
    author: {
      name: String,
      surname: String
    }
  }
}
```
#### Result:
Generated schema contains only allowed fields.
```graphql
{
  user: {
    name: String,
    surname: String,
    email: String
  },
  articles: {
    title: String,
    author: {
      name: String
    }
  }
}
```

## Implementation:

##### createGraphQLObjectType(props, fields) => function (acl)
Return function which expect acl as param and generate GraphQLObjectType.

`props` - {Object} - GraphQLObjectType properties

`fields` - {Object} - expect GQL object or function which returns GQL object

##### index.js
```javascript
const { GraphQLSchema } = require('graphql')

const {ROLES, ACL} = require('./acl')
const createRoot = require('./root')

const userSchema = new GraphQLSchema({
  description: 'User graphQL',
  query: createRoot(ACL[ROLES.USER])
})

const adminSchema = new GraphQLSchema({
  description: 'Admin graphQL',
  query: createRoot(ACL[ROLES.ADMIN])
})
```


##### root.js
```javascript
const { GraphQLString } = require('graphql')
const { createGraphQLObjectType } = require('graphql-acl')

const createUser = require('./user')

const user = acl => ({
  description: 'User object',
  type: createUser(acl)
})

const version = ({
  description: 'Version',
  type: GraphQLString
})

const createRoot = createGraphQLObjectType({
  name: 'Query'
}, {
  user,
  version
})

module.exports = createRoot
```

##### user.js
```javascript
const { GraphQLString } = require('graphql')
const { createGraphQLObjectType } = require('graphql-acl')

const name = ({
  description: 'Name',
  type: GraphQLString
})

const surname = ({
  description: 'Surname',
  type: GraphQLString
})

const createUser = createGraphQLObjectType({
  name: 'User'
}, {
  name,
  surname
})

module.exports = createUser
```

##### acl.js
```javascript
const ROLES = {
  USER: 'USER',
  ADMIN: 'ADMIN'
}

const userAcl = {
  user: {
    name: true
  }
}

const ACL = {
  [ROLES.USER]: userAcl,
  [ROLES.ADMIN]: true // Allow everything
}

module.exports = {
  ROLES,
  ACL
}
```

---
_Source: https://npm.io/package/graphql-acl · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
