Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Mutation: {
- addPost: async (parent, args) => {
- // Add new post to dbPosts
- const task = fawn.Task();
- task.save(
- dbPost,
- {
- _id: new mongoose.Types.ObjectId(),
- title: args.title,
- content: args.content,
- created: args.created,
- author: {
- id: args.author_id,
- first_name: args.author_first_name,
- last_name: args.author_last_name,
- }
- }
- );
- }
- }
- scalar DateTime
- type Query {
- posts: [Post],
- post(id: ID!): Post,
- }
- type Mutation {
- addPost(
- title: String!,
- content: String!,
- created: DateTime!,
- author_id: String!,
- author_first_name: String!
- author_last_name: String!): Post,
- }
- type Post {
- id: ID!
- title: String!,
- content: String!,
- author: Author!,
- created: DateTime,
- }
- const { GraphQLScalarType } = require('graphql/type');
- const tmUTC = () => {
- const tmLoc = new Date();
- return tmLoc.getTime() + tmLoc.getTimezoneOffset() * 60000;
- };
- DateTime = new GraphQLScalarType({
- name: 'DateTime',
- description: 'Date/Time custom scalar type',
- parseValue: () => { // runs on mutation
- return tmUTC();
- },
- serialize: (value) => { // runs on query
- return new Date(value.getTime());
- },
- parseLiteral: () => {
- return tmUTC();
- },
- });
- module.exports = DateTime;
- mutation{
- addPost(
- title: "Ghostbusters",
- content: "Lots and lots of ghosts here...",
- created: "",
- author_id: "5ba0c2491c9d440000ac8fc3",
- author_first_name: "Bill",
- author_last_name: "Murray"
- ){
- title
- content
- id
- created
- }
- }
- type Mutation {
- addPost(
- title: String!,
- content: String!,
- // created: DateTime!, changed in next line
- created: DateTime, // no ! means not required
- author_id: String!,
- author_first_name: String!
- author_last_name: String!): Post,
- }
- addPost: async (parent, args) => {
- // if args does not have created, make it here
- const task = fawn.Task();
- task.save(
- dbPost,
Add Comment
Please, Sign In to add comment