Skip to content
This repository has been archived by the owner on Jun 20, 2022. It is now read-only.

Commit

Permalink
fix: json graphql type warning fix
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangoacher committed Mar 5, 2020
1 parent ac91546 commit 80fe749
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
40 changes: 40 additions & 0 deletions packages/trail-fastify-graphql-plugin/lib/graphql-json-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const { GraphQLScalarType } = require('graphql')
const { Kind } = require('graphql/language')

function parseObject (ast) {
return ast.fields.reduce((obj, field) => {
obj[field.name.value] = parseLiteral(field.value)
return obj
}, Object.create(null))
}

function parseLiteral (ast) {
switch (ast.kind) {
case Kind.STRING:
case Kind.BOOLEAN:
return ast.value
case Kind.INT:
case Kind.FLOAT:
return parseFloat(ast.value)
case Kind.OBJECT:
return parseObject(ast)
case Kind.LIST:
return ast.values.map(parseLiteral)
case Kind.NULL:
return null
default:
return undefined
}
}

const GraphQLJSON = new GraphQLScalarType({
name: 'JSON',
description: 'A scalar type representing JSON values',
serialize: value => value,
parseValue: value => value,
parseLiteral
})

module.exports = { GraphQLJSON }
2 changes: 1 addition & 1 deletion packages/trail-fastify-graphql-plugin/lib/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { GraphQLScalarType } = require('graphql')
const { Kind } = require('graphql/language')
const { GraphQLJSON } = require('graphql-type-json')
const { GraphQLJSON } = require('./graphql-json-type')
const { DateTime } = require('luxon')
const { TrailsManager } = require('@nearform/trail-core')

Expand Down
1 change: 0 additions & 1 deletion packages/trail-fastify-graphql-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"graphql": "^14.6.0",
"graphql-jit": "0.4.2",
"graphql-tools": "4.0.7",
"graphql-type-json": "^0.3.1",
"luxon": "^1.22.0"
},
"devDependencies": {
Expand Down

0 comments on commit 80fe749

Please sign in to comment.