diff --git a/lib/graphQl/joiConverter.js b/lib/graphQl/joiConverter.js index 8448e674..8ff3a46e 100644 --- a/lib/graphQl/joiConverter.js +++ b/lib/graphQl/joiConverter.js @@ -2,8 +2,33 @@ 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 +// 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 +41,9 @@ joiConverter.simpleAttribute = joiScheme => { if (type === 'number') { type = 'float' } + if (type === 'json') { + return ObjectScalarType + } if (type === 'array') { if (joiScheme._inner.items.length === 1) {