From b7579f863b2fed57bf8ad14add9b47c4e0b69815 Mon Sep 17 00:00:00 2001 From: Meg Mitchell Date: Tue, 12 Feb 2019 13:34:33 -0800 Subject: [PATCH 1/5] Add graphQl support for arbitrary JSON objects --- lib/graphQl/joiConverter.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/graphQl/joiConverter.js b/lib/graphQl/joiConverter.js index 8448e674..72eae734 100644 --- a/lib/graphQl/joiConverter.js +++ b/lib/graphQl/joiConverter.js @@ -4,6 +4,30 @@ const joiConverter = module.exports = { } const graphQl = require('graphql') const readTypes = require('./readTypes.js') +// Custom GraphQL object to handle arbitrary JSON blobs +// https://stackoverflow.com/questions/45598812/graphql-blackbox-any-type +const ObjectScalarType = new graphQl.GraphQLScalarType({ + name: 'Object', + description: 'Arbitrary object', + parseValue: (value) => { + return typeof value === 'object' ? value + : typeof value === 'string' ? JSON.parse(value) + : null + }, + serialize: (value) => { + return typeof value === 'object' ? value + : typeof value === 'string' ? JSON.parse(value) + : null + }, + parseLiteral: (ast) => { + switch (ast.kind) { + case Kind.STRING: return JSON.parse(ast.value) + case Kind.OBJECT: throw new Error(`Not sure what to do with OBJECT for ObjectScalarType`) + default: return null + } + } +}) + joiConverter.simpleAttribute = joiScheme => { let type = joiScheme._type if (type === 'any') { @@ -16,6 +40,9 @@ joiConverter.simpleAttribute = joiScheme => { if (type === 'number') { type = 'float' } + if (type === 'json') { + return ObjectScalarType; + } if (type === 'array') { if (joiScheme._inner.items.length === 1) { From 0969541238fda20805c109e3cb6032d1eb80c24e Mon Sep 17 00:00:00 2001 From: Kendra Date: Thu, 21 Feb 2019 12:02:59 -0800 Subject: [PATCH 2/5] removing referenced example --- lib/graphQl/joiConverter.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/graphQl/joiConverter.js b/lib/graphQl/joiConverter.js index 72eae734..8914df98 100644 --- a/lib/graphQl/joiConverter.js +++ b/lib/graphQl/joiConverter.js @@ -5,7 +5,6 @@ const graphQl = require('graphql') const readTypes = require('./readTypes.js') // Custom GraphQL object to handle arbitrary JSON blobs -// https://stackoverflow.com/questions/45598812/graphql-blackbox-any-type const ObjectScalarType = new graphQl.GraphQLScalarType({ name: 'Object', description: 'Arbitrary object', From cbb7c5705763b859464423937c16098b7b44744b Mon Sep 17 00:00:00 2001 From: Kendra Date: Thu, 21 Feb 2019 14:11:19 -0800 Subject: [PATCH 3/5] fixing styling errors --- lib/graphQl/joiConverter.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/graphQl/joiConverter.js b/lib/graphQl/joiConverter.js index 8914df98..2e4a48ee 100644 --- a/lib/graphQl/joiConverter.js +++ b/lib/graphQl/joiConverter.js @@ -11,12 +11,12 @@ const ObjectScalarType = new graphQl.GraphQLScalarType({ parseValue: (value) => { return typeof value === 'object' ? value : typeof value === 'string' ? JSON.parse(value) - : null + : null }, serialize: (value) => { return typeof value === 'object' ? value : typeof value === 'string' ? JSON.parse(value) - : null + : null }, parseLiteral: (ast) => { switch (ast.kind) { @@ -40,7 +40,7 @@ joiConverter.simpleAttribute = joiScheme => { type = 'float' } if (type === 'json') { - return ObjectScalarType; + return ObjectScalarType } if (type === 'array') { From a5af09e34b8377e488c52bd0fe9dac73ba8f6c72 Mon Sep 17 00:00:00 2001 From: Kendra Date: Thu, 21 Feb 2019 14:18:38 -0800 Subject: [PATCH 4/5] importing Kind --- lib/graphQl/joiConverter.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/graphQl/joiConverter.js b/lib/graphQl/joiConverter.js index 2e4a48ee..6b4c86b7 100644 --- a/lib/graphQl/joiConverter.js +++ b/lib/graphQl/joiConverter.js @@ -2,6 +2,7 @@ const joiConverter = module.exports = { } const graphQl = require('graphql') +const {Kind} = require('graphql/language') const readTypes = require('./readTypes.js') // Custom GraphQL object to handle arbitrary JSON blobs From 8d78e95f911206cf7cdaee5f4968c7e797845a52 Mon Sep 17 00:00:00 2001 From: Kendra Date: Mon, 25 Feb 2019 10:40:15 -0800 Subject: [PATCH 5/5] adding in link for Stack Overflow content --- lib/graphQl/joiConverter.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/graphQl/joiConverter.js b/lib/graphQl/joiConverter.js index 6b4c86b7..8ff3a46e 100644 --- a/lib/graphQl/joiConverter.js +++ b/lib/graphQl/joiConverter.js @@ -6,6 +6,7 @@ const {Kind} = require('graphql/language') const readTypes = require('./readTypes.js') // Custom GraphQL object to handle arbitrary JSON blobs +// https://stackoverflow.com/questions/45598812/graphql-blackbox-any-type const ObjectScalarType = new graphQl.GraphQLScalarType({ name: 'Object', description: 'Arbitrary object',