Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON type support #428

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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