# graphql-anonym-directives

> Provides some graphql directives to add role based anonymization to graphql schemata.

Latest version **1.0.2** (published 2021-07-27) · ISC license · 0 weekly downloads

## Install

```sh
npm install graphql-anonym-directives
pnpm add graphql-anonym-directives
yarn add graphql-anonym-directives
bun add graphql-anonym-directives
```

## 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.2 |
| Published | 2021-07-27 |
| First published | 2021-07-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | phein |
| Keywords | graphql, anonymization, directives, schema |

## Links

- npm: https://www.npmjs.com/package/graphql-anonym-directives
- Repository: git@git.tu-berlin.de:my-awesome-group/graphql-access-control
- Homepage: https://git.tu-berlin.de/my-awesome-group/graphql-access-control
- npm.io page: https://npm.io/package/graphql-anonym-directives

## Dependencies (4)

- [graphql](https://npm.io/package/graphql.md) ^15.5.1
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [graphql-tools](https://npm.io/package/graphql-tools.md) ^7.0.5
- [value-anonymizer](https://npm.io/package/value-anonymizer.md) ^1.0.0

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2021-07-27
- 1.0.1 — 2021-07-27
- 1.0.0 — 2021-07-26

## README

# graphql-anonym-directives
This package provides special ```SchemaDirectiveVisitor``` classes. Those can be added to schemas: https://www.apollographql.com/docs/apollo-server/schema/directives/ 

## Usage
### Installation
```sh
npm install graphql-anonym-directives
```
### Requirements
The anonymization directives use JWT tokens to extract to role of the current requester. Therefore, you must provide the JWT secret as environment variable.
``` 
JWT_SECRET=qwertyuiopasdfghjklzxcvbnm123456
```
It can be loaded with the ```dotenv``` package in you ```index.js```: 
```js
const dotenv = require("dotenv");
dotenv.config();
```
Also the context, that is passed as argument for each resolver call needs to store the token passed by the request. This can be done when creating the Apollo server (the argument ```req``` consists of the request information):
```js
const server = new ApolloServer({
    typeDefs,
    resolvers,
    context: ({req}) => {
        return {req}
    } 
})
```

To use the anonymization directives, you need to import them and declare the ```getAnonymizationParameter``` function. This function must return the paramters, which then are passed by the directive to the anonymizer function (to learn more, look here for the anonymizer: ). As arguments, it gets the ```role``` and all 4 arguments (```result, args, context, info```), that are also passed to the resolvers.

Don't forget to add the directive as argument when creating the apollo server.

A whole implementation could look like this:

```js

import {NoiseDirective} from "graphql-access-control";
NoiseDirective.prototype.getAnonymizationParameter = function(role, result, args, context, info){
    const m = new Map();
    m.set(("Advertiser, Symptom.pain"), {
        typeOfDistribution: "normal", 
        distributionParameters:{
            mean: 0,
            standardDeviation: 2,
        }, 
        valueParameters:{
            isInt: true,
        }
    });    

    var lookup = role + ", " + info.parentType.name + "."+ info.fieldName
    return m.get(lookup);
};

const server = new ApolloServer({
    typeDefs,
    resolvers,
    context: ({req}) => {
        return {req, models}
    },
    schemaDirectives: { 
        noise: NoiseDirective,
    }, 
});
```

The schema then could implement the noise directive: 

```
type Symptom{
    id: ID!
    pain: Float @noise
}
```

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