Skip to content

Commit

Permalink
feat: switch own JSON type implementation on improved `graphql-type-j…
Browse files Browse the repository at this point in the history
…son` package, due its popularity and last [amazing changes](taion/graphql-type-json#31) by @benjie
  • Loading branch information
nodkz committed Apr 25, 2018
1 parent 78c9000 commit e93379e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
},
"dependencies": {
"babel-runtime": "^6.26.0",
"graphql-type-json": "^0.2.1",
"object-path": "^0.11.4"
},
"config": {
Expand Down
84 changes: 43 additions & 41 deletions src/type/json.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
/* @flow strict */

// copied from https://github.com/taion/graphql-type-json
import GraphQLJSON from 'graphql-type-json';

import { GraphQLScalarType, Kind } from '../graphql';
export default GraphQLJSON;

function identity(value) {
return value;
}

function parseLiteral(ast) {
switch (ast.kind) {
case Kind.STRING:
case Kind.BOOLEAN:
return ast.value;
case Kind.INT:
case Kind.FLOAT:
return parseFloat(ast.value);
case Kind.OBJECT: {
const value = Object.create(null);
ast.fields.forEach(field => {
value[field.name.value] = parseLiteral(field.value);
});

return value;
}
case Kind.LIST:
return ast.values.map(parseLiteral);
case Kind.NULL:
return null;
default:
return undefined;
}
}

export default new GraphQLScalarType({
name: 'JSON',
description:
'The `JSON` scalar type represents JSON values as specified by ' +
'[ECMA-404](http://www.ecma-international.org/' +
'publications/files/ECMA-ST/ECMA-404.pdf).',
serialize: identity,
parseValue: identity,
parseLiteral,
});
// import { GraphQLScalarType, Kind } from '../graphql';
//
// function identity(value) {
// return value;
// }
//
// function parseLiteral(ast) {
// switch (ast.kind) {
// case Kind.STRING:
// case Kind.BOOLEAN:
// return ast.value;
// case Kind.INT:
// case Kind.FLOAT:
// return parseFloat(ast.value);
// case Kind.OBJECT: {
// const value = Object.create(null);
// ast.fields.forEach(field => {
// value[field.name.value] = parseLiteral(field.value);
// });
//
// return value;
// }
// case Kind.LIST:
// return ast.values.map(parseLiteral);
// case Kind.NULL:
// return null;
// default:
// return undefined;
// }
// }
//
// export default new GraphQLScalarType({
// name: 'JSON',
// description:
// 'The `JSON` scalar type represents JSON values as specified by ' +
// '[ECMA-404](http://www.ecma-international.org/' +
// 'publications/files/ECMA-ST/ECMA-404.pdf).',
// serialize: identity,
// parseValue: identity,
// parseLiteral,
// });
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,10 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"

graphql-type-json@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/graphql-type-json/-/graphql-type-json-0.2.1.tgz#d2c177e2f1b17d87f81072cd05311c0754baa420"

graphql@0.13.2:
version "0.13.2"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.13.2.tgz#4c740ae3c222823e7004096f832e7b93b2108270"
Expand Down

1 comment on commit e93379e

@benjie
Copy link

@benjie benjie commented on e93379e Apr 25, 2018

Choose a reason for hiding this comment

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

🙇

Please sign in to comment.