JSON . , , - , graphql-type-json.
, . , :
const { GraphQLScalarType, Kind } = require('graphql')
const Anything = new GraphQLScalarType({
name: 'Anything',
description: 'Any value.',
parseValue: (value) => value,
parseLiteral,
serialize: (value) => value,
})
function parseLiteral (ast) {
switch (ast.kind) {
case Kind.BOOLEAN:
case Kind.STRING:
return ast.value
case Kind.INT:
case Kind.FLOAT:
return Number(ast.value)
case Kind.LIST:
return ast.values.map(parseLiteral)
case Kind.OBJECT:
return ast.fields.reduce((accumulator, field) => {
accumulator[field.name.value] = parseLiteral(field.value)
return accumulator
}, {})
case Kind.NULL:
return null
default:
throw new Error('Unexpected kind in parseLiteral: ${ast.kind}')
}
}
, ( ), ( ). serialize GraphQL, , , data, . parseLiteral GraphQL, , (, "foo", 4.2 [12, 20]). parseValue GraphQL, , .
parseValue serialize . parseLiteral AST, , , .
, . , . , , , - :
if (typeof value == 'function') {
throw new TypeError('Cannot serialize a function!')
}
return value
. vanilla GraphQL.js, , (GraphQLString, GraphQLInt ..). Apollo, . SDL:
const resolvers = {
...
// The property name here must match the name you specified in the constructor
Anything,
}
const typeDefs = '
scalar Anything
'