Skip to content
This repository was archived by the owner on Jan 4, 2020. It is now read-only.

Commit b61816e

Browse files
committed
added new types
updated dependencies
1 parent a5ff06a commit b61816e

File tree

9 files changed

+200
-22
lines changed

9 files changed

+200
-22
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
Custom types plugin for graphql-factory
33

44
### Custom Types
5+
* `FactroyBase64`
56
* `FactoryDateTime`
7+
* `FactoryEmail`
68
* `FactoryJSON`
9+
* `FactoryURL`
710

811
### Example
912
Create a simple type and schema in `graphql-factory` and execute a query
@@ -64,4 +67,6 @@ lib.Log('{ getLog { timestamp, message, info } }')
6467
The following types were ported to graphql-factory format but all credit goes to the original developers.
6568

6669
* `FactoryDateTime` ported from [`graphql-custom-datetype`](https://github.com/soundtrackyourbrand/graphql-custom-datetype)
70+
* `FactoryEmail` ported from [`graphql-custom-types`](https://github.com/stylesuxx/graphql-custom-types)
6771
* `FactoryJSON` ported from [`graphql-type-json`](https://github.com/taion/graphql-type-json)
72+
* `FactoryURL` ported from [`graphql-custom-types`](https://github.com/stylesuxx/graphql-custom-types)

index.js

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@
22

33
Object.defineProperty(exports, '__esModule', { value: true });
44

5+
var FactoryBase64 = {
6+
type: 'Scalar',
7+
name: 'FactoryBase64',
8+
description: 'Converts value to and from base64',
9+
serialize: function serialize(value) {
10+
return new Buffer(value, 'base64').toString();
11+
},
12+
parseValue: function parseValue(value) {
13+
return new Buffer(value).toString('base64');
14+
},
15+
parseLiteral: function parseLiteral(ast) {
16+
var _graphql = this.graphql,
17+
GraphQLError = _graphql.GraphQLError,
18+
Kind = _graphql.Kind;
19+
20+
21+
if (ast.kind !== Kind.STRING) {
22+
throw new GraphQLError('Query error: expected Base64 to be a string but got a: ' + ast.kind, [ast]);
23+
}
24+
25+
return new Buffer(ast.value).toString('base64');
26+
}
27+
};
28+
529
/*
630
* Ported type from https://github.com/soundtrackyourbrand/graphql-custom-datetype
731
*/
@@ -20,12 +44,13 @@ function coerceDate(value) {
2044
var FactoryDateTime = {
2145
type: 'Scalar',
2246
name: 'FactoryDateTime',
47+
description: 'Represents a Date object',
2348
serialize: coerceDate,
2449
parseValue: coerceDate,
2550
parseLiteral: function parseLiteral(ast) {
26-
var _graphql = this.graphql;
27-
var GraphQLError = _graphql.GraphQLError;
28-
var Kind = _graphql.Kind;
51+
var _graphql = this.graphql,
52+
GraphQLError = _graphql.GraphQLError,
53+
Kind = _graphql.Kind;
2954

3055

3156
if (ast.kind !== Kind.STRING) {
@@ -42,10 +67,41 @@ var FactoryDateTime = {
4267
}
4368
};
4469

70+
var FactoryEmail = {
71+
type: 'Scalar',
72+
name: 'FactoryEmail',
73+
description: 'The Email scalar type represents E-Mail addresses compliant to RFC 822.',
74+
serialize: function serialize(value) {
75+
return value;
76+
},
77+
parseValue: function parseValue(value) {
78+
return value;
79+
},
80+
parseLiteral: function parseLiteral(ast) {
81+
var _graphql = this.graphql,
82+
GraphQLError = _graphql.GraphQLError,
83+
Kind = _graphql.Kind;
84+
85+
// regex taken from https://github.com/stylesuxx/graphql-custom-types
86+
87+
var rx = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
88+
89+
if (ast.kind !== Kind.STRING) {
90+
throw new GraphQLError('Query error: expected Email to be a string but got a: ' + ast.kind, [ast]);
91+
}
92+
93+
if (!ast.value.match(rx)) {
94+
throw new GraphQLError('Query error: invalid Email', [ast]);
95+
}
96+
97+
return ast.value;
98+
}
99+
};
100+
45101
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
46102
return typeof obj;
47103
} : function (obj) {
48-
return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
104+
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
49105
};
50106

51107
/*
@@ -97,14 +153,49 @@ var FactoryJSON = {
97153
parseLiteral: parseLiteral
98154
};
99155

156+
var FactoryURL = {
157+
type: 'Scalar',
158+
name: 'FactoryURL',
159+
description: 'The URL scalar type represents URL addresses.',
160+
serialize: function serialize(value) {
161+
return value;
162+
},
163+
parseValue: function parseValue(value) {
164+
return value;
165+
},
166+
parseLiteral: function parseLiteral(ast) {
167+
var _graphql = this.graphql,
168+
GraphQLError = _graphql.GraphQLError,
169+
Kind = _graphql.Kind;
170+
171+
// regex taken from https://github.com/stylesuxx/graphql-custom-types
172+
173+
var rx = new RegExp('^(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?$', 'i');
174+
175+
if (ast.kind !== Kind.STRING) {
176+
throw new GraphQLError('Query error: expected URL to be a string but got a: ' + ast.kind, [ast]);
177+
}
178+
179+
if (!ast.value.match(rx)) {
180+
throw new GraphQLError('Query error: invalid URL', [ast]);
181+
}
182+
183+
return ast.value;
184+
}
185+
};
186+
100187
var types = {
188+
FactoryBase64: FactoryBase64,
101189
FactoryDateTime: FactoryDateTime,
102-
FactoryJSON: FactoryJSON
190+
FactoryEmail: FactoryEmail,
191+
FactoryJSON: FactoryJSON,
192+
FactoryURL: FactoryURL
103193
};
104194

105195
var index = {
196+
name: 'GraphQLFactoryTypes',
106197
types: types
107198
};
108199

109200
exports.types = types;
110-
exports['default'] = index;
201+
exports['default'] = index;

package.json

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-factory-types",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "Custom types plugin for graphql-factory",
55
"license": "MIT",
66
"main": "index.js",
@@ -19,22 +19,19 @@
1919
"author": "Branden Horiuchi <bhoriuchi@gmail.com>",
2020
"dependencies": {},
2121
"devDependencies": {
22-
"graphql": "^0.6.0",
23-
"graphql-factory": "^0.4.0",
22+
"graphql": "^0.8.0",
23+
"graphql-factory": "^1.0.0",
2424
"chai": "^3.5.0",
25-
"babel-core": "^6.0.0",
26-
"babel-preset-es2015": "6.13.0",
27-
"babel-preset-stage-2": "^6.0.0",
28-
"babel-plugin-transform-runtime": "^6.0.0",
29-
"babel-runtime": "^6.9.0",
30-
"babel-register": "^6.11.0",
25+
"babel-core": "^6.21.0",
26+
"babel-preset-es2015": "^6.18.0",
27+
"babel-preset-stage-2": "^6.18.0",
28+
"babel-plugin-transform-runtime": "^6.15.0",
29+
"babel-runtime": "^6.20.0",
30+
"babel-register": "^6.18.0",
3131
"mocha": "^3.0.0",
32-
"node-getopt": "^0.2.0",
33-
"rethinkdbdash": "^2.3.6",
34-
"rewire": "^2.5.1",
35-
"rollup": "^0.34.0",
36-
"rollup-plugin-babel": "^2.6.0",
37-
"babel-preset-es2015-rollup": "^1.1.0"
32+
"rollup": "^0.38.0",
33+
"rollup-plugin-babel": "^2.7.0",
34+
"babel-preset-es2015-rollup": "^3.0.0"
3835
},
3936
"bugs": {
4037
"url": "https://github.com/graphql-factory/graphql-factory-types/issues"

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import types from './types/index'
33
export { types }
44

55
export default {
6+
name: 'GraphQLFactoryTypes',
67
types
78
}

src/types/FactoryBase64.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default {
2+
type: 'Scalar',
3+
name: 'FactoryBase64',
4+
description: 'Converts value to and from base64',
5+
serialize (value) {
6+
return (new Buffer(value, 'base64')).toString()
7+
},
8+
parseValue (value) {
9+
return (new Buffer(value)).toString('base64')
10+
},
11+
parseLiteral (ast) {
12+
let { GraphQLError, Kind } = this.graphql
13+
14+
if (ast.kind !== Kind.STRING) {
15+
throw new GraphQLError('Query error: expected Base64 to be a string but got a: ' + ast.kind, [ast])
16+
}
17+
18+
return (new Buffer(ast.value)).toString('base64')
19+
}
20+
}

src/types/FactoryDateTime.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function coerceDate (value) {
1616
export default {
1717
type: 'Scalar',
1818
name: 'FactoryDateTime',
19+
description: 'Represents a Date object',
1920
serialize: coerceDate,
2021
parseValue: coerceDate,
2122
parseLiteral (ast) {

src/types/FactoryEmail.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default {
2+
type: 'Scalar',
3+
name: 'FactoryEmail',
4+
description: 'The Email scalar type represents E-Mail addresses compliant to RFC 822.',
5+
serialize (value) {
6+
return value
7+
},
8+
parseValue (value) {
9+
return value
10+
},
11+
parseLiteral (ast) {
12+
let { GraphQLError, Kind } = this.graphql
13+
14+
// regex taken from https://github.com/stylesuxx/graphql-custom-types
15+
let rx = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i
16+
17+
if (ast.kind !== Kind.STRING) {
18+
throw new GraphQLError('Query error: expected Email to be a string but got a: ' + ast.kind, [ast])
19+
}
20+
21+
if (!ast.value.match(rx)) {
22+
throw new GraphQLError('Query error: invalid Email', [ast])
23+
}
24+
25+
return ast.value
26+
}
27+
}

src/types/FactoryURL.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default {
2+
type: 'Scalar',
3+
name: 'FactoryURL',
4+
description: 'The URL scalar type represents URL addresses.',
5+
serialize (value) {
6+
return value
7+
},
8+
parseValue (value) {
9+
return value
10+
},
11+
parseLiteral (ast) {
12+
let { GraphQLError, Kind } = this.graphql
13+
14+
// regex taken from https://github.com/stylesuxx/graphql-custom-types
15+
let rx = new RegExp('^(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?$', 'i')
16+
17+
if (ast.kind !== Kind.STRING) {
18+
throw new GraphQLError('Query error: expected URL to be a string but got a: ' + ast.kind, [ast])
19+
}
20+
21+
if (!ast.value.match(rx)) {
22+
throw new GraphQLError('Query error: invalid URL', [ast])
23+
}
24+
25+
return ast.value
26+
}
27+
}

src/types/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import FactoryBase64 from './FactoryBase64'
12
import FactoryDateTime from './FactoryDateTime'
3+
import FactoryEmail from './FactoryEmail'
24
import FactoryJSON from './FactoryJSON'
5+
import FactoryURL from './FactoryURL'
36

7+
export { FactoryBase64}
48
export { FactoryDateTime }
9+
export { FactoryEmail }
510
export { FactoryJSON }
11+
export { FactoryURL }
612

713
export default {
14+
FactoryBase64,
815
FactoryDateTime,
9-
FactoryJSON
16+
FactoryEmail,
17+
FactoryJSON,
18+
FactoryURL
1019
}

0 commit comments

Comments
 (0)