Skip to content

Instantly share code, notes, and snippets.

@kluu1
Last active April 1, 2020 18:58
Show Gist options
  • Select an option

  • Save kluu1/f22a5d1c621fbdeacb592cf902c8c94e to your computer and use it in GitHub Desktop.

Select an option

Save kluu1/f22a5d1c621fbdeacb592cf902c8c94e to your computer and use it in GitHub Desktop.
const { GraphQLScalarType } = require('graphql') ;
const { Kind } = require('graphql/language');
const orders = require('../db');
const resolvers = {
Query: {
orders: (_, args) => {
return orders;
}
},
Mutation: {
addOrder: (_, { userId, amount }) => {
const newOrder = {
userId,
amount,
createdAt: Date.now()
}
orders.push(newOrder);
return newOrder;
}
},
Date: new GraphQLScalarType({
name: 'Date',
description: 'Custom date scalar',
parseValue(value) {
return value;
},
serialize(value) {
return new Date(Number(value));
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return new Date(ast.value);
}
return null;
}
})
};
module.exports = resolvers;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment