<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sarkis Arutiunian on Medium]]></title>
        <description><![CDATA[Stories by Sarkis Arutiunian on Medium]]></description>
        <link>https://medium.com/@sarkis.tlt?source=rss-2252136ddca1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*D4w-MXlVVpeEgsv11N8XOA.png</url>
            <title>Stories by Sarkis Arutiunian on Medium</title>
            <link>https://medium.com/@sarkis.tlt?source=rss-2252136ddca1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 20:59:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sarkis.tlt/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SoftLayer Object Storage helper]]></title>
            <link>https://medium.com/@sarkis.tlt/softlayer-object-storage-helper-c8d63910b57c?source=rss-2252136ddca1------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8d63910b57c</guid>
            <category><![CDATA[ibm]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[softlayer]]></category>
            <category><![CDATA[object-storage]]></category>
            <dc:creator><![CDATA[Sarkis Arutiunian]]></dc:creator>
            <pubDate>Fri, 31 Mar 2017 23:17:47 GMT</pubDate>
            <atom:updated>2021-09-16T07:07:48.762Z</atom:updated>
            <content:encoded><![CDATA[<p>Pretty helpful module for those of us who’s using object storage. <a href="https://github.com/sarkistlt/softlayer-object-storage">link</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8d63910b57c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL CMS v2 is ready to use]]></title>
            <link>https://medium.com/@sarkis.tlt/graphql-cms-v2-is-ready-to-use-bceee835e489?source=rss-2252136ddca1------2</link>
            <guid isPermaLink="false">https://medium.com/p/bceee835e489</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Sarkis Arutiunian]]></dc:creator>
            <pubDate>Tue, 15 Nov 2016 04:32:38 GMT</pubDate>
            <atom:updated>2016-11-15T23:19:38.123Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wFcPcqvMqoem6FIX2SYZ9w.png" /></figure><p>Earlier I introduced the first version of CMS: <a href="https://medium.com/@sarkis.tlt/auto-generating-cms-based-on-your-graphql-schema-5eaa6b07987b#.92to62kpp">Auto generating CMS based on your GraphQL schema</a>, and I am highly recommend you to read that article too if you haven’t yet.</p><p>In this article I want to shortly introduce to you the second version of <a href="https://github.com/sarkistlt/graphql-auto-generating-cms">GraphQL CMS</a>. You’ll find what’s new and what we have and what else you should expect in future release.</p><p><strong>Shortly about project:</strong></p><p><a href="https://github.com/sarkistlt/graphql-auto-generating-cms">GraphQL CMS</a> is a npm module which can save you a lot of time you’re currently spending on integration with some CMS or building your own admin-panel. All you need to do is just provide your printed GraphQL schema in config object and that’s it, module will do the rest of work for you! It will automatically generate fully-functional CMS with all your GraphQL essence, and on any changes in schema it will dynamically catch all changes and generate new menu points, fields and etc.</p><p><strong>Product philosophy:</strong></p><p><strong>Simplicity.</strong> You don’t have to change a line of code in your project, so you’re free to use any architecture you want. Consider CMS as simple React component which you can render wherever you want, inside of other components. For example to provide header and footer or on separate route.</p><p>For the same reason we don’t provide authentication system, so you can use any approach to secure your CMS and API you want. We recommend to use <a href="http://feathersjs.com/">FeathersJs</a>, it’s easy to use and a great free tool.</p><p><strong>What’s new and what it support:</strong></p><p>The module is pretty scalable and has ability to extend CMS with any custom functionality so it can solve 100% of your needs from CMS.</p><p>Even without custom extending CMS it will automatically give you following functionalities, which is more than enough for 90% of needs from CMS:</p><ul><li><strong>[New]</strong> <a href="https://sarkistlt.gitbooks.io/graphql-cms/content/">Documentation</a></li><li><strong>[New]</strong> Support of nested GraphQL Type fields, any deeps</li><li><strong>[New]</strong> Support of GraphQL List, with auto populating if list of other essence from data-base, or with ability directly create new items in list if it’s just a list of some items</li><li><strong>[New] </strong>Ability to use file uploading system and refer it to any field you want</li><li>Pagination</li><li>Fields and Side menu points ordering</li><li>Ability of detailed configuration with custom labels for fields, menu points, and etc</li><li>Easy to extend with custom components where you can use any other custom functions you need</li><li>Full support of CRUD</li></ul><p>As you can see CMS supports a lot of useful stuff out of the box.</p><p><strong>TODO:</strong></p><ul><li>Test cases and commented code, to make it easier to contribute</li></ul><p>If you have any suggestions to what else you would like to see in the new release please feel free to give your advice.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bceee835e489" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Auto generating CMS based on your GraphQL schema.]]></title>
            <link>https://medium.com/@sarkis.tlt/auto-generating-cms-based-on-your-graphql-schema-5eaa6b07987b?source=rss-2252136ddca1------2</link>
            <guid isPermaLink="false">https://medium.com/p/5eaa6b07987b</guid>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Sarkis Arutiunian]]></dc:creator>
            <pubDate>Tue, 01 Nov 2016 05:37:40 GMT</pubDate>
            <atom:updated>2016-11-17T17:49:05.246Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wFcPcqvMqoem6FIX2SYZ9w.png" /></figure><p><a href="https://medium.com/@sarkis.tlt/graphql-cms-v2-is-ready-to-use-bceee835e489#.cenw3nx6d"><strong>second version of GraphQL CMS</strong></a></p><p>I was really excited about capabilities graphQL gives you when I first started to work with it.</p><p>So far this pretty new but powerful tool doesn’t have that good support from developers community as I would like it to have.</p><p>In my last article I wrote about npm package <a href="https://github.com/sarkistlt/mongoose-schema-to-graphql">mongoose-schema-to-graphql</a> which will help any developers who works with Mongoose to short their code twice and save the same amount of time.</p><p>Today I will talk about my new package <a href="https://github.com/sarkistlt/graphql-auto-generating-cms"><strong>graphql-auto-generating-cms</strong></a> which probably could save you even more time, and let you think more about architecture and business processes then about building UI for admin-panel. You will find some screenshot on the end of the article.</p><p>As you already figured out from the name, this package will use your printed schema to automatically generate fully-functional CMS without any additional efforts.The most important is that without any changes in your app architecture and current code!</p><p>Package contains two modules: first is a simple React component you will use to render CMS, second is Node/Express middleware to provide schema and additional configurations if you needed.</p><p>Basically you have two ways of how to generate CMS: first way is with minimum efforts, you would just provide schema and insert CMS component wherever you want, in a separate rout or inside another component “wrapper”. For example to provide footer or header with login system or just to extend CMS functionality. And second way is more detailed and advanced configuration. In both situation you have to follow the simple pattern we will talk about shortly.</p><p>Right now this package doesn’t support list and nested elements, but it will be provided in future releases. For now you can solve this problem just by extending autogenerated CMS. Will talk how to do that in this article.</p><p>What does CMS support:</p><ul><li>Pagination</li><li>Create new items</li><li>Update</li><li>Remove</li><li>Exclude some types from CMS</li><li>Detailed rules to generate CMS</li><li>Field validation based on your schema</li><li>Custom pages to extend CMS functionality</li></ul><p>What it doesn’t support [will be supported in future releases]:</p><ul><li>Nested elements and lists [could be solved by using custom pages]</li><li>Routing [just not first priority]</li><li>File uploading</li></ul><p>So far functionality provided by this module will be enough for most cases of projects, and will save you a good amount of time and money.</p><p>It’s good enough out of the box for some static content pages, and for any other solution you can just extend functionality by providing custom CMS pages.</p><p>Based on the the above lets separate this article to following paragraphs:</p><ul><li>Common requirements</li><li>Prepare to start</li><li>Fast method of generating CMS</li><li>More detail configured CMS generation</li><li>Config file structure</li><li>Extend CMS with your additional functionalities</li></ul><h4>Common requirements</h4><p>Actually this is pretty simple. All your Query methods has to have: {offset: Int!, limit: Int!} arguments which basically will be used for pagination, because without pagination if you would provide 60k documents to CMS component it will crushed.</p><p>In your resolver it could look like the following:</p><pre><strong>return new </strong>Promise((resolve, reject) =&gt; {<br>   Ingredients.find(query).skip(offset).limit(limit).exec((err, res)    =&gt;  err ? reject(err) : resolve(res));<br>});</pre><p>Second requirements is in your Query method. You have to support “id” or “_id” argument with type “string” or “Int” based on which kind of storage you use. This argument will be used to query one item, so make sure that you provide this fields in graphql Type’s fields as well.</p><p>Also, if you use “findOne” instead of “find” or any other method to return one item, make sure that you wrapped it inside array, [{item}].</p><p>Keep in mind that CMS will use only graphql types which has one Query method and at least one Mutation method.</p><p>All types can only have one Query method [find] to query list of items or one item by providing id, and three Mutation method [create, update, remove].</p><h4>Prepare to start</h4><pre>npm i -S graphql-auto-generating-cms</pre><p>On your server side, we have to insert middleware to provide schema, and endpoint url which we will use on client, in example it is ‘/graphql_cms_endpoint’:</p><pre>…<br>import express from &#39;express&#39;;<br>import graphqlCMS from &#39;graphql-auto-generating-cms/lib/middleware&#39;;<br>import schema from &#39;../schema&#39;;<br>const printSchema = require(&#39;graphql/utilities/schemaPrinter&#39;).printSchema;<br>let app = express();<br><br>let config = {schema: printSchema(schema)}<br>app.use(&#39;/graphql_cms_endpoint&#39;, graphqlCMS(config));<br>…<br>app.listen(port)</pre><p>After that we can use our React component to render CMS wherever we want. In the example below we will run it on a separate rout ‘/graphql-cms’:</p><pre>...<br>import GraphqlCMS from &#39;graphql-auto-generating-cms&#39;;<br><br>export default (<br>    &lt;Router onUpdate={() =&gt; window.scrollTo(0, 0)} history={browserHistory}&gt;<br>        &lt;Route<br>           path=&#39;/graphql-cms&#39;<br>           endpoint=&#39;/graphql_cms_endpoint&#39;<br>           graphql=&#39;/graphql&#39;<br>           components={GraphqlCMS}<br>        /&gt;<br>        …<br>&lt;/Router&gt;</pre><p>Or as child component:</p><pre>&lt;GraphqlCMS<br>   endpoint=&#39;/graphql_cms_endpoint&#39;<br>   graphql=&#39;/graphql&#39;<br>/&gt;</pre><p>In property “endpoint” we provide same endpoint URL we provided in middleware and in “graphql” property we provide URL to our GraphQL API.</p><p>And basically that’s it! Based on which approach (simple or detailed) you will use on path ‘/graphql-cms’ you will see your ready to use autogenerated CMS. Now let’s talk about aproach how to make all this work.</p><h4>Fast method of generating CMS</h4><p>To use this approach and leave all work to module you have to use following naming pattern in your graphql schema. Keep in mind by using second approach you don’t have to follow any pattern so you don’t have to change anything in your existing code! I prefer second approach but this one will be very useful for new project or in dev mode.</p><p>Your CMS structure will use the same structure you used in your schema, so if you have:</p><pre>{<br>productType: {},<br>userType: {},<br>categoryType: {},<br>...<br>}</pre><p>In your side menu of CMS it will have same order:</p><pre>productType<br>userType<br>categoryType</pre><p>It would be the same about properties. If you have following shape:</p><pre><strong>let </strong>productType = <strong>new </strong>GraphQLObjectType({<br>    name: &#39;productType&#39;,<br>    fields: {<br>        _id: {type: GraphQLString},<br>        title: {type: GraphQLString},<br>        shortDescription: {type: GraphQLString},<br>        price: {type: GraphQLString},<br>        isPublished: {type: GraphQLBoolean},<br>        createdAt: {type: GraphQLString},<br>        updatedAt: {type: GraphQLString},<br>        bulletPoints: {type: GraphQLString},<br>        scienceShort: {type: GraphQLString},<br>        scienceFull: {type: GraphQLString},<br>    }<br>});</pre><p>In your UI of product item view page, fields will have exact same order.</p><p>So if you want to change the order in your CMS, you just have to make changes in your schema.</p><p>By the way, in the second approach with configuration, to sort the fields in CMS we will use same approach except instead of making any change in schema we will specify it in config object.</p><p>About naming. As we talk above about Query method [find] and Mutation methods [create, update, remove], In order to let module know which method is what, you have to use following pattern of naming Query`s or Mutation`s methods:</p><pre>[graphql Type name]_[action]</pre><p>examples:</p><pre>productType_find<br>productType_create<br>productType_update<br>productType_remove</pre><p>The pattern is pretty easy and again in the second approach we won’t have to follow this pattern or do any changes in existing code. So let’s talk about it.</p><h4>More detail configured CMS generation</h4><p>Pros in compare with the first method:</p><ul><li>You don’t have to change your current code at all</li><li>You don’t have to use naming pattern</li><li>You can deprecate some method you don’t want to use in CMS</li><li>You can change the order for the side menu or fields in the view without any change in current schema</li><li>You can provide different side menu and fields label. By default it will use your graphql Type name for side menu and props name for fields label in the view page</li><li>Disabling some fields in item view page</li><li>You can customize which fields will be used in the table list page for each column. By default it will use [id/_id] for first column and second field of graphql Type as the second column</li><li>You can specify custom input types and controllers, for example “date” or ‘textarea’</li></ul><p>By default all fields of graphql Type’s which is not mentioned in Mutation method as arguments will be shows as disabled, so you can’t edit it, it can be different fields based on your action. When you edit or create new item, each action will use arguments from its Mutation method to mark some fields disabled or to enable all of them.</p><p>So let’s see how it works.</p><p>On your server side we just have to extend config object. All config properties besides “schema” are not required.</p><pre>let config = {schema: printSchema(schema)}<br>app.use(&#39;/graphql_cms_endpoint&#39;, graphqlCMS(config));</pre><p><strong>Config file structure</strong></p><pre>let config = {<br>    schema: printSchema(schema), <br>    // your printed schema [required]<br>    <br>    exclude: [&#39;paymentType&#39;, &#39;invoiceType&#39;], <br>    //graphql Types which you don&#39;t want to show in CMS<br><br>    rules: {<br>    // rules object is a tree with rules for each or some <br>    // graphql types <br>    // CMS in addition will use the same order for side menu <br>    // and fields <br>    // in the view page as you will provide in “rules” object<br>    <br>        categoryType: { <br>        // graphql Type name, by default will be used as name <br>        // for side menu<br>        <br>            label: &#39;Categories&#39;, <br>            // custom side menu name<br><br>            listHeader: {<br>            // data from this fields will be used to show on first <br>            // column [id] and second [title] on list page. <br>            // you can provide  couple of fields for each columns <br>            // so it will shows as “String” + “ “ + “String”<br>            <br>                id: [&#39;id&#39;],<br>                title: [&#39;description&#39;]<br>            },<br>            <br>            resolvers: {<br>            // if you don&#39;t want to use the naming pattern <br>            // you have to provide <br>            // Query&#39;s and Mutation&#39;s method name for each <br>            // graphql Type.<br>                                    <br>                find: {<br>                    resolver: &#39;getCategories&#39; <br>                    // Query method name<br>                },<br>                create: {<br>                    resolver: &#39;addCategory&#39;<br>                    // Mutation method name<br>                    <br>                    allowed: true<br>                },<br>                update: {<br>                    resolver: &#39;updateCategory&#39;<br>                    // Mutation method name<br>                    <br>                    allowed: true<br>                },<br>                remove: {<br>                    allowed: false<br>                    // if you don&#39;t want to provide some method <br>                    // to client <br>                    // side you can depreciate it for any action  <br>                    // besides “find”<br>                }<br>            },<br>            fields: {<br>                _id: {}, <br>                // never exclude this field or “id” you always <br>                // have to provide id<br>                <br>                sortNumber: {<br>                    label: &#39;custom field name to show in UI&#39;,<br>                    inputControl: &#39;input&#39;, <br>                    // can be “input” or “textarea”<br>                    <br>                    inputType: &#39;number&#39;, <br>                    // can be any input type: date, text, file etc.<br>                    <br>                    disabled: true, <br>                    // will disable field from editing<br>                    <br>                    exclude: false, <br>                    // if true (by default false) it won&#39;t <br>                    // provide this field<br>                    // to client side so you can&#39;t see it in UI<br>                },<br>                name: {}, <br>                // you can also provide empty object <br>                // if you want to just order fields<br>                <br>                createdAt: {},<br>                updatedAt: {},<br>                isPublished: {}<br>            }<br>        }<br>    }<br>}</pre><h4>Extend CMS with your additional functionalities</h4><p>You can also improve and extend CMS functionality by providing custom pages to CMS with your own logic. For example to handle some features auto-generator doesn’t support yet, like lists or file uploading.</p><p>I’m not going to teach you on how to build React components, so I will show you the logic of how to provide custom components to CMS.</p><p>All custom pages will have first priority in side menu and will show above other menu points.</p><p>On your client side we have to provide one more property “newMenuItems” to component:</p><pre>&lt;Route<br>   path=&#39;/graphql-cms&#39;<br>   endpoint=&#39;/graphql_cms_endpoint&#39;<br>   graphql=&#39;/graphql&#39;<br>   newMenuItems={customPages}<br>   components={GraphqlCMS}<br>/&gt;</pre><p>And in that property we provide array with new menu points “customPages” has following structure:</p><pre>let customPages = [<br>    {<br>       label: &#39;Custom Page&#39;,<br>       secret: &#39;uniqeForEachComponentSecret&#39;,<br>       view: {<br>           secret: &#39;sameUniqeComponentSecret&#39;,<br>           component: CustomDashboard //your custom React component<br>       }<br>    }<br>]</pre><p>Above code will look like screenshot below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q0xui4na_LbZlTFRnU-gWQ.png" /></figure><p>I hope this package will be helpful for you. If you like this project, you can star/fork it on GitHub to be up to date about new releases.</p><p>This is the first release so if you find any bugs please let me know.</p><p>Also it will be very appreciated if you can help with any of the following:</p><ul><li>Well written documentation</li><li>Your participation and contribution to this project to keep it alive</li><li>Any suggestion on what would you like to see in future releases</li></ul><p>Thank you.</p><p><strong>Currently I’m working on new version of module, so Star project on GitHub to be up to date.</strong></p><p><strong>What will new version support:</strong></p><ul><li><strong>Nested properties in graphQL Type, any depth</strong></li><li><strong>graphQLList support</strong></li><li><strong>Routing</strong></li><li><strong>File uploading</strong></li></ul><p><strong>So with new release auto-generated CMS will handle 90% of your needs from CMS, just by providing printed GraphQL schema!</strong></p><p><a href="https://github.com/sarkistlt/graphql-auto-generating-cms/tree/master/example"><strong>example code</strong></a></p><p><a href="https://github.com/sarkistlt/graphql-auto-generating-cms"><strong>GitHub</strong></a></p><p>Screenshots:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zyFkKo8eEbTT58XrKoH24A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tEERSPuQ2A1QyG_sHNjqrg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2KQY1zMjSe1w9Tw5zbREJQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T1gOKOYUC0X-DETGL-OguQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BE6F9UyyYboHarjxlWtXbg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5eaa6b07987b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use Mongoose schema to generate graphQL type.]]></title>
            <link>https://medium.com/@sarkis.tlt/how-to-use-mongoose-schema-to-generate-graphql-type-ada63a89915?source=rss-2252136ddca1------2</link>
            <guid isPermaLink="false">https://medium.com/p/ada63a89915</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[mongoose]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[mongodb]]></category>
            <dc:creator><![CDATA[Sarkis Arutiunian]]></dc:creator>
            <pubDate>Wed, 21 Sep 2016 05:00:24 GMT</pubDate>
            <atom:updated>2016-09-26T23:46:26.795Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SxELACkBC3P_UDIR7F3rGg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w9X24THjo-ApNZxm6aOokA.jpeg" /></figure><p>First time I started to work with graphQL I thought, “wow that’s awesome! I can build scalable web app. with absolutely new view to architecture”.</p><p>So I started to build new CMS for one of my project on top of React, <a href="http://graphql.org/learn/">graphQL</a>, <a href="http://dev.apollodata.com/react/">Apollo</a> and MongoDb.</p><p>Simplified app. architecture looks like the following:</p><pre>__root<br>1 |__client<br>2 |__public<br>3 |__middleware<br>4 |__server</pre><ol><li>Contains all React components. Each component has presentational and connecter “Apollo-client” to pass fetched data and mutation methods to component.</li><li>Client-side application bundle and other public data.</li><li>Mongoose: schemas, methods, helpers and graphQL: types, queries, mutations and schema.</li><li>Server-side code bundle with express server.</li></ol><p>I also use <a href="https://webpack.github.io/">webpack</a> to build app and separate client and server side code.</p><p>As soon as the project was coming together, I had seen that for each essence and collection in data-base I have to almost duplicate the same schema which I already created for Mongoose to create graphQL type.</p><p>For example we have collection “coupons”:</p><pre><strong>let </strong>couponSchema = mongoose.Schema({<br>    couponCode: Array,<br>    description: String,<br>    discountAmount: String,<br>    minimumAmount: String,<br>    singleUseOnly: Boolean,<br>    createdAt: mongoose.Schema.Types.Date,<br>    updatedAt: mongoose.Schema.Types.Date,<br>    expirationDate: mongoose.Schema.Types.Date<br>});</pre><p>Now we have to create graphQL type to be able to fetch or manipulate data from our data-base:</p><pre><strong>let </strong>couponType = <strong>new </strong>GraphQLObjectType({<br>    name: &#39;couponType&#39;,<br>    description: &#39;single use coupon&#39;,<br>    fields: {<br>        _id: {type: GraphQLString},<br>        couponCode: {type: <strong>new </strong>GraphQLList(GraphQLString)},<br>        description: {type: GraphQLString},<br>        discountAmount: {type: GraphQLString},<br>        minimumAmount: {type: GraphQLString},<br>        singleUseOnly: {type: GraphQLBoolean},<br>        createdAt: {type: GraphQLString},<br>        updatedAt: {type: GraphQLString},<br>        expirationDate: {type: GraphQLString}<br>    }<br>});</pre><p>So as you can see it’s pretty much the same logic as in Mongoose schema. Now imagine that you have a couple tens of essences with different structure, nested elements and etc. You’ll have to retype almost same thing for each of them which is a lot of logically duplicated codes!</p><p>To avoid this problem I built a small npm package: “<a href="https://github.com/sarkistlt/mongoose-schema-to-graphql">mongoose-schema-to-graphql</a>”. It’s just 3kb of code which will save you a lot of time and prevent typing useless lines of code.</p><p>Basically this package uses your existed Mongoose schema, (which you anyway have to declare to create collection) and based on that it returns a generated graphQL class with the same structure.</p><p>All we need is:</p><pre>npm i mongoose-schema-to-graphql --save</pre><p>After that you have to import “MTGQL” function from the package which takes one config object as argument with the following structure:</p><pre>let configs = {<br>              name: &#39;couponType&#39;, <br>//graphQL type&#39;s name</pre><pre>              description: &#39;Coupon base schema&#39;, <br>//graphQL type&#39;s description</pre><pre>              class: &#39;GraphQLObjectType&#39;, <br>//&quot;definitions&quot; class name</pre><pre>              schema: couponSchema, <br>//your Mongoose schema</pre><pre>              exclude: [&#39;_id&#39;], <br>//fields which you want to exclude from mongoose schema</pre><pre>              props: {<br>                price: {type: GraphQLFloat}<br>              } <br>//add custom properties or overwrite existed<br>          }</pre><p>Couple examples on how it works below:</p><blockquote>dbSchema.js</blockquote><pre><strong>import </strong>mongoose <strong>from </strong>&#39;mongoose&#39;;</pre><pre><strong>let </strong>selectObj = {<br>    value: String,<br>    label: String<br>};<br><br><strong>let </strong>answerSchema = mongoose.Schema({<br>    createdAt: mongoose.Schema.Types.Date,<br>    updatedAt: mongoose.Schema.Types.Date,<br>    title: String,<br>    answersImage: String,<br>    recommended: [selectObj],<br>    isPublished: Boolean<br>});<br><br><strong>export let </strong>questionSchema = mongoose.Schema({<br>    question: String,<br>    defRecommended: [selectObj],<br>    createdAt: mongoose.Schema.Types.Date,<br>    updatedAt: mongoose.Schema.Types.Date,<br>    isPublished: Boolean,<br>    multipleChoice: Boolean,<br>    answers: [answerSchema]<br>});</pre><p>In your graphQLs type code:</p><blockquote>type.js</blockquote><pre><strong>import </strong>MTGQL <strong>from </strong>&#39;mongoose-schema-to-graphql&#39;;<br><strong>import </strong>{questionSchema} <strong>from </strong>&#39;./dbSchemas&#39;;<br><br><strong>let </strong>config = {<br>    name: &#39;questionType&#39;,<br>    description: &#39;Question collection\&#39;s type&#39;,<br>    class: &#39;GraphQLObjectType&#39;,<br>    schema: questionSchema,<br>    exclude: [&#39;_id&#39;]<br>};<br><br><strong>export let </strong>questionType = MTGQL(config);</pre><p>Just 10 lines of code which equal to:</p><pre><strong>import </strong>{<br>    GraphQLObjectType, <br>    GraphQLString, <br>    GraphQLBoolean, <br>    GraphQLList, <br>    GraphQLInt<br>} <strong>from </strong>&#39;graphql&#39;;<br><br><strong>let </strong>selectType = <strong>new </strong>GraphQLObjectType({<br>    name: &#39;selectType&#39;,<br>    fields: {<br>        value: {type: GraphQLString},<br>        label: {type: GraphQLString}<br>    }<br>});<br><br><strong>let </strong>answerType = <strong>new </strong>GraphQLObjectType({<br>    name: &#39;answerType&#39;,<br>    description: &#39;answer type for question&#39;,<br>    fields: {<br>        title: {type: GraphQLString},<br>        answersImage: {type: GraphQLString},<br>        recommended: {type: <strong>new </strong>GraphQLList(selectType)},<br>        createdAt: {type: GraphQLString},<br>        updatedAt: {type: GraphQLString},<br>        isPublished: {type: GraphQLBoolean}<br>    }<br>});<br><br><strong>export let </strong>questionType = <strong>new </strong>GraphQLObjectType({<br>    name: &#39;questionType&#39;,<br>    description: &#39;Question collection\&#39;s type&#39;,<br>    fields: {<br>        question: {type: GraphQLString},<br>        defRecommended: {type: <strong>new </strong>GraphQLList(selectType)},<br>        createdAt: {type: GraphQLString},<br>        updatedAt: {type: GraphQLString},<br>        isPublished: {type: GraphQLBoolean},<br>        multipleChoice: {type: GraphQLBoolean},<br>        answers: {type: <strong>new </strong>GraphQLList(answerType)}<br>    }<br>});</pre><p>I think the difference is evident. Package doesn’t support “GraphQLFloat” out of the box because in Mongoose schema you can declare only integer numbers, but you can easily handle it by just passing property manually in config object.</p><p>More information on how it works can be found on <a href="https://github.com/sarkistlt/mongoose-schema-to-graphql">GitHub</a>.</p><p>I hope this package and article will be useful for you.</p><p>If you have any suggestions just let me know.</p><p>Thank You.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ada63a89915" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>