Primitive scalar types in GraphQL are Int
, Float
, String
, Boolean
and ID
. For JSON
and Date
you need to define your own custom scalar types, the documentation is pretty clear on how to do this.
In your schema you have to add:
scalar Date
type MyType {
created: Date
}
Then, in your code you have to add the type implementation:
import { GraphQLScalarType } from 'graphql';
const dateScalar = new GraphQLScalarType({
name: 'Date',
parseValue(value) {
return new Date(value);
},
serialize(value) {
return value.toISOString();
},
})
Finally, you have to include this custom scalar type in your resolvers:
const server = new ApolloServer({
typeDefs,
resolvers: {
Date: dateScalar,
// Remaining resolvers..
},
});
This Date
implementation will parse any string accepted by the Date
constructor, and will return the date as a string in ISO format.
For JSON
you might use graphql-type-json
and import it as shown here.