Skip to content

Commit

Permalink
🎉 reinit
Browse files Browse the repository at this point in the history
  • Loading branch information
up9cloud committed Feb 27, 2019
0 parents commit 6fd16e7
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": ["node8"]
}
8 changes: 8 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,8 @@
module.exports = {
root: true,
env: {
node: true,
mocha: true
},
extends: 'standard'
}
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
/node_modules/
/dist/
/.nyc_output/
6 changes: 6 additions & 0 deletions .npmignore
@@ -0,0 +1,6 @@
/src/
/test/
.babelrc
.eslintrc.js
.gitignore
.travis.yml
17 changes: 17 additions & 0 deletions .travis.yml
@@ -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
21 changes: 21 additions & 0 deletions LICENSE
@@ -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.
29 changes: 29 additions & 0 deletions README.md
@@ -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
```
48 changes: 48 additions & 0 deletions package.json
@@ -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"
}
}
41 changes: 41 additions & 0 deletions src/index.js
@@ -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])
}
})
104 changes: 104 additions & 0 deletions test/index.js
@@ -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')
}
})
})
})

0 comments on commit 6fd16e7

Please sign in to comment.