Created
March 18, 2019 18:29
-
-
Save MikeIbberson/557ebdddea51d246a5cfc24557268741 to your computer and use it in GitHub Desktop.
GraphQL scalar for converting strings into MongoID objects.
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
import { GraphQLScalarType } from 'graphql'; | |
import { Kind } from 'graphql/language'; | |
import { ObjectId } from 'mongodb'; | |
export default new GraphQLScalarType({ | |
name: 'ID', | |
description: 'MongoDB ObjectID string type', | |
serialize: value => value.toString(), | |
parseValue: value => ObjectId(value), | |
parseLiteral: ast => | |
ast.kind === Kind.STRING ? | |
ObjectId(ast.value) : | |
null | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment