Skip to content

Instantly share code, notes, and snippets.

@MikeIbberson
Created March 18, 2019 18:29
Show Gist options
  • Save MikeIbberson/557ebdddea51d246a5cfc24557268741 to your computer and use it in GitHub Desktop.
Save MikeIbberson/557ebdddea51d246a5cfc24557268741 to your computer and use it in GitHub Desktop.
GraphQL scalar for converting strings into MongoID objects.
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