Last active
November 22, 2018 13:45
-
-
Save barsumek/f6ef1a4472935f74f79340d94cbddbdf to your computer and use it in GitHub Desktop.
Detox your GraphQL: Mocking Date scalar type example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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