Skip to content

Instantly share code, notes, and snippets.

@barsumek
Last active November 22, 2018 13:45
Show Gist options
  • Save barsumek/f6ef1a4472935f74f79340d94cbddbdf to your computer and use it in GitHub Desktop.
Save barsumek/f6ef1a4472935f74f79340d94cbddbdf to your computer and use it in GitHub Desktop.
Detox your GraphQL: Mocking Date scalar type example
const { GraphQLScalarType } = require("graphql");
const { Kind } = require("graphql/language");
const date = {
Date: () => new Date()
};
// for custom scalar types such as Date you need to write resolvers
// then add them to ApolloServer config in startGraphQLServer
const resolvers = {
Date: new GraphQLScalarType({
name: "Date",
description: "Date custom scalar type",
parseValue(value) {
return new Date(value);
},
serialize(value) {
return value.getTime();
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return new Date(ast.value);
}
return null;
}
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment