The GraphQL specification includes default scalar types Int
, Float
, String
, Boolean
, and ID
. If you need to support other data types (e.g. Date
), you can define custom scalar types.
Define a scalar type:
# types.graphql
scalar JSONObject
Implement a type (or use one from the package, like graphql-type-json):
# graphql-type-json.ts
export const GraphQLJSONObject = new GraphQLScalarType({
name: 'JSONObject',
description: 'The `JSONObject` scalar type...',
serialize(value) {
...
},
parseValue(value) {
...
},
parseLiteral(ast) {
...
},
});
Make the type a part of the resolvers:
# resolvers.ts
import { GraphQLJSONObject } from './graphql-type-json.ts';
export const resolvers = {
JSONObject: GraphQLJSONObject
}
References: