Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6fd16e7
Showing
10 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["node8"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
root: true, | ||
env: { | ||
node: true, | ||
mocha: true | ||
}, | ||
extends: 'standard' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules/ | ||
/dist/ | ||
/.nyc_output/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/src/ | ||
/test/ | ||
.babelrc | ||
.eslintrc.js | ||
.gitignore | ||
.travis.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: node_js | ||
node_js: | ||
- stable | ||
- '8' | ||
after_success: | ||
- npm run coverage | ||
- npm run build | ||
deploy: | ||
provider: npm | ||
email: "$NPM_EMAIL" | ||
api_key: | ||
secure: bRkZXQdiBiKZgEdBHqPBlRCPra1taWTEiJleD9LSFIspcfMOLugwDdpWV5cxWdFdbzpLe+HUZI0O1bhXz5WcBoanSVdyg2ytZPvL5xUtvYYUYs005pj/EzEoz91at0Fjj5VRPPa7+TPCg8ePLFHMvnkopLzvSxTOI6geKs759HIpoaksPhwsEDT6gBgNkic47H/3nLQ6rRfDtBxU3lHgjJPh/BxwwamguQCwKsrSOTYPGRmMgGHv98Sqw/eQvZ34hUSMfbFq0bGX/IpjbMrHJI9jKvnTlU0y6G8iB4uquNCDxDL7PN5QTTzw2iPHzeVjGjDo0wmZZMECcZYlJhkPL+J2JX6jOPAd/SK5xkk8v/JNtXGiQuJHGrSXGtA/YOd8WtWJ3Aam57VX/uWEhvcbVXHNkao5mKvinxwMag/MqKUoy/9wP49hOhNU8JToCSTNj7vfrO5gdZ2Ma4LONZmR2WjKdhVG0TUxtA1ERGocXW8HiU4Q6s/txHEkZYB1VG+SyxULtGf/JmoizcJWE+9SVrFAirs+OjOolf69P4FQWw2QgYU4J8s1qhYc6F7PLK3kvmt5byRcwtwxMsCiMutWLbM7Yk7F7P20k49fyMbOpzJWEnfIQ3XO/UuqsTFURA6omZtARW4bfI43F64Fj+0gDXJDmnDoiDpo2L8hZtHNkU0= | ||
on: | ||
tags: true | ||
repo: up9cloud/graphql-tools-type-uint | ||
branch: master | ||
skip_cleanup: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 啊不就好棒棒 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# graphql-tools-type-uint | ||
|
||
[![Build Status](https://travis-ci.org/up9cloud/graphql-tools-type-uint.svg?branch=master)](https://travis-ci.org/up9cloud/graphql-tools-type-uint) | ||
[![Coverage Status](https://coveralls.io/repos/github/up9cloud/graphql-tools-type-uint/badge.svg?branch=master)](https://coveralls.io/github/up9cloud/graphql-tools-type-uint?branch=master) | ||
|
||
UInt (unsigned int) scalar type for [graphql-tools](https://github.com/apollographql/graphql-tools) | ||
|
||
## Usage | ||
|
||
```js | ||
import { makeExecutableSchema } from 'graphql-tools' | ||
import UInt from 'graphql-tools-type-uint' | ||
|
||
let typeDefs = [` | ||
scalar UInt | ||
type Query { | ||
value(v: UInt): UInt | ||
}` | ||
] | ||
let resolvers = { | ||
UInt, | ||
Query: { | ||
value: (root, { v }) => v | ||
} | ||
} | ||
let schema = makeExecutableSchema({ typeDefs, resolvers }) | ||
|
||
export default schema | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"name": "graphql-tools-type-uint", | ||
"version": "2.0.2", | ||
"description": "UInt (unsigned int) scalar type for [graphql-tools](https://github.com/apollographql/graphql-tools)", | ||
"main": "dist/index.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "npm run lint && nyc mocha --require babel-register test", | ||
"lint": "eslint src test", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"build": "babel src -d dist" | ||
}, | ||
"repository": "git@github.com:up9cloud/graphql-tools-type-uint.git", | ||
"author": "up9cloud (https://github.com/up9cloud)", | ||
"license": "MIT", | ||
"keywords": [ | ||
"graphql", | ||
"graphql-tools", | ||
"type", | ||
"scalar", | ||
"int", | ||
"unsigned" | ||
], | ||
"peerDependencies": { | ||
"graphql": "^14.0.2" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-node8": "^1.2.0", | ||
"chai": "^4.2.0", | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.9.0", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-node": "^8.0.0", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"graphql": "^14.0.2", | ||
"graphql-tools": "^4.0.3", | ||
"mocha": "^5.2.0", | ||
"nyc": "^13.1.0" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { GraphQLScalarType } from 'graphql' | ||
import { GraphQLError } from 'graphql/error' | ||
|
||
const MIN = 0 | ||
const MAX = Math.pow(2, 32) - 1 | ||
|
||
export default new GraphQLScalarType({ | ||
name: 'UInt', | ||
description: `UInt (unsigned int between ${MIN} and ${MAX}) scalar type for [graphql-tools](https://github.com/apollographql/graphql-tools)`, | ||
serialize (value) { | ||
if ( | ||
Number.isInteger(value) && | ||
value >= MIN && | ||
value <= MAX | ||
) { | ||
return value | ||
} | ||
return null | ||
}, | ||
parseValue (value) { | ||
if ( | ||
Number.isInteger(value) && | ||
value >= MIN && | ||
value <= MAX | ||
) { | ||
return value | ||
} | ||
throw new GraphQLError('', []) | ||
}, | ||
parseLiteral (ast) { | ||
const value = Number(ast.value) | ||
if ( | ||
Number.isInteger(value) && | ||
value >= MIN && | ||
value <= MAX | ||
) { | ||
return value | ||
} | ||
throw new GraphQLError(`Invalid UInt literal.\n${ast.value} is not UInt.`, [ast]) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { expect } from 'chai' | ||
import { graphql } from 'graphql' | ||
import { makeExecutableSchema } from 'graphql-tools' | ||
|
||
import UInt from '../src' | ||
|
||
const whiteList = [ | ||
0, | ||
1.0, | ||
Math.pow(2, 32) - 1 | ||
] | ||
const blackList = [ | ||
-1, | ||
0.1, | ||
Math.pow(2, 32), | ||
'foo', | ||
[], | ||
{} | ||
] | ||
|
||
let typeDefs = [` | ||
scalar UInt | ||
type Query { | ||
value(v: UInt): UInt | ||
}` | ||
] | ||
let resolvers = { | ||
UInt, | ||
Query: { | ||
value: (root, { v }) => v | ||
} | ||
} | ||
let schema = makeExecutableSchema({ typeDefs, resolvers }) | ||
|
||
describe('UInt', () => { | ||
describe('serialize', () => { | ||
it('valid value should pass', () => { | ||
for (let v of whiteList) { | ||
expect(UInt.serialize(v)).to.be.equal(v) | ||
} | ||
}) | ||
it('invalid value should return null', () => { | ||
for (let v of blackList) { | ||
expect(UInt.serialize(v)).to.be.equal(null) | ||
} | ||
}) | ||
}) | ||
|
||
describe('parseValue', () => { | ||
it('valid value should pass', async () => { | ||
for (let v of whiteList) { | ||
let { data } = await graphql( | ||
schema, | ||
`query ($v: UInt) { | ||
value(v: $v) | ||
}`, | ||
null, | ||
null, | ||
{ v } | ||
) | ||
expect(data.value).to.be.equal(v) | ||
} | ||
}) | ||
it('invalid value should return errors', async () => { | ||
for (let v of blackList) { | ||
let data = await graphql( | ||
schema, | ||
`query ($v: UInt) { | ||
value(v: $v) | ||
}`, | ||
null, | ||
null, | ||
{ v } | ||
) | ||
expect(data.data).to.be.equal(undefined) | ||
// expect(data.data.value).to.be.equal(undefined) | ||
expect(data.errors).to.be.an('array') | ||
} | ||
}) | ||
}) | ||
|
||
describe('parseLiteral', () => { | ||
it('valid value should pass', async () => { | ||
for (let v of whiteList) { | ||
let { data } = await graphql( | ||
schema, | ||
`query { value(v: ${isNaN(v) ? JSON.stringify(v) : v}) }` | ||
) | ||
expect(data.value).to.be.equal(v) | ||
} | ||
}) | ||
it('invalid value should return errors', async () => { | ||
for (let v of blackList) { | ||
let data = await graphql( | ||
schema, | ||
`query { value(v: ${isNaN(v) ? JSON.stringify(v) : v}) }` | ||
) | ||
expect(data.data).to.be.equal(undefined) | ||
// expect(data.data.value).to.be.equal(undefined) | ||
expect(data.errors).to.be.an('array') | ||
} | ||
}) | ||
}) | ||
}) |