Now that we are squared with Mongoose models, it's time to move to the core functionality in the app, that is creating chats and sending messages. In this video, we will enable members to start conversations with other users, and in the next episode, we will implement relationships between GraphQL types via query population.
When starting a chat, the API consumer needs to indicate the IDs of the participants that they'd want to include in the exchange. This necessitates us to validate an array of strings as Object IDs, which joi does not provide for out of the box. Luckily, it's easy to extend joi with custom validators https://github.com/hapijs/joi/blob/v1... We could either use an existing module (such as https://www.npmjs.com/package/@wegolo... from NPM) or write our own. For demo purposes, we'll go with the later.
To make it simple, we'll ask the client not to provide their own ID in the list. When it's time to feed the IDs via insert on Chat, we'll push the signed in user's ID ourselves. This is the easiest workaround that keeps the business logic straightforward and the code clean.
Next time around, we'll begin populating related models so that users can issue nested queries in the API. See you then!
Watch video MERN Stack & GraphQL - #14 Creating a Chat online without registration, duration hours minute second in high quality. This video was added by user Code Realm 20 January 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 6,60 once and liked it 7 people.