Skip to content
Merged
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.2.0] - 2018-01-16
### Changed
- Implemented more strict numeric type checking
- Some type exception messages changed slightly
- Changed `graphql` from a dependency to a _peer_ dependency

### Added
- NegativeInt
- NegativeFloat
- NonPositiveInt
- NonPositiveFloat
- more tests

## [0.1.0] - 2017-07-14
### Added
- Initial Release - released as [`@okgrow/graphql-scalars`](https://www.npmjs.com/package/@okgrow/graphql-scalars) on npm.
40 changes: 34 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ In your schema:
```graphql
scalar DateTime

scalar NonPositiveInt
scalar PositiveInt
scalar NonNegativeInt
scalar NegativeInt

scalar NonPositiveFloat
scalar PositiveFloat
scalar NonNegativeFloat
scalar NegativeFloat

scalar EmailAddress
scalar URL
Expand All @@ -31,9 +36,17 @@ In your resolver map, first import them:
```js
import {
DateTime,

NonPositiveInt,
PositiveInt,
NonNegativeInt,
NegativeInt,

NonPositiveFloat,
PositiveFloat,
NonNegativeFloat,
NegativeFloat,

EmailAddress,
URL,
} from '@okgrow/graphql-scalars';
Expand All @@ -45,10 +58,15 @@ Then make sure they're in the root resolver map like this:
const myResolverMap = {
DateTime,

NonPositiveInt,
PositiveInt,
NonNegativeInt,
NegativeInt,

NonPositiveFloat,
PositiveFloat,
NonNegativeFloat,
NegativeFloat,

EmailAddress,
URL,
Expand Down Expand Up @@ -134,15 +152,27 @@ inevitable parsing or conversion themselves.
### NonNegativeInt
Integers that will have a value of 0 or more. Uses [`parseInt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt).

### NonPositiveInt
Integers that will have a value of 0 or less. Uses [`parseInt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt).

### PositiveInt
Integers that will have a value greater than 0. Uses [`parseInt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt).

### NegativeInt
Integers that will have a value less than 0. Uses [`parseInt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt).

### NonNegativeFloat
Floats that will have a value of 0 or more. Uses [`parseFloat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat).

### NonPositiveFloat
Floats that will have a value of 0 or less. Uses [`parseFloat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat).

### PositiveFloat
Floats that will have a value greater than 0. Uses [`parseFloat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat).

### NegativeFloat
Floats that will have a value less than 0. Uses [`parseFloat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat).

### EmailAddress
A field whose value conforms to the standard internet email address format as specified in
[RFC822](https://www.w3.org/Protocols/rfc822/).
Expand All @@ -157,15 +187,11 @@ We'd like to keep growing this package, within reason, to include the scalar typ
required when defining GraphQL schemas. We welcome both suggestions and pull requests. A couple of
ideas we're considering are:

- NegativeInt
- NegativeFloat

These are easy to add, we just haven't run into cases for them yet.

- PhoneNumber
- PostalCode
- BLOB

These both have challenges in terms of making them globally useful so they need a bit of thought.
These all have challenges in terms of making them globally useful so they need a bit of thought.

For `PhoneNumber` we can probably just use the [E.164 specification](https://en.wikipedia.org/wiki/E.164)
which is simply `+17895551234`. The very powerful
Expand All @@ -176,6 +202,8 @@ parse user input and _get_ the E.164 format to pass _into_ a schema.
Postal codes are [a bit more involved](https://en.wikipedia.org/wiki/List_of_postal_codes). But,
again, it's probably just a really long regex.

BLOBs could be a base64-encoded object of some kind.

## What's this all about?
GraphQL is a wonderful new approach to application data and API layers that's gaining momentum. If
you have not heard of it, start [here](http://graphql.org/learn/) and check out
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@okgrow/graphql-scalars",
"version": "0.1.0",
"version": "0.2.0",
"description": "A collection of scalar types not included in base GraphQL.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -31,10 +31,11 @@
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^3.0.0",
"eslint-plugin-react": "^6.10.3",
"graphql": "^0.10.1",
"jest": "^20.0.4",
"nodemon": "1.11.x"
},
"dependencies": {
"peerDependencies": {
"graphql": "^0.10.1"
Copy link
Contributor

@cfnelson cfnelson Feb 7, 2018

Choose a reason for hiding this comment

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

@ccuilla You will want to also add graphql as a devDependency in addition to having it as a peerDependency to allow for a simple quick install if someone clones your repo directly and wishes to run tests etc.

}
}
27 changes: 27 additions & 0 deletions src/NegativeFloat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GraphQLScalarType } from 'graphql';
import { GraphQLError } from 'graphql/error';
import { Kind } from 'graphql/language';

import { processValue, VALIDATIONS } from './utilities';

export default new GraphQLScalarType({
name: 'NegativeFloat',

description: 'Floats that will have a value less than 0.',

serialize(value) {
return processValue(value, VALIDATIONS.NegativeFloat);
},

parseValue(value) {
return processValue(value, VALIDATIONS.NegativeFloat);
},

parseLiteral(ast) {
if (ast.kind !== Kind.FLOAT) {
throw new GraphQLError(`Can only validate floating point numbers as negative floating point numbers but got a: ${ast.kind}`); // eslint-disable-line max-len
}

return processValue(ast.value, VALIDATIONS.NegativeFloat);
},
});
27 changes: 27 additions & 0 deletions src/NegativeInt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GraphQLScalarType } from 'graphql';
import { GraphQLError } from 'graphql/error';
import { Kind } from 'graphql/language';

import { processValue, VALIDATIONS } from './utilities';

export default new GraphQLScalarType({
name: 'NegativeInt',

description: 'Integers that will have a value less than 0.',

serialize(value) {
return processValue(value, VALIDATIONS.NegativeInt);
},

parseValue(value) {
return processValue(value, VALIDATIONS.NegativeInt);
},

parseLiteral(ast) {
if (ast.kind !== Kind.INT) {
throw new GraphQLError(`Can only validate integers as negative integers but got a: ${ast.kind}`); // eslint-disable-line max-len
}

return processValue(ast.value, VALIDATIONS.NegativeInt);
},
});
18 changes: 4 additions & 14 deletions src/NonNegativeFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,26 @@ import { GraphQLScalarType } from 'graphql';
import { GraphQLError } from 'graphql/error';
import { Kind } from 'graphql/language';

function processValue(value) {
if (isNaN(value)) {
throw new TypeError(`Value is not a number: ${value}`);
}

if (value < 0) {
throw new TypeError(`Value is a negative number: ${value}`);
}

return parseFloat(value);
}
import { processValue, VALIDATIONS } from './utilities';

export default new GraphQLScalarType({
name: 'NonNegativeFloat',

description: 'Floats that will have a value of 0 or more.',

serialize(value) {
return processValue(value);
return processValue(value, VALIDATIONS.NonNegativeFloat);
},

parseValue(value) {
return processValue(value);
return processValue(value, VALIDATIONS.NonNegativeFloat);
},

parseLiteral(ast) {
if (ast.kind !== Kind.FLOAT) {
throw new GraphQLError(`Can only validate floating point numbers as non-negative floating point numbers but got a: ${ast.kind}`); // eslint-disable-line max-len
}

return processValue(ast.value);
return processValue(ast.value, VALIDATIONS.NonNegativeFloat);
},
});
18 changes: 4 additions & 14 deletions src/NonNegativeInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,26 @@ import { GraphQLScalarType } from 'graphql';
import { GraphQLError } from 'graphql/error';
import { Kind } from 'graphql/language';

function processValue(value) {
if (isNaN(value)) {
throw new TypeError(`Value is not a number: ${value}`);
}

if (value < 0) {
throw new TypeError(`Value is a negative number: ${value}`);
}

return parseInt(value, 10);
}
import { processValue, VALIDATIONS } from './utilities';

export default new GraphQLScalarType({
name: 'NonNegativeInt',

description: 'Integers that will have a value of 0 or more.',

serialize(value) {
return processValue(value);
return processValue(value, VALIDATIONS.NonNegativeInt);
},

parseValue(value) {
return processValue(value);
return processValue(value, VALIDATIONS.NonNegativeInt);
},

parseLiteral(ast) {
if (ast.kind !== Kind.INT) {
throw new GraphQLError(`Can only validate integers as non-negative integers but got a: ${ast.kind}`); // eslint-disable-line max-len
}

return processValue(ast.value);
return processValue(ast.value, VALIDATIONS.NonNegativeInt);
},
});
27 changes: 27 additions & 0 deletions src/NonPositiveFloat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GraphQLScalarType } from 'graphql';
import { GraphQLError } from 'graphql/error';
import { Kind } from 'graphql/language';

import { processValue, VALIDATIONS } from './utilities';

export default new GraphQLScalarType({
name: 'NonPositiveFloat',

description: 'Floats that will have a value of 0 or less.',

serialize(value) {
return processValue(value, VALIDATIONS.NonPositiveFloat);
},

parseValue(value) {
return processValue(value, VALIDATIONS.NonPositiveFloat);
},

parseLiteral(ast) {
if (ast.kind !== Kind.FLOAT) {
throw new GraphQLError(`Can only validate floating point numbers as non-positive floating point numbers but got a: ${ast.kind}`); // eslint-disable-line max-len
}

return processValue(ast.value, VALIDATIONS.NonPositiveFloat);
},
});
27 changes: 27 additions & 0 deletions src/NonPositiveInt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GraphQLScalarType } from 'graphql';
import { GraphQLError } from 'graphql/error';
import { Kind } from 'graphql/language';

import { processValue, VALIDATIONS } from './utilities';

export default new GraphQLScalarType({
name: 'NonPositiveInt',

description: 'Integers that will have a value of 0 or less.',

serialize(value) {
return processValue(value, VALIDATIONS.NonPositiveInt);
},

parseValue(value) {
return processValue(value, VALIDATIONS.NonPositiveInt);
},

parseLiteral(ast) {
if (ast.kind !== Kind.INT) {
throw new GraphQLError(`Can only validate integers as non-positive integers but got a: ${ast.kind}`); // eslint-disable-line max-len
}

return processValue(ast.value, VALIDATIONS.NonPositiveInt);
},
});
18 changes: 4 additions & 14 deletions src/PositiveFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,26 @@ import { GraphQLScalarType } from 'graphql';
import { GraphQLError } from 'graphql/error';
import { Kind } from 'graphql/language';

function processValue(value) {
if (isNaN(value)) {
throw new TypeError(`Value is not a number: ${value}`);
}

if (!(value > 0)) {
throw new TypeError(`Value is not a positive number: ${value}`);
}

return parseFloat(value);
}
import { processValue, VALIDATIONS } from './utilities';

export default new GraphQLScalarType({
name: 'PositiveFloat',

description: 'Floats that will have a value greater than 0.',

serialize(value) {
return processValue(value);
return processValue(value, VALIDATIONS.PositiveFloat);
},

parseValue(value) {
return processValue(value);
return processValue(value, VALIDATIONS.PositiveFloat);
},

parseLiteral(ast) {
if (ast.kind !== Kind.FLOAT) {
throw new GraphQLError(`Can only validate floating point numbers as positive floating point numbers but got a: ${ast.kind}`); // eslint-disable-line max-len
}

return processValue(ast.value);
return processValue(ast.value, VALIDATIONS.PositiveFloat);
},
});
Loading