I have this custom scalar on the server side
new GraphQLScalarType({
name: 'Date',
description: 'Date custom scalar type',
serialize(value) {
if (value instanceof Date) {
return value.getTime(); // Convert outgoing Date to timestamp for JSON
}
throw Error('GraphQL Date Scalar serializer expected a `Date` object');
},
parseValue(value) {
if (typeof value === 'number') {
return new Date(value); // Convert incoming timestamp to Date
}
throw new Error('GraphQL Date Scalar parser expected a `number`');
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
// Convert hard-coded AST string to timestamp and then to Date
return new Date(parseInt(ast.value, 10));
}
// Invalid hard-coded value (not an integer)
return null;
},
});
I wanna do the same thing on the client side, turning Date
graphql typed data to javascript Date
object when receiving, calling .getTime()
and turning into timestamp when sending.
I tried settings typeDefs
and resolvers
on the Apollo Client. But it doesn’t have any effect.
const client = new ApolloClient({
uri: 'http://localhost:4000',
cache: new InMemoryCache(),
typeDefs: `
scalar Date
`,
resolvers: {
Date: dateScalar, // same custom scalar as the server side
}
});
``