Sunday, 17 November 2019

MERN + GraphQL backend difficulty

I am trying to practice building a full stack app in MERN + GraphQL. I am running into an issue where when I create and insert a new object with Mongoose, I can see the change reflected in the DB immediately, but when I run a get all query in GraphQL, the new object doesn't show up for 30 seconds to a minute. this is keeping my React UI and Apollo cache from updating as well.​[Server code for reference if needed](https://github.com/markbmullins/Node-Notes-GraphQL/tree/master/server)This is my GraphQL Schema:​``js const schema = buildSchema( type Note { id: ID title: String content: String } type Query { getNote(id: ID!): Note getNotes: [Note] } type Mutation { createNote(title: String!, content: String): Note updateNote(id: ID!, title: String!, content: String): Note deleteNote(id: ID!): Boolean } ); `` I am running the getNotes query,``` { getNotes { id content } }``` and my resolvers:```js const root = { getNotes: noteService.getAll(), getNote: ({ id }) => noteService.getByID(id), createNote: ({ title, content }) => noteService.create(title, content).then(note => note), updateNote: ({ id, title, content }) => noteService.update(id, title, content).then(note => note), deleteNote: ({ id }) => noteService.deleteById(id).then(note => (note ? true : false)) };const graphql = graphqlHTTP({ schema, rootValue: root, graphiql: true }); ```service layer for this function is very thin,js const getAll = () => { return noteRepository.getAll(); };and repository function with mongoosejs const getAll = () => { return Note.find({}).exec(); };and Mongoose schema:```jsconst mongoose = require('mongoose'); const Schema = mongoose.Schema;let Note = new Schema({ title: String, content: String });module.exports = mongoose.model('Note', Note); ```

Submitted November 18, 2019 at 04:27AM by DeepSpaceGalileo

No comments:

Post a Comment