Skip to content

Commit 2af15a3

Browse files
committed
Initial commit
0 parents  commit 2af15a3

15 files changed

+5989
-0
lines changed

.coveralls.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
service_name: travis-pro

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.eslintrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extends: standard

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
.nyc_output
3+
coverage
4+
node_modules

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
node_js:
3+
- node
4+
- lts/*
5+
- '8'
6+
before_install:
7+
- npm i -g npm
8+
after_success:
9+
- nyc report --reporter=text-lcov | npx coveralls
10+
- npx semantic-release
11+
branches:
12+
except:
13+
- /^v\d+\.\d+\.\d+$/
14+
notifications:
15+
email: false

.vscode/launch.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "test",
11+
"program": "${workspaceFolder}/node_modules/ava/profile.js",
12+
"args": [
13+
"${file}",
14+
"--serial"
15+
],
16+
"internalConsoleOptions": "openOnSessionStart",
17+
"skipFiles": [
18+
"node_modules/**/*.js",
19+
"<node_internals>/**/*.js"
20+
]
21+
}
22+
]
23+
}

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"files.exclude": {
3+
"**/.git": true,
4+
"**/.nyc_output": true,
5+
"**/.vscode": true,
6+
"**/node_modules": true
7+
}
8+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Ferdinand Prantl
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# graphql-resolvable-directive
2+
3+
[![NPM version](https://badge.fury.io/js/graphql-resolvable-directive.png)](http://badge.fury.io/js/graphql-resolvable-directive)
4+
[![Build Status](https://travis-ci.org/prantlf/graphql-resolvable-directive.png)](https://travis-ci.org/prantlf/graphql-resolvable-directive)
5+
[![Coverage Status](https://coveralls.io/repos/github/prantlf/graphql-resolvable-directive/badge.svg?branch=master)](https://coveralls.io/github/prantlf/graphql-resolvable-directive?branch=master)
6+
[![devDependency Status](https://david-dm.org/prantlf/graphql-resolvable-directive/dev-status.svg)](https://david-dm.org/prantlf/graphql-resolvable-directive#info=devDependencies)
7+
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
8+
9+
[![NPM Downloads](https://nodei.co/npm/graphql-resolvable-directive.png?downloads=true&stars=true)](https://www.npmjs.com/package/graphql-resolvable-directive)
10+
11+
Supports GraphQL custom directives that hook into the field execution. It can be used for validation or transformation of the resulting field values.
12+
13+
## Synopsis
14+
15+
```js
16+
const {
17+
GraphQLResolvableDirective, supportResolvableDirectives
18+
} = require('graphql-resolvable-directive')
19+
20+
// Performs the logical negation on the field value.
21+
const notDirective = new GraphQLResolvableDirective({
22+
name: 'not',
23+
description: 'Negates the field execution result.',
24+
locations: [DirectiveLocation.FIELD],
25+
async resolve (resolve, source, args, context, info) {
26+
const result = await resolve()
27+
return !result
28+
}
29+
})
30+
31+
// Exposes a single field "falsy" returning the `null` value.
32+
// Recognizes the `not` directive.
33+
const schema = new GraphQLSchema({
34+
directives: [notDirective],
35+
query: new GraphQLObjectType({
36+
name: 'Query',
37+
fields: () => ({
38+
falsy: { type: GraphQLBoolean }
39+
})
40+
})
41+
})
42+
visitFields(schema, field => supportCustomDirectives(field, schema))
43+
44+
// Returns `true` instead of the default `null`.
45+
const { data } = await graphql(schema, '{ falsy @not }')
46+
assert(data.falsy)
47+
```
48+
49+
## Installation
50+
51+
This module can be installed in your project using [NPM] or [Yarn]. Make sure, that you use [Node.js] version 8 or newer.
52+
53+
```sh
54+
$ npm i graphql-resolvable-directive -S
55+
```
56+
57+
```sh
58+
$ yarn add graphql-resolvable-directive
59+
```
60+
61+
## Description
62+
63+
### GraphQLResolvableDirective
64+
65+
Base class for custom directives with field execution hooks. If they include the `resolve` method, it will be called instead of the original field resolver. It would usually call the original resolver to inspect or modify its result.
66+
67+
```js
68+
const { GraphQLResolvableDirective } = require('graphql-resolvable-directive')
69+
70+
const isTruthyDirective = new GraphQLResolvableDirective({
71+
name: 'isTruthy',
72+
description: 'Checks if the field execution result is truthy.',
73+
locations: [DirectiveLocation.FIELD],
74+
async resolve (resolve, source, args, context, info) {
75+
const result = await resolve()
76+
if (!result) {
77+
throw new Error(`The field "${info.fieldName}" was not truthy.`)
78+
}
79+
return result
80+
}
81+
})
82+
83+
const toLowerCaseDirective = new GraphQLResolvableDirective({
84+
name: 'toLowerCase',
85+
description: 'Converts a string value to lower-case.',
86+
locations: [DirectiveLocation.FIELD],
87+
async resolve (resolve, source, args, context, info) {
88+
const result = await resolve()
89+
return result.toLowerCase()
90+
}
91+
})
92+
```
93+
94+
If the directives are chained, the results are passed from the first directive to the next one and so on. The last returned value is assigned to the field.
95+
96+
```js
97+
graphql(schema, '{ name @isTruthy @toLowerCase }')
98+
```
99+
100+
### supportCustomDirectives(field, schema)
101+
102+
Enables hooking into the field execution by a custom directive for the specified field. Directives have to be provided in the schema configuration.
103+
104+
* `field` has to be a field configuration object
105+
* `schema` has to be an object instance of the type `GraphQLSchema`
106+
107+
Field configurations are usually obtained from a schema by a field visitor like [graphql-field-visitor], for example.
108+
109+
```js
110+
const { supportResolvableDirectives } = require('graphql-resolvable-directive')
111+
const { visitFields } = require('graphql-field-visitor')
112+
113+
const schema = new GraphQLSchema({
114+
directives: [isTruthyDirective, toLowerCaseDirective],
115+
query: ...
116+
})
117+
118+
visitFields(schema, field => supportCustomDirectives(field, schema))
119+
```
120+
121+
## Contributing
122+
123+
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
124+
125+
## Release History
126+
127+
* 2019-08-18 v0.0.1 Initial release
128+
129+
## License
130+
131+
Copyright (c) 2019 Ferdinand Prantl
132+
133+
Licensed under the MIT license.
134+
135+
[Node.js]: http://nodejs.org/
136+
[NPM]: https://www.npmjs.com/
137+
[Yarn]: https://yarnpkg.com/
138+
[graphql-field-visitor]: https://github.com/prantlf/graphql-field-visitor

lib/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const GraphQLResolvableDirective = require('./resolvable-directive')
2+
const supportResolvableDirectives = require('./support-resolvable-directives')
3+
4+
module.exports = { GraphQLResolvableDirective, supportResolvableDirectives }

0 commit comments

Comments
 (0)