From a8c13139e1472f6317e3516065b73b92a6208b2c Mon Sep 17 00:00:00 2001 From: nikolabjelo Date: Sun, 2 Jun 2019 19:07:04 +0200 Subject: [PATCH] temporary including json object type directly in the code (TOFIX) --- package-lock.json | 5 -- package.json | 1 - src/schema/types/GraphQLJSONObject.js | 83 +++++++++++++++++++++++++ src/schema/types/TradingSystem.js | 2 +- src/schema/types/input/TradingSystem.js | 2 +- 5 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 src/schema/types/GraphQLJSONObject.js diff --git a/package-lock.json b/package-lock.json index db1d1bca1..80cc95a42 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3587,11 +3587,6 @@ "uuid": "^3.1.0" } }, - "graphql-type-json": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/graphql-type-json/-/graphql-type-json-0.3.0.tgz", - "integrity": "sha512-lnxg5HiB95yxy+/5cDKtP6pZo0zgntsOmqsjeCBXFGJ4YoMF3+1YaSEKWJntNTu+VsAm3zf6lPxFpp1kxzofLA==" - }, "graphql-upload": { "version": "8.0.6", "resolved": "https://registry.npmjs.org/graphql-upload/-/graphql-upload-8.0.6.tgz", diff --git a/package.json b/package.json index c139ff155..667c39594 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ "dotenv": "^6.0.0", "express": "^4.16.3", "graphql": "^0.13.2", - "graphql-type-json": "^0.3.0", "mongoose": "^5.3.4" } } diff --git a/src/schema/types/GraphQLJSONObject.js b/src/schema/types/GraphQLJSONObject.js new file mode 100644 index 000000000..4c4b52774 --- /dev/null +++ b/src/schema/types/GraphQLJSONObject.js @@ -0,0 +1,83 @@ +/* eslint-disable no-void */ +exports.__esModule = true; +exports.GraphQLJSONObject = void 0; +exports.default = void 0; +exports.GraphQLJSON = void 0; + +const _graphql = require('graphql'); + +const _language = require('graphql/language'); + +function identity(value) { + return value; +} + +function ensureObject(value) { + if (typeof value !== 'object' || value === null || Array.isArray(value)) { + throw new TypeError(`JSONObject cannot represent non-object value: ${value}`); + } + + return value; +} + +function parseObject(ast, variables) { + const value = Object.create(null); + ast.fields.forEach((field) => { + // eslint-disable-next-line no-use-before-define + value[field.name.value] = parseLiteral(field.value, variables); + }); + return value; +} + +function parseLiteral(ast, variables) { + switch (ast.kind) { + case _language.Kind.STRING: + case _language.Kind.BOOLEAN: + return ast.value; + + case _language.Kind.INT: + case _language.Kind.FLOAT: + return parseFloat(ast.value); + + case _language.Kind.OBJECT: + return parseObject(ast, variables); + + case _language.Kind.LIST: + return ast.values.map(n => parseLiteral(n, variables)); + + case _language.Kind.NULL: + return null; + + case _language.Kind.VARIABLE: + { + const name = ast.name.value; + return variables ? variables[name] : undefined; + } + + default: + return undefined; + } +} // This named export is intended for users of CommonJS. Users of ES modules +// should instead use the default export. + + +const GraphQLJSON = new _graphql.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, +}); +exports.GraphQLJSON = GraphQLJSON; +const _default = GraphQLJSON; +exports.default = _default; +const GraphQLJSONObject = new _graphql.GraphQLScalarType({ + name: 'JSONObject', + description: 'The `JSONObject` scalar type represents JSON objects as specified by' + + ' [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).', + serialize: ensureObject, + parseValue: ensureObject, + parseLiteral: parseObject, +}); +exports.GraphQLJSONObject = GraphQLJSONObject; diff --git a/src/schema/types/TradingSystem.js b/src/schema/types/TradingSystem.js index 08d1596bc..3cc5584e6 100644 --- a/src/schema/types/TradingSystem.js +++ b/src/schema/types/TradingSystem.js @@ -3,7 +3,7 @@ import { GraphQLString, GraphQLID, } from 'graphql'; -import GraphQLJSON from 'graphql-type-json'; +import GraphQLJSON from './GraphQLJSONObject'; const Type = new GraphQLObjectType({ name: 'TradingSystem', diff --git a/src/schema/types/input/TradingSystem.js b/src/schema/types/input/TradingSystem.js index 0ac42e74c..4dc4bd5f1 100644 --- a/src/schema/types/input/TradingSystem.js +++ b/src/schema/types/input/TradingSystem.js @@ -1,7 +1,7 @@ import { GraphQLInputObjectType, } from 'graphql'; -import GraphQLJSON from 'graphql-type-json'; +import GraphQLJSON from '../GraphQLJSONObject'; const Type = new GraphQLInputObjectType({ name: 'TradingSystemInput',