This repository has been archived by the owner on Jun 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac91546
commit 80fe749
Showing
3 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
packages/trail-fastify-graphql-plugin/lib/graphql-json-type.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters