Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/graphQl/joiConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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) {
Expand Down