-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
11 changed files
with
3,948 additions
and
2,174 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 |
---|---|---|
@@ -1,3 +1,12 @@ | ||
{ | ||
"plugins": ["transform-es2015-modules-commonjs"] | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"node": "current" | ||
} | ||
} | ||
] | ||
] | ||
} |
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 |
---|---|---|
@@ -1,14 +1,59 @@ | ||
version: 2 | ||
version: 2.1 | ||
|
||
references: | ||
node_image: &node_image | ||
circleci/node:10 | ||
working_directory: &working_directory | ||
~/repo | ||
dependencies_cache_key: &dependencies_cache_key | ||
v1-dependencies-{{ checksum "package.json" }} | ||
|
||
jobs: | ||
build: | ||
working_directory: ~/graphql-date-type | ||
install_dependencies: | ||
install_dependencies: | ||
docker: | ||
- image: circleci/node:8.1.2 | ||
- image: *node_image | ||
working_directory: *working_directory | ||
steps: | ||
- checkout | ||
- attach_workspace: | ||
at: *working_directory | ||
- restore_cache: | ||
key: *dependencies_cache_key | ||
- run: | ||
name: Install dependencies | ||
command: yarn | ||
name: Install node_modules | ||
command: yarn install --ignore-engines | ||
- save_cache: | ||
key: *dependencies_cache_key | ||
paths: | ||
- node_modules | ||
- persist_to_workspace: | ||
root: . | ||
paths: node_modules | ||
|
||
lint_build_test: | ||
docker: | ||
- image: *node_image | ||
working_directory: *working_directory | ||
steps: | ||
- checkout | ||
- attach_workspace: | ||
at: *working_directory | ||
- run: | ||
name: Lint | ||
command: yarn lint | ||
- run: | ||
name: Build | ||
command: yarn build:clean && yarn build | ||
- run: | ||
name: Test | ||
command: yarn test | ||
|
||
workflows: | ||
version: 2 | ||
build_test: | ||
jobs: | ||
- install_dependencies | ||
- lint_build_test: | ||
requires: | ||
- install_dependencies |
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,37 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es6": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"globals": { | ||
"Atomics": "readonly", | ||
"SharedArrayBuffer": "readonly" | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 2018, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"comma-dangle": [ | ||
"error", | ||
"always-multiline" | ||
], | ||
"indent": [ | ||
"error", | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
] | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,38 +1,36 @@ | ||
{ | ||
"name": "graphql-date-type", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"main": "dist/index.js", | ||
"author": "Natthakit Susanthitanon <ns@storylog.co>", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "babel src -d dist --plugins transform-es2015-modules-commonjs", | ||
"build:clean": "rm -rf dist", | ||
"build": "babel src -d dist --ignore '**/*.spec.js'", | ||
"lint": "eslint .", | ||
"install": "yarn run build", | ||
"pretest": "yarn run lint", | ||
"test": "jest --coverage --forceExit --runInBand", | ||
"test": "jest", | ||
"test:watch": "jest --watch" | ||
}, | ||
"dependencies": { | ||
"graphql": "^14.5.4" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.5.5", | ||
"@babel/core": "^7.5.5", | ||
"@babel/preset-env": "^7.5.5", | ||
"babel-jest": "^24.9.0", | ||
"eslint": "^6.3.0", | ||
"jest": "^24.9.0", | ||
"jest-cli": "^24.9.0", | ||
"jest-matcher-utils": "^24.9.0" | ||
}, | ||
"jest": { | ||
"testEnvironment": "node", | ||
"coverageReporters": [ | ||
"html" | ||
], | ||
"roots": [ | ||
"<rootDir>/tests" | ||
"<rootDir>/src" | ||
], | ||
"testMatch": [ | ||
"**/?(*.)(spec|test).js?(x)" | ||
"**/*.spec.js" | ||
] | ||
}, | ||
"dependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.24.1", | ||
"graphql": "^0.10.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "3.19.0", | ||
"eslint-config-storylog": "https://github.com/storylog/eslint-config-storylog.git", | ||
"jest": "^20.0.4", | ||
"jest-cli": "^20.0.4" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,38 +1,37 @@ | ||
import { GraphQLScalarType } from 'graphql'; | ||
import { Kind } from 'graphql/language'; | ||
import { GraphQLScalarType, Kind } from 'graphql'; | ||
|
||
function errorMessage(value) { | ||
return `Cannot represent an invalid Date instance ${JSON.stringify(value)}`; | ||
} | ||
|
||
function parseDate(value) { | ||
const date = new Date(value); | ||
|
||
if (isNaN(date)) { | ||
throw new TypeError(errorMessage(value)); | ||
} | ||
|
||
return date; | ||
} | ||
|
||
export default new GraphQLScalarType({ | ||
name: 'Date', | ||
description: 'Date custom scalar type', | ||
parseValue(value) { | ||
description: 'Date string in ISO format', | ||
serialize(value) { | ||
try { | ||
return (new Date(value)).toISOString(); | ||
} catch (error) { | ||
throw new TypeError( | ||
`Cannot represent an invalid Date instance ${JSON.stringify(value)}` | ||
); | ||
throw new TypeError(errorMessage(value)); | ||
} | ||
}, | ||
parseLiteral(ast) { | ||
try { | ||
if (ast.kind === _language.Kind.INT) { | ||
return (new Date(parseInt(ast.value, 10))).toISOString(); | ||
} | ||
return null; | ||
} catch (error) { | ||
throw new TypeError( | ||
`Cannot represent an invalid Date instance ${JSON.stringify(ast)}` | ||
); | ||
} | ||
parseValue(value) { | ||
return parseDate(value); | ||
}, | ||
serialize(value) { | ||
try { | ||
if (!value) return value; | ||
return (new Date(value)).toISOString(); | ||
} catch (error) { | ||
throw new TypeError( | ||
`Cannot represent an invalid Date instance ${JSON.stringify(value)}` | ||
); | ||
parseLiteral(ast) { | ||
if (ast.kind === Kind.STRING) { | ||
return parseDate(ast.value); | ||
} | ||
|
||
throw new TypeError(errorMessage(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,75 @@ | ||
/* eslint-env jest */ | ||
|
||
import { Kind } from 'graphql'; | ||
import { stringify } from 'jest-matcher-utils'; | ||
import GraphQLDate from './index'; | ||
|
||
describe('GraphQLDate', () => { | ||
describe('serialize', () => { | ||
[ | ||
[new Date(Date.UTC(2019, 0, 1)), '2019-01-01T00:00:00.000Z'], | ||
[new Date(Date.UTC(2019, 0, 1, 11, 22, 33, 444)), '2019-01-01T11:22:33.444Z'], | ||
].forEach(([value, expected]) => { | ||
it(`serialize Date ${stringify(value)} to ${stringify(expected)}`, () => { | ||
expect(GraphQLDate.serialize(value)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
it('throw error when serializing invalid date', () => { | ||
expect(() => { | ||
GraphQLDate.serialize(new Date('invalid date')); | ||
}).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('parseValue', () => { | ||
[ | ||
['2019-01-01T00:00:00Z', new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))], | ||
['2019-01-01T00:00:00.000Z', new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))], | ||
['2019-01-01T11:22:33.444Z', new Date(Date.UTC(2019, 0, 1, 11, 22, 33, 444))], | ||
].forEach(([value, expected]) => { | ||
it(`parse ${stringify(value)} to Date ${stringify(expected)}`, () => { | ||
expect(GraphQLDate.parseValue(value)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
it('throw error when parsing invalid date string', () => { | ||
expect(() => { | ||
GraphQLDate.parseValue('invalid date'); | ||
}).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('parseLiteral', () => { | ||
[ | ||
['2019-01-01T00:00:00Z', new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))], | ||
['2019-01-01T00:00:00.000Z', new Date(Date.UTC(2019, 0, 1, 0, 0, 0, 0))], | ||
['2019-01-01T11:22:33.444Z', new Date(Date.UTC(2019, 0, 1, 11, 22, 33, 444))], | ||
].forEach(([value, expected]) => { | ||
const literal = { | ||
kind: Kind.STRING, | ||
value, | ||
}; | ||
|
||
it(`parse literal ${stringify(literal)} to Date ${stringify(expected)}`, () => { | ||
expect(GraphQLDate.parseLiteral(literal)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
[ | ||
[Kind.STRING, 'invalid date'], | ||
[Kind.INT, 1], | ||
].forEach(([kind, value]) => { | ||
const literal = { | ||
kind, | ||
value, | ||
}; | ||
|
||
it(`throw error when parsing invalid literal ${stringify(literal)}`, () => { | ||
expect(() => { | ||
GraphQLDate.parseLiteral(literal); | ||
}).toThrowError(); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.