Skip to content

use ES6 export, export all directives as default export, add default export test #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 57 additions & 99 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,7 @@ npm install --save graphql-custom-directives

### Usage
```javascript
import {
GraphQLDateDirective,
GraphQLNumberDirective,
GraphQLCurrencyDirective,
GraphQLLowerCaseDirective,
GraphQLUpperCaseDirective,
GraphQLCamelCaseDirective,
GraphQLStartCaseDirective,
GraphQLCapitalizeDirective,
GraphQLKebabCaseDirective,
GraphQLTrimDirective,
GraphQLDefaultToDirective,
GraphQLToLowerDirective,
GraphQLToUpperDirective,
GraphQLTemplateDirective,
GraphQLPhoneDirective,
applySchemaCustomDirectives,
} from 'graphql-custom-directives';
import GraphQLCustomDirectives, { applySchemaCustomDirectives } from 'graphql-custom-directives';

const query = new GraphQLObjectType({
name: 'Query',
Expand All @@ -53,23 +36,7 @@ const query = new GraphQLObjectType({
});

const schema = new GraphQLSchema({
directives: [
GraphQLDateDirective,
GraphQLNumberDirective,
GraphQLCurrencyDirective,
GraphQLLowerCaseDirective,
GraphQLUpperCaseDirective,
GraphQLCamelCaseDirective,
GraphQLStartCaseDirective,
GraphQLCapitalizeDirective,
GraphQLKebabCaseDirective,
GraphQLTrimDirective,
GraphQLDefaultToDirective,
GraphQLToLowerDirective,
GraphQLToUpperDirective,
GraphQLPhoneDirective,
GraphQLTemplateDirective
],
directives: GraphQLCustomDirectives,
query
});

Expand All @@ -81,16 +48,32 @@ graphql(schema, `{ input(value: "test") @upperCase }`)
});
```

### Using with apollo

is also possible
### Usage with apollo-server / graphql-tools

```javascript
import { makeExecutableSchema } from 'graphql-tools';
import GraphQLCustomDirectives, { applySchemaCustomDirectives } from 'graphql-custom-directives';

let schema = makeExecutableSchema(...);

schema._directives.push(...GraphQLCustomDirectives);
applySchemaCustomDirectives(schema);
```

### Available Directives

```javascript
// Import Array of All Directives
import GraphQLCustomDirectives from 'graphql-custom-directives';

// Import Individual Directives
import {
// Date Formatting
GraphQLDateDirective,
// Number Formatting
GraphQLNumberDirective,
GraphQLCurrencyDirective,
// String Formatting
GraphQLLowerCaseDirective,
GraphQLUpperCaseDirective,
GraphQLCamelCaseDirective,
Expand All @@ -103,84 +86,59 @@ import {
GraphQLToUpperDirective,
GraphQLTemplateDirective,
GraphQLPhoneDirective
applySchemaCustomDirectives
} from 'graphql-custom-directives';

let directives = [
GraphQLDateDirective,
GraphQLNumberDirective,
GraphQLCurrencyDirective,
GraphQLLowerCaseDirective,
GraphQLUpperCaseDirective,
GraphQLCamelCaseDirective,
GraphQLStartCaseDirective,
GraphQLCapitalizeDirective,
GraphQLKebabCaseDirective,
GraphQLTrimDirective,
GraphQLDefaultToDirective,
GraphQLToLowerDirective,
GraphQLToUpperDirective,
GraphQLTemplateDirective,
GraphQLPhoneDirective
]

let schema = makeExecutableSchema(...);

schema._directives.push.apply(schema._directives, directives);
applySchemaCustomDirectives(schema);

```

### Date formatting directives

Adding date directive to graphql query for formatting the result using [Moment](https://github.com/moment/moment).

- Using default date format:
```javascript
```graphql
query {
input(value: "2016-01-01T00:00:00") @date
}
// => { input: "01 Jan 2016 00:00" }
# => { input: "01 Jan 2016 00:00" }
```
- Using specific moment format:
```javascript
```graphql
query {
input(value: "2016-01-01T00:00:00") @date(as:"YYYY")
}
// => { input: "2016" }
# => { input: "2016" }
```
- Using days ago format
```javascript
```graphql
query {
input(value: "${(new Date).toISOString()}") @date(as:"days ago")
}
// => { input: "0 days ago" }
# => { input: "0 days ago" }
```

### Number formatting directives

Adding number directive to graphql query for formatting the result using [Numeral-js](https://github.com/adamwdraper/Numeral-js).

- Using default format:
```javascript
```graphql
query {
input(value: "1500.404") @number
}
// => { input: "1,500" }
# => { input: "1,500" }
```
- Using specific numeral format:
```javascript
```graphql
query {
input(value: "-1500.404") @number(as:"(0,0.00)")
}
// => { input: "(1,500.40)" }
# => { input: "(1,500.40)" }
```
- Using default currency format:
```javascript
```graphql
query {
input(value: "1500") @currency
}
// => { input: "$1,500)" }
# => { input: "$1,500)" }
```

### String formatting directives
Expand All @@ -189,110 +147,110 @@ Adding string directive to graphql query for formatting the result using [Lodash

- Using lowerCase directive:

```javascript
```graphql
query {
input(value: "FOO BAR") @lowerCase
}
// => { input: "foo bar" }
# => { input: "foo bar" }
```

- Using upperCase directive:

```javascript
```graphql
query {
input(value: "foo bar") @upperCase
}
// => { input: "FOO BAR" }
# => { input: "FOO BAR" }
```

- Using camelCase directive:

```javascript
```graphql
query {
input(value: "foo bar") @camelCase
}
// => { input: "fooBar" }
# => { input: "fooBar" }
```

- Using startCase directive:

```javascript
```graphql
query {
input(value: "foo bar") @startCase
}
// => { input: "Foo Bar" }
# => { input: "Foo Bar" }
```

- Using capitalize directive:

```javascript
```graphql
query {
input(value: "foo bar") @capitalize
}
// => { input: "Foo var" }
# => { input: "Foo var" }
```

- Using kebabCase directive:

```javascript
```graphql
query {
input(value: "foo bar") @kebabCase
}
// => { input: "foo-bar" }
# => { input: "foo-bar" }
```

- Using trim directive:

```javascript
```graphql
query {
input(value: " foo bar ") @trim
}
// => { input: "foo bar" }
# => { input: "foo bar" }
```

- Using default directive:

```javascript
```graphql
query {
input @default(to:"N/A")
}
// => { input: "N/A" }
# => { input: "N/A" }
```

- Using toLower directive:

```javascript
```graphql
query {
input(value: "FOO BAR") @toLower
}
// => { input: "foo bar" }
# => { input: "foo bar" }
```

- Using toUpper directive:

```javascript
```graphql
query {
input(value: "foo bar") @toUpper
}
// => { input: "FOO BAR" }
# => { input: "FOO BAR" }
```

- Using template directive:

```javascript
```graphql
query {
input(value: "foo bar") @template(as:"${input} ${toUpper(input)}")
}
// => { input: "foo bar FOO BAR" }
# => { input: "foo bar FOO BAR" }
```

- Using template together with trim and toUpper directives:

```javascript
```graphql
query {
input(value: " foo bar ") @trim @template(as:"${input} ${input}") @toUpper
}
// => { input: "FOO BAR FOO BAR" }
# => { input: "FOO BAR FOO BAR" }
```

### Phone formatting directives
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"homepage": "https://github.com/lirown/graphql-custom-directives#readme",
"dependencies": {
"lodash": "^4.17.5",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why Lodash dependency twice?

"moment": "^2.22.2",
"numeral": "^2.0.6",
"lodash": "^4.17.5",
Expand Down
4 changes: 2 additions & 2 deletions src/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function wrapFieldsWithMiddleware(type, deepWrap = true, typeMet = {}, level = 1
* create a new graphql custom directive which contain a resolve
* function for altering the execution of the graphql
*/
exports.GraphQLCustomDirective = function(config) {
export const GraphQLCustomDirective = function(config) {
const directive = new GraphQLDirective(config);

if (config.resolve) {
Expand All @@ -189,7 +189,7 @@ exports.GraphQLCustomDirective = function(config) {
/**
* Apply custom directives support in the graphql schema
*/
exports.applySchemaCustomDirectives = function(schema) {
export const applySchemaCustomDirectives = function(schema) {
if (!(schema instanceof GraphQLSchema)) {
throw new Error('Schema must be instanceof GraphQLSchema');
}
Expand Down
2 changes: 1 addition & 1 deletion src/directives/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import numeral from 'numeral';

const DEFAULT_CURRENCY_FORMAT = '$0,0';

exports.GraphQLCurrencyDirective = new GraphQLCustomDirective({
export const GraphQLCurrencyDirective = new GraphQLCustomDirective({
name: 'currency',
description: 'Format the currency value from resolving the field',
locations: [DirectiveLocation.FIELD],
Expand Down
4 changes: 2 additions & 2 deletions src/directives/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const moment = require('moment');

const DEFAULT_DATE_FORMAT = 'DD MMM YYYY HH:mm';

exports.GraphQLDateDirective = new GraphQLCustomDirective({
export const GraphQLDateDirective = new GraphQLCustomDirective({
name: 'date',
description: 'Format the date from resolving the field by moment module',
locations: [DirectiveLocation.FIELD],
Expand Down Expand Up @@ -38,7 +38,7 @@ exports.GraphQLDateDirective = new GraphQLCustomDirective({
},
});

exports.GraphQLTimeOffsetDirective = new GraphQLCustomDirective({
export const GraphQLTimeOffsetDirective = new GraphQLCustomDirective({
name: 'timeOffset',
description: 'Add offset (in minutes) to a 13 digit unixtime',
locations: [DirectiveLocation.FIELD],
Expand Down
2 changes: 1 addition & 1 deletion src/directives/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import numeral from 'numeral';

const DEFAULT_NUMBER_FORMAT = '0,0';

exports.GraphQLNumberDirective = new GraphQLCustomDirective({
export const GraphQLNumberDirective = new GraphQLCustomDirective({
name: 'number',
description: 'Format the number value from resolving the field',
locations: [DirectiveLocation.FIELD],
Expand Down
Loading