├── .gitignore ├── .npmignore ├── Gruntfile.js ├── Makefile ├── README.md ├── eslint.yaml ├── package.json ├── src ├── gtt-1-void.js ├── gtt-2-int.js ├── gtt-3-float.js ├── gtt-4-string.js ├── gtt-5-date.js ├── gtt-6-uuid.js ├── gtt-7-json.js └── gtt.js └── tst └── gtt.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | lib 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | /* global module: true */ 26 | module.exports = function (grunt) { 27 | grunt.loadNpmTasks("grunt-eslint") 28 | grunt.loadNpmTasks("grunt-babel") 29 | grunt.loadNpmTasks("grunt-mocha-test") 30 | grunt.loadNpmTasks("grunt-contrib-clean") 31 | grunt.loadNpmTasks("grunt-contrib-watch") 32 | grunt.initConfig({ 33 | eslint: { 34 | options: { 35 | configFile: "eslint.yaml" 36 | }, 37 | "graphql-tools-types": [ "src/**/*.js", "tst/**/*.js" ] 38 | }, 39 | babel: { 40 | "graphql-tools-types": { 41 | files: [ 42 | { 43 | expand: true, 44 | cwd: "src/", 45 | src: [ "*.js" ], 46 | dest: "lib/" 47 | } 48 | ], 49 | options: { 50 | sourceMap: false, 51 | presets: [ 52 | [ "@babel/preset-env", { 53 | "targets": { 54 | "node": "10.0.0" 55 | } 56 | } ] 57 | ], 58 | plugins: [ 59 | [ "@babel/plugin-transform-runtime", { 60 | "corejs": 3, 61 | "helpers": true, 62 | "regenerator": false 63 | } ] 64 | ] 65 | } 66 | } 67 | }, 68 | mochaTest: { 69 | "graphql-tools-types": { 70 | src: [ "tst/*.js", "!tst/common.js" ] 71 | }, 72 | options: { 73 | reporter: "spec", 74 | require: "tst/common.js" 75 | } 76 | }, 77 | clean: { 78 | clean: [ "lib" ], 79 | distclean: [ "node_modules" ] 80 | }, 81 | watch: { 82 | "src": { 83 | files: [ "src/**/*.js", "tst/**/*.js" ], 84 | tasks: [ "default" ], 85 | options: {} 86 | } 87 | } 88 | }) 89 | grunt.registerTask("default", [ "eslint", "babel" ]) 90 | grunt.registerTask("test", [ "mochaTest" ]) 91 | grunt.registerTask("dev", [ "default", "watch" ]) 92 | } 93 | 94 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ## Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ## 5 | ## Permission is hereby granted, free of charge, to any person obtaining 6 | ## a copy of this software and associated documentation files (the 7 | ## "Software"), to deal in the Software without restriction, including 8 | ## without limitation the rights to use, copy, modify, merge, publish, 9 | ## distribute, sublicense, and/or sell copies of the Software, and to 10 | ## permit persons to whom the Software is furnished to do so, subject to 11 | ## the following conditions: 12 | ## 13 | ## The above copyright notice and this permission notice shall be included 14 | ## in all copies or substantial portions of the Software. 15 | ## 16 | ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ## MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ## IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ## CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ## TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ## SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | ## 24 | 25 | NPM = npm 26 | GRUNT = ./node_modules/grunt-cli/bin/grunt 27 | 28 | all: build 29 | 30 | bootstrap: 31 | @if [ ! -x $(GRUNT) ]; then $(NPM) install; fi 32 | 33 | build: bootstrap 34 | @$(GRUNT) 35 | 36 | clean: bootstrap 37 | @$(GRUNT) clean:clean 38 | 39 | distclean: bootstrap 40 | @$(GRUNT) clean:clean clean:distclean 41 | 42 | test: 43 | @$(GRUNT) test 44 | 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | GraphQL-Tools-Types 3 | =================== 4 | 5 | Custom Scalar Types for GraphQL-Tools 6 | 7 |

8 | 9 | 10 |

11 | 12 | 13 | About 14 | ----- 15 | 16 | This Node.js module provides the custom scalar types Void, Integer, Float, 17 | String, Date, Universally Unique Identifier (UUID) and 18 | JavaScript Object Notation (JSON) for 19 | [GraphQL Tools](https://github.com/apollostack/graphql-tools), 20 | a wrapper around the [GraphQL](http://graphql.org/) engine 21 | [GraphQL.js](https://github.com/graphql/graphql-js). 22 | 23 | Installation 24 | ------------ 25 | 26 | ```shell 27 | $ npm install graphql-tools-types 28 | ``` 29 | 30 | Usage 31 | ----- 32 | 33 | ```js 34 | import UUID from "pure-uuid" 35 | import * as GraphQL from "graphql" 36 | import * as GraphQLTools from "graphql-tools" 37 | import GraphQLToolsTypes from "graphql-tools-types" 38 | 39 | let definition = ` 40 | schema { 41 | query: RootQuery 42 | } 43 | scalar Void 44 | scalar MyInt 45 | scalar MyFloat 46 | scalar MyString 47 | scalar Date 48 | scalar UUID 49 | scalar JSON 50 | scalar Coord 51 | type RootQuery { 52 | exampleVoid: Void 53 | exampleMyInt(num: MyInt): Int 54 | exampleMyFloat(num: MyFloat): Float 55 | exampleMyString(str: MyString): String 56 | exampleDate(date: Date): Date 57 | exampleUUID(uuid: UUID): UUID 58 | exampleJSON(json: JSON): JSON 59 | exampleCoord(coord: Coord): Coord 60 | } 61 | ` 62 | let resolvers = { 63 | Void: GraphQLToolsTypes.Void({ name: "MyVoid" }), 64 | MyInt: GraphQLToolsTypes.Int({ name: "MyInt", min: 0, max: 100 }), 65 | MyFloat: GraphQLToolsTypes.Float({ name: "MyFloat", min: 0.0, max: 100.0 }), 66 | MyString: GraphQLToolsTypes.String({ name: "MyString", regex: /^(?:foo|bar|quux)$/ }), 67 | Date: GraphQLToolsTypes.Date({ name: "MyDate" }), 68 | UUID: GraphQLToolsTypes.UUID({ name: "MyUUID" }), 69 | JSON: GraphQLToolsTypes.JSON({ name: "MyJSON" }), 70 | Coord: GraphQLToolsTypes.JSON({ name: "Coord", struct: "{ x: number, y: number }" }), 71 | RootQuery: { 72 | exampleVoid: (root, args, ctx, info) => { 73 | return {} 74 | }, 75 | exampleMyInt: (root, args, ctx, info) => { 76 | return args.num 77 | }, 78 | exampleMyFloat: (root, args, ctx, info) => { 79 | return args.num 80 | }, 81 | exampleMyString: (root, args, ctx, info) => { 82 | return args.str 83 | }, 84 | exampleDate: (root, args, ctx, info) => { 85 | return args.date 86 | }, 87 | exampleUUID: (root, args, ctx, info) => { 88 | return args.uuid 89 | }, 90 | exampleJSON: (root, args, ctx, info) => { 91 | return args.json 92 | }, 93 | exampleCoord: (root, args, ctx, info) => { 94 | return args.coord 95 | } 96 | } 97 | } 98 | let schema = GraphQLTools.makeExecutableSchema({ 99 | typeDefs: [ definition ], 100 | resolvers: resolvers, 101 | }) 102 | let query = ` 103 | query ($date: Date, $uuid: UUID, $json: JSON, $coord: Coord) { 104 | exampleVoid, 105 | exampleMyInt(num: 100), 106 | exampleMyFloat(num: 42.7), 107 | exampleMyString(str: "foo") 108 | date1: exampleDate(date: $date), 109 | date2: exampleDate(date: "2016-08-16T00:01:02.000Z"), 110 | uuid1: exampleUUID(uuid: $uuid), 111 | uuid2: exampleUUID(uuid: "6cbe657c-63e3-11e6-aa83-080027e303e4"), 112 | json1: exampleJSON(json: $json), 113 | json2: exampleJSON(json: { foo: "bar", baz: 42, quux: true }), 114 | coord1: exampleCoord(coord: $coord), 115 | coord2: exampleCoord(coord: { x: 7, y: 42 }) 116 | } 117 | ` 118 | let variables = { 119 | date: "2016-08-16T00:01:02.000Z", 120 | uuid: "6cbe657c-63e3-11e6-aa83-080027e303e4", 121 | json: { foo: "bar", baz: 42, quux: true }, 122 | coord: { x: 7, y: 42 } 123 | } 124 | GraphQL.graphql(schema, query, null, null, variables).then((result) => { 125 | console.log("OK", result) 126 | }).catch((result) => { 127 | console.log("ERROR", result) 128 | }) 129 | ``` 130 | 131 | ```sh 132 | $ npm test 133 | OK { data: 134 | { exampleJSON: { foo: 'bar', baz: 42, quux: true }, 135 | exampleUUID: '6cbe657c-63e3-11e6-aa83-080027e303e4', 136 | exampleDate: '2016-08-16T00:01:02.000Z', 137 | exampleVoid: {}, 138 | exampleMyInt: 100, 139 | exampleMyFloat: 42.7, 140 | exampleMyString: 'foo' } } 141 | ``` 142 | 143 | License 144 | ------- 145 | 146 | Copyright (c) 2016-2021 Dr. Ralf S. Engelschall (http://engelschall.com/) 147 | 148 | Permission is hereby granted, free of charge, to any person obtaining 149 | a copy of this software and associated documentation files (the 150 | "Software"), to deal in the Software without restriction, including 151 | without limitation the rights to use, copy, modify, merge, publish, 152 | distribute, sublicense, and/or sell copies of the Software, and to 153 | permit persons to whom the Software is furnished to do so, subject to 154 | the following conditions: 155 | 156 | The above copyright notice and this permission notice shall be included 157 | in all copies or substantial portions of the Software. 158 | 159 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 160 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 162 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 163 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 164 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 165 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 166 | 167 | -------------------------------------------------------------------------------- /eslint.yaml: -------------------------------------------------------------------------------- 1 | ## 2 | ## GraphQL-Tools-Sequelize -- Integration of GraphQL-Tools and Sequelize ORM 3 | ## Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ## 5 | ## Permission is hereby granted, free of charge, to any person obtaining 6 | ## a copy of this software and associated documentation files (the 7 | ## "Software"), to deal in the Software without restriction, including 8 | ## without limitation the rights to use, copy, modify, merge, publish, 9 | ## distribute, sublicense, and/or sell copies of the Software, and to 10 | ## permit persons to whom the Software is furnished to do so, subject to 11 | ## the following conditions: 12 | ## 13 | ## The above copyright notice and this permission notice shall be included 14 | ## in all copies or substantial portions of the Software. 15 | ## 16 | ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ## MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ## IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ## CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ## TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ## SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | ## 24 | 25 | --- 26 | 27 | extends: 28 | - eslint:recommended 29 | - eslint-config-standard 30 | 31 | parserOptions: 32 | ecmaVersion: 8 33 | sourceType: module 34 | ecmaFeatures: 35 | jsx: false 36 | 37 | parser: babel-eslint 38 | 39 | env: 40 | browser: true 41 | node: false 42 | commonjs: true 43 | worker: true 44 | serviceworker: true 45 | 46 | globals: 47 | process: true 48 | 49 | rules: 50 | # modified rules 51 | indent: [ "error", 4, { "SwitchCase": 1 } ] 52 | linebreak-style: [ "error", "unix" ] 53 | semi: [ "error", "never" ] 54 | operator-linebreak: [ "error", "after", { "overrides": { "&&": "before", "||": "before", ":": "before" } } ] 55 | brace-style: [ "error", "stroustrup", { "allowSingleLine": true } ] 56 | quotes: [ "error", "double" ] 57 | 58 | # disabled rules 59 | no-multi-spaces: off 60 | no-multiple-empty-lines: off 61 | key-spacing: off 62 | object-property-newline: off 63 | curly: off 64 | space-in-parens: off 65 | array-bracket-spacing: off 66 | 67 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graphql-tools-types", 3 | "version": "1.3.2", 4 | "description": "Custom Types for GraphQL-Tools", 5 | "keywords": [ "graphql", "graphql-tools", "custom", "types", "json", "uuid" ], 6 | "main": "./lib/gtt.js", 7 | "license": "MIT", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/rse/graphql-tools-types.git" 11 | }, 12 | "author": { 13 | "name": "Dr. Ralf S. Engelschall", 14 | "email": "rse@engelschall.com", 15 | "url": "http://engelschall.com" 16 | }, 17 | "homepage": "https://github.com/rse/graphql-tools-types", 18 | "bugs": "https://github.com/rse/graphql-tools-types/issues", 19 | "dependencies": { 20 | "ducky": "2.7.3", 21 | "pure-uuid": "1.6.2", 22 | "graphql": "15.4.0", 23 | "@babel/runtime-corejs3": "7.12.5" 24 | }, 25 | "devDependencies": { 26 | "graphql-tools": "7.0.2", 27 | "grunt": "1.3.0", 28 | "grunt-cli": "1.3.2", 29 | "grunt-contrib-clean": "2.0.0", 30 | "grunt-contrib-watch": "1.1.0", 31 | "grunt-babel": "8.0.0", 32 | "grunt-mocha-test": "0.13.3", 33 | "grunt-eslint": "23.0.0", 34 | "babel-eslint": "10.1.0", 35 | "@babel/core": "7.12.10", 36 | "@babel/node": "7.12.10", 37 | "eslint": "7.17.0", 38 | "eslint-config-standard": "16.0.2", 39 | "eslint-plugin-promise": "4.2.1", 40 | "eslint-plugin-import": "2.22.1", 41 | "eslint-plugin-node": "11.1.0", 42 | "@babel/preset-env": "7.12.11", 43 | "@babel/plugin-transform-runtime": "7.12.10", 44 | "mocha": "8.2.1", 45 | "chai": "4.2.0" 46 | }, 47 | "engines": { 48 | "node": ">=10.0.0" 49 | }, 50 | "scripts": { 51 | "prepublishOnly": "grunt default", 52 | "build": "grunt default", 53 | "test": "babel-node --presets @babel/preset-env tst/gtt.js" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/gtt-1-void.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | /* external requirements */ 26 | import Ducky from "ducky" 27 | import * as GraphQLLanguage from "graphql/language" 28 | import { GraphQLError } from "graphql/error" 29 | 30 | /* Void resolver for GraphQL Tools */ 31 | export default function ResolverVoid (options = {}) { 32 | const errors = [] 33 | if (!Ducky.validate(options, "{ name: string, value?: any }", errors)) 34 | throw new GraphQLError("[graphql-tools-types] " + 35 | `invalid parameters: ${errors.join("; ")}`, []) 36 | if (options.value === undefined) 37 | options.value = {} 38 | return { 39 | /* serialize value sent as output to the client */ 40 | __serialize: (/* value */) => { 41 | return options.value 42 | }, 43 | 44 | /* parse value received as input from client */ 45 | __parseValue: (value) => { 46 | if (typeof value === "object") 47 | return options.value 48 | else 49 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 50 | "invalid Void input value (object expected)", []) 51 | }, 52 | 53 | /* parse value received as literal in AST */ 54 | __parseLiteral: (ast) => { 55 | if (ast.kind !== GraphQLLanguage.Kind.OBJECT) 56 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 57 | "invalid Void literal (object expected)", [ ast ]) 58 | const value = options.value 59 | return value 60 | } 61 | } 62 | } 63 | 64 | -------------------------------------------------------------------------------- /src/gtt-2-int.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | /* external requirements */ 26 | import Ducky from "ducky" 27 | import * as GraphQL from "graphql" 28 | import * as GraphQLLanguage from "graphql/language" 29 | import { GraphQLError } from "graphql/error" 30 | 31 | /* Integer resolver for GraphQL Tools */ 32 | export default function ResolverInt (options = {}) { 33 | const errors = [] 34 | if (!Ducky.validate(options, "{ name: string, min?: number, max?: number, fn?: function }", errors)) 35 | throw new GraphQLError("[graphql-tools-types] " + 36 | `invalid parameters: ${errors.join("; ")}`, []) 37 | const validate = (value, ast = null) => { 38 | if (options.min !== undefined && value < options.min) 39 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 40 | `minimum value is ${options.min}`, ast !== null ? [ ast ] : []) 41 | if (options.max !== undefined && value > options.max) 42 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 43 | `maximum value is ${options.max}`, ast !== null ? [ ast ] : []) 44 | if (options.fn !== undefined && !options.fn(value)) 45 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 46 | "value not valid", ast !== null ? [ ast ] : []) 47 | } 48 | return { 49 | /* serialize value sent as output to the client */ 50 | __serialize: (value) => { 51 | return value 52 | }, 53 | 54 | /* parse value received as input from client */ 55 | __parseValue: (value) => { 56 | if (typeof value === "string") { 57 | const re = /^[+-]?\d+$/ 58 | if (!re.test(value)) 59 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 60 | `invalid input value (matching regular expression "${re}" expected)`, []) 61 | value = parseInt(value, 10) 62 | } 63 | else if (typeof value !== "number") 64 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 65 | "invalid input value (type \"number\" or \"string\" expected)", []) 66 | validate(value) 67 | return value 68 | }, 69 | 70 | /* parse value received as literal in AST */ 71 | __parseLiteral: (ast) => { 72 | if (ast.kind !== GraphQLLanguage.Kind.INT) 73 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 74 | "invalid AST node (kind \"INT\" expected)", [ ast ]) 75 | const value = GraphQL.GraphQLInt.parseLiteral(ast) 76 | validate(value, ast) 77 | return value 78 | } 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /src/gtt-3-float.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | /* external requirements */ 26 | import Ducky from "ducky" 27 | import * as GraphQL from "graphql" 28 | import * as GraphQLLanguage from "graphql/language" 29 | import { GraphQLError } from "graphql/error" 30 | 31 | /* Float resolver for GraphQL Tools */ 32 | export default function ResolverFloat (options = {}) { 33 | const errors = [] 34 | if (!Ducky.validate(options, "{ name: string, min?: number, max?: number, fn?: function }", errors)) 35 | throw new GraphQLError("[graphql-tools-types] " + 36 | `invalid parameters: ${errors.join("; ")}`, []) 37 | const validate = (value, ast = null) => { 38 | if (options.min !== undefined && value < options.min) 39 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 40 | `minimum value is ${options.min}`, ast !== null ? [ ast ] : []) 41 | if (options.max !== undefined && value > options.max) 42 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 43 | `maximum value is ${options.max}`, ast !== null ? [ ast ] : []) 44 | if (options.fn !== undefined && !options.fn(value)) 45 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 46 | "value not valid", ast !== null ? [ ast ] : []) 47 | } 48 | return { 49 | /* serialize value sent as output to the client */ 50 | __serialize: (value) => { 51 | return value 52 | }, 53 | 54 | /* parse value received as input from client */ 55 | __parseValue: (value) => { 56 | if (typeof value === "string") { 57 | const re = /^[+-]?(?:\d*\.?\d+(?:[eE][+-]?\d+)?|Infinity)$/ 58 | if (!re.test(value)) 59 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 60 | `invalid input value (matching regular expression "${re}" expected)`, []) 61 | value = parseFloat(value) 62 | } 63 | else if (typeof value !== "number") 64 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 65 | "invalid input value (type \"number\" or \"string\" expected)", []) 66 | validate(value) 67 | return value 68 | }, 69 | 70 | /* parse value received as literal in AST */ 71 | __parseLiteral: (ast) => { 72 | if (ast.kind !== GraphQLLanguage.Kind.FLOAT) 73 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 74 | "invalid AST node (kind \"FLOAT\" expected)", [ ast ]) 75 | const value = GraphQL.GraphQLFloat.parseLiteral(ast) 76 | validate(value, ast) 77 | return value 78 | } 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /src/gtt-4-string.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | /* external requirements */ 26 | import Ducky from "ducky" 27 | import * as GraphQL from "graphql" 28 | import * as GraphQLLanguage from "graphql/language" 29 | import { GraphQLError } from "graphql/error" 30 | 31 | /* String resolver for GraphQL Tools */ 32 | export default function ResolverString (options = {}) { 33 | const errors = [] 34 | if (!Ducky.validate(options, "{ name: string, min?: number, max?: number, regex?: RegExp, fn?: function }", errors)) 35 | throw new GraphQLError("[graphql-tools-types] " + 36 | `invalid parameters: ${errors.join("; ")}`, []) 37 | const validate = (value, ast = null) => { 38 | if (options.min !== undefined && value.length < options.min) 39 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 40 | `minimum length is ${options.min}`, ast !== null ? [ ast ] : []) 41 | if (options.max !== undefined && value.length > options.max) 42 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 43 | `maximum length is ${options.max}`, ast !== null ? [ ast ] : []) 44 | if (options.regex !== undefined && !options.regex.test(value)) 45 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 46 | `value does not match ${options.regex}`, ast !== null ? [ ast ] : []) 47 | if (options.fn !== undefined && !options.fn(value)) 48 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 49 | "value not valid", ast !== null ? [ ast ] : []) 50 | } 51 | return { 52 | /* serialize value sent as output to the client */ 53 | __serialize: (value) => { 54 | return value 55 | }, 56 | 57 | /* parse value received as input from client */ 58 | __parseValue: (value) => { 59 | if (typeof value !== "string") 60 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 61 | "invalid input value (type \"string\" expected)", []) 62 | validate(value) 63 | return value 64 | }, 65 | 66 | /* parse value received as literal in AST */ 67 | __parseLiteral: (ast) => { 68 | if (ast.kind !== GraphQLLanguage.Kind.STRING) 69 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 70 | "invalid AST node (kind \"STRING\" expected)", [ ast ]) 71 | const value = GraphQL.GraphQLString.parseLiteral(ast) 72 | validate(value, ast) 73 | return value 74 | } 75 | } 76 | } 77 | 78 | -------------------------------------------------------------------------------- /src/gtt-5-date.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | /* external requirements */ 26 | import Ducky from "ducky" 27 | import * as GraphQL from "graphql" 28 | import * as GraphQLLanguage from "graphql/language" 29 | import { GraphQLError } from "graphql/error" 30 | 31 | /* Date resolver for GraphQL Tools */ 32 | export default function ResolverDate (options = {}) { 33 | const errors = [] 34 | if (!Ducky.validate(options, "{ name: string, serialization?: string, min?: (string|Date), max?: (string|Date), fn?: function }", errors)) 35 | throw new GraphQLError("[graphql-tools-types] " + 36 | `invalid parameters: ${errors.join("; ")}`, []) 37 | if (options.serialization === undefined) 38 | options.serialization = "string" 39 | if (typeof options.min === "string") 40 | options.min = new Date(options.min) 41 | if (typeof options.max === "string") 42 | options.max = new Date(options.max) 43 | const validate = (value, ast = null) => { 44 | if (options.min !== undefined && value.getTime() < options.min.getTime()) 45 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 46 | `minimum date is ${options.min.toISOString()}`, ast !== null ? [ ast ] : []) 47 | if (options.max !== undefined && value.getTime() > options.max.getTime()) 48 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 49 | `maximum date is ${options.max.toISOString()}`, ast !== null ? [ ast ] : []) 50 | if (options.fn !== undefined && !options.fn(value)) 51 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 52 | "value not valid", ast !== null ? [ ast ] : []) 53 | } 54 | return { 55 | /* serialize value sent as output to the client */ 56 | __serialize: (value) => { 57 | return (options.serialization === "string" ? value.toISOString() : value.getTime()) 58 | }, 59 | 60 | /* parse value received as input from client */ 61 | __parseValue: (value) => { 62 | if (typeof value === "string" || typeof value === "number") { 63 | value = new Date(value) 64 | if (isNaN(value.getTime())) 65 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 66 | "invalid Date input value", []) 67 | validate(value) 68 | return value 69 | } 70 | else if (typeof value === "object" && value instanceof Date) 71 | return value 72 | else 73 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 74 | "invalid Date input value (string or Date expected)", []) 75 | }, 76 | 77 | /* parse value received as literal in AST */ 78 | __parseLiteral: (ast) => { 79 | let value 80 | if (ast.kind === GraphQLLanguage.Kind.STRING) 81 | value = GraphQL.GraphQLString.parseLiteral(ast) 82 | else if (ast.kind === GraphQLLanguage.Kind.INT) 83 | value = GraphQL.GraphQLInt.parseLiteral(ast) 84 | else 85 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 86 | "invalid Date literal (string or number expected)", [ ast ]) 87 | value = new Date(value) 88 | if (isNaN(value.getTime())) 89 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 90 | "invalid Date literal (date within valid range expected)", []) 91 | validate(value, ast) 92 | return value 93 | } 94 | } 95 | } 96 | 97 | -------------------------------------------------------------------------------- /src/gtt-6-uuid.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | /* external requirements */ 26 | import Ducky from "ducky" 27 | import UUID from "pure-uuid" 28 | import * as GraphQL from "graphql" 29 | import * as GraphQLLanguage from "graphql/language" 30 | import { GraphQLError } from "graphql/error" 31 | 32 | /* UUID resolver for GraphQL Tools */ 33 | export default function ResolverUUID (options = {}) { 34 | const errors = [] 35 | if (!Ducky.validate(options, "{ name: string, storage?: string, fn?: function }", errors)) 36 | throw new GraphQLError("[graphql-tools-types] " + 37 | `invalid parameters: ${errors.join("; ")}`, []) 38 | if (options.storage === undefined) 39 | options.storage = "binary" 40 | const validate = (value, ast = null) => { 41 | if (options.fn !== undefined && !options.fn(value)) 42 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 43 | "value not valid", ast !== null ? [ ast ] : []) 44 | } 45 | return { 46 | /* serialize value sent as output to the client */ 47 | __serialize: (value) => { 48 | return (options.storage === "binary" ? value.format() : value) 49 | }, 50 | 51 | /* parse value received as input from client */ 52 | __parseValue: (value) => { 53 | if (typeof value !== "string") 54 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 55 | "invalid UUID input value (string expected)", []) 56 | try { 57 | value = new UUID(value) 58 | } 59 | catch (ex) { 60 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 61 | "invalid UUID string representation", []) 62 | } 63 | if (options.storage === "string") 64 | value = value.format() 65 | validate(value) 66 | return value 67 | }, 68 | 69 | /* parse value received as literal in AST */ 70 | __parseLiteral: (ast) => { 71 | if (ast.kind !== GraphQLLanguage.Kind.STRING) 72 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 73 | "invalid UUID literal (string expected)", [ ast ]) 74 | let value = GraphQL.GraphQLString.parseLiteral(ast) 75 | try { 76 | value = new UUID(value) 77 | } 78 | catch (ex) { 79 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 80 | "invalid UUID string representation", [ ast ]) 81 | } 82 | if (options.storage === "string") 83 | value = value.format() 84 | validate(value, ast) 85 | return value 86 | } 87 | } 88 | } 89 | 90 | -------------------------------------------------------------------------------- /src/gtt-7-json.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | /* external requirements */ 26 | import Ducky from "ducky" 27 | import * as GraphQL from "graphql" 28 | import * as GraphQLLanguage from "graphql/language" 29 | import { GraphQLError } from "graphql/error" 30 | 31 | /* JSON resolver for GraphQL Tools */ 32 | export default function ResolverJSON (options = {}) { 33 | const errors = [] 34 | if (!Ducky.validate(options, "{ name: string, struct?: string }", errors)) 35 | throw new GraphQLError("[graphql-tools-types] " + 36 | `invalid parameters: ${errors.join("; ")}`, []) 37 | return { 38 | /* serialize value sent as output to the client */ 39 | __serialize: (value) => { 40 | /* no-op as JSON is native output format */ 41 | return value 42 | }, 43 | 44 | /* parse value received as input from client */ 45 | __parseValue: (value) => { 46 | /* no-op (except for structure validation) as JSON is native input format */ 47 | if (options.struct !== undefined) { 48 | const errors = [] 49 | if (!Ducky.validate(value, options.struct, errors)) 50 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 51 | `unexpected JSON structure: ${errors.join("; ")}`, []) 52 | } 53 | return value 54 | }, 55 | 56 | /* parse value received as literal in AST */ 57 | __parseLiteral: (ast, variables) => { 58 | let result 59 | try { 60 | const parseJSONLiteral = (ast) => { 61 | switch (ast.kind) { 62 | case GraphQLLanguage.Kind.BOOLEAN: 63 | return GraphQL.GraphQLBoolean.parseLiteral(ast, variables) 64 | case GraphQLLanguage.Kind.INT: 65 | return GraphQL.GraphQLInt.parseLiteral(ast, variables) 66 | case GraphQLLanguage.Kind.STRING: 67 | return GraphQL.GraphQLString.parseLiteral(ast, variables) 68 | case GraphQLLanguage.Kind.FLOAT: 69 | return GraphQL.GraphQLFloat.parseLiteral(ast, variables) 70 | case GraphQLLanguage.Kind.ENUM: 71 | return String(ast.value) 72 | case GraphQLLanguage.Kind.VARIABLE: 73 | return variables ? variables[ast.name.value] : "" 74 | case GraphQLLanguage.Kind.LIST: 75 | return ast.values.map((astItem) => { 76 | return parseJSONLiteral(astItem) /* RECURSION */ 77 | }) 78 | case GraphQLLanguage.Kind.OBJECT: { 79 | const value = Object.create(null) 80 | ast.fields.forEach((field) => { 81 | value[field.name.value] = parseJSONLiteral(field.value) /* RECURSION */ 82 | }) 83 | return value 84 | } 85 | default: 86 | return null 87 | } 88 | } 89 | result = parseJSONLiteral(ast) 90 | } 91 | catch (ex) { 92 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 93 | `error parsing JSON: ${ex.toString()}`, [ ast ]) 94 | } 95 | if (options.struct !== undefined) { 96 | const errors = [] 97 | if (!Ducky.validate(result, options.struct, errors)) 98 | throw new GraphQLError(`[graphql-tools-types] ${options.name}: ` + 99 | `unexpected JSON structure: ${errors.join("; ")}`, []) 100 | } 101 | return result 102 | } 103 | } 104 | } 105 | 106 | -------------------------------------------------------------------------------- /src/gtt.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | /* import the resolver functions */ 26 | import ResolverVoid from "./gtt-1-void" 27 | import ResolverInt from "./gtt-2-int" 28 | import ResolverFloat from "./gtt-3-float" 29 | import ResolverString from "./gtt-4-string" 30 | import ResolverDate from "./gtt-5-date" 31 | import ResolverUUID from "./gtt-6-uuid" 32 | import ResolverJSON from "./gtt-7-json" 33 | 34 | /* export the resolver functions */ 35 | module.exports = { 36 | Void: ResolverVoid, 37 | Int: ResolverInt, 38 | Float: ResolverFloat, 39 | String: ResolverString, 40 | Date: ResolverDate, 41 | UUID: ResolverUUID, 42 | JSON: ResolverJSON 43 | } 44 | 45 | -------------------------------------------------------------------------------- /tst/gtt.js: -------------------------------------------------------------------------------- 1 | /* 2 | ** GraphQL-Tools-Types -- Custom Scalar Types for GraphQL-Tools 3 | ** Copyright (c) 2016-2021 Dr. Ralf S. Engelschall 4 | ** 5 | ** Permission is hereby granted, free of charge, to any person obtaining 6 | ** a copy of this software and associated documentation files (the 7 | ** "Software"), to deal in the Software without restriction, including 8 | ** without limitation the rights to use, copy, modify, merge, publish, 9 | ** distribute, sublicense, and/or sell copies of the Software, and to 10 | ** permit persons to whom the Software is furnished to do so, subject to 11 | ** the following conditions: 12 | ** 13 | ** The above copyright notice and this permission notice shall be included 14 | ** in all copies or substantial portions of the Software. 15 | ** 16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | */ 24 | 25 | import * as GraphQL from "graphql" 26 | import * as GraphQLTools from "graphql-tools" 27 | import GraphQLToolsTypes from "../lib/gtt.js" 28 | 29 | const definition = ` 30 | schema { 31 | query: RootQuery 32 | } 33 | scalar Void 34 | scalar MyInt 35 | scalar MyFloat 36 | scalar MyString 37 | scalar Date 38 | scalar UUID 39 | scalar JSON 40 | scalar Coord 41 | type RootQuery { 42 | exampleVoid: Void 43 | exampleMyInt(num: MyInt): Int 44 | exampleMyFloat(num: MyFloat): Float 45 | exampleMyString(str: MyString): String 46 | exampleDate(date: Date): Date 47 | exampleUUID(uuid: UUID): UUID 48 | exampleJSON(json: JSON): JSON 49 | exampleCoord(coord: Coord): Coord 50 | } 51 | ` 52 | const resolvers = { 53 | Void: GraphQLToolsTypes.Void({ name: "MyVoid" }), 54 | MyInt: GraphQLToolsTypes.Int({ name: "MyInt", min: 0, max: 100 }), 55 | MyFloat: GraphQLToolsTypes.Float({ name: "MyFloat", min: 0.0, max: 100.0 }), 56 | MyString: GraphQLToolsTypes.String({ name: "MyString", regex: /^(?:foo|bar|quux)$/ }), 57 | Date: GraphQLToolsTypes.Date({ name: "MyDate" }), 58 | UUID: GraphQLToolsTypes.UUID({ name: "MyUUID" }), 59 | JSON: GraphQLToolsTypes.JSON({ name: "MyJSON" }), 60 | Coord: GraphQLToolsTypes.JSON({ name: "Coord", struct: "{ x: number, y: number }" }), 61 | RootQuery: { 62 | exampleVoid: (root, args, ctx, info) => { 63 | return {} 64 | }, 65 | exampleMyInt: (root, args, ctx, info) => { 66 | return args.num 67 | }, 68 | exampleMyFloat: (root, args, ctx, info) => { 69 | return args.num 70 | }, 71 | exampleMyString: (root, args, ctx, info) => { 72 | return args.str 73 | }, 74 | exampleDate: (root, args, ctx, info) => { 75 | return args.date 76 | }, 77 | exampleUUID: (root, args, ctx, info) => { 78 | return args.uuid 79 | }, 80 | exampleJSON: (root, args, ctx, info) => { 81 | return args.json 82 | }, 83 | exampleCoord: (root, args, ctx, info) => { 84 | return args.coord 85 | } 86 | } 87 | } 88 | const schema = GraphQLTools.makeExecutableSchema({ 89 | typeDefs: [ definition ], 90 | resolvers: resolvers 91 | }) 92 | const query = ` 93 | query ($int: MyInt, $float: MyFloat, $string: MyString, $date: Date, $uuid: UUID, $json: JSON, $coord: Coord) { 94 | exampleVoid, 95 | int1: exampleMyInt(num: $int), 96 | int2: exampleMyInt(num: 100), 97 | float1: exampleMyFloat(num: $float), 98 | float2: exampleMyFloat(num: 42.7), 99 | string1: exampleMyString(str: $string) 100 | string2: exampleMyString(str: "foo") 101 | date1: exampleDate(date: $date), 102 | date2: exampleDate(date: "2016-08-16T00:01:02.000Z"), 103 | uuid1: exampleUUID(uuid: $uuid), 104 | uuid2: exampleUUID(uuid: "6cbe657c-63e3-11e6-aa83-080027e303e4"), 105 | json1: exampleJSON(json: $json), 106 | json2: exampleJSON(json: { foo: "bar", baz: 42, quux: true }), 107 | coord1: exampleCoord(coord: $coord), 108 | coord2: exampleCoord(coord: { x: 7, y: 42 }) 109 | } 110 | ` 111 | const variables = { 112 | int: 100, 113 | float: 42.7, 114 | string: "foo", 115 | date: "2016-08-16T00:01:02.000Z", 116 | uuid: "6cbe657c-63e3-11e6-aa83-080027e303e4", 117 | json: { foo: "bar", baz: 42, quux: true }, 118 | coord: { x: 7, y: 42 } 119 | } 120 | GraphQL.graphql(schema, query, null, null, variables).then((result) => { 121 | /* eslint no-console: off */ 122 | console.log("OK", result) 123 | }).catch((result) => { 124 | /* eslint no-console: off */ 125 | console.log("ERROR", result) 126 | }) 127 | 128 | --------------------------------------------------------------------------------