<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Danish Siraj</title>
    <description>The latest articles on DEV Community by Danish Siraj (@danishsiraj).</description>
    <link>https://dev.to/danishsiraj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F525197%2F2596c807-94cd-4286-8b36-7a446e7f2b59.jpeg</url>
      <title>DEV Community: Danish Siraj</title>
      <link>https://dev.to/danishsiraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/danishsiraj"/>
    <language>en</language>
    <item>
      <title>Creating a GraphQL Server from just Mongoose Models</title>
      <dc:creator>Danish Siraj</dc:creator>
      <pubDate>Mon, 03 Oct 2022 03:45:46 +0000</pubDate>
      <link>https://dev.to/danishsiraj/creating-a-graphql-server-from-just-mongoose-models-3mi1</link>
      <guid>https://dev.to/danishsiraj/creating-a-graphql-server-from-just-mongoose-models-3mi1</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js and is one of the most common libraries that NodeJs developers are acquainted with. Mongoose has been used widely for REST applications, with the coming of &lt;a href="https://graphql.org/" rel="noopener noreferrer"&gt;GraphQL&lt;/a&gt;, which is a query language for APIs to get exactly what you need, a simple interface to bridge both seemed to be missing.&lt;/p&gt;

&lt;p&gt;To fill this gap and automatically generate a GraphQL Server from just mongoose models and provide basic CRUD operations for all models with support for deep nested populations, I created this library &lt;a href="https://www.npmjs.com/package/mongoose-graphql-server" rel="noopener noreferrer"&gt;mongoose-graphql-server&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting Started&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To get started with this library and generate CRUD with GraphQL on just mongoose models.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: Assuming MongoDB is installed locally or the connection string needs to be changed accordingly, to test with a free cloud MongDB database checkout &lt;a href="https://www.mongodb.com/atlas/database" rel="noopener noreferrer"&gt;MongoDB Atlas&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Make a directory for the project.&lt;/p&gt;

&lt;p&gt;In that directory initiate a new NodeJs project with npm or yarn&lt;/p&gt;

&lt;p&gt;With npm&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With yarn&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;yarn init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the required dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;mongoose mongoose-graphql-server &lt;span class="nt"&gt;--save&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;yarn add mongoose mongoose-graphql-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a file &lt;code&gt;index.js&lt;/code&gt; for the server code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require('mongoose');
const {
  generateSchema,
  createGraphQLServer,
} = require('mongoose-graphql-server');
const PORT = process.env.port || 3000;

mongoose.connect('mongodb://localhost/test');
const db = mongoose.connection;

const init = async () =&amp;gt; {
  // Register models

  const userModel = mongoose.model('user', {name: String, age: Number});
  const catModel = mongoose.model('cat', {name: String});
  // Build the schema

  const schema = generateSchema(mongoose);

  // Create the graphQL server

  const app = await createGraphQLServer(schema);

  // Start the server

  app.listen(PORT, () =&amp;gt; {
    console.log(
      `🚀🚀🚀 The server is running at http://localhost:${PORT}/`,
      `The GraphQL explorer is running at http://localhost:${PORT}/graphql`
    );
  });
};

db.once('open', init);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start the server with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there are no errors, head over to &lt;a href="http://localhost:3000/graphql" rel="noopener noreferrer"&gt;GraphQL endpoint&lt;/a&gt; to start exploring using the GraphQL explorer.&lt;/p&gt;

&lt;p&gt;This screen should be visible on successful server start&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgaitzwa5g6n0p26ainzr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgaitzwa5g6n0p26ainzr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was just a basic introduction more documentation and examples can be found on &lt;a href="https://www.npmjs.com/package/mongoose-graphql-server" rel="noopener noreferrer"&gt;mongoose-graphql-server&lt;/a&gt; or the &lt;a href="https://github.com/DanishSiraj/mongoose-graphql-server" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Any type of feedback is highly appreciated as this is my first npm package and since &lt;a href="https://hacktoberfest.com/" rel="noopener noreferrer"&gt;Hacktoberfest&lt;/a&gt; is upon us contributions to this project are also welcomed.&lt;/p&gt;

</description>
      <category>mongoose</category>
      <category>graphql</category>
      <category>mongodb</category>
      <category>node</category>
    </item>
  </channel>
</rss>
