DEV Community

Waheed Nazir
Waheed Nazir

Posted on

MongooseDB & GraphQl data-types

Which data type will be used against the Mongoose data type? Here we go.

Mongoose data types

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

   const studentSchema = new Schema(
        {
            "year": {
                "type": "Number"
            },
            "semester": {
                "type": "String"
            },
           "profile": {
                "name": {

## 
                    "type": "String"
                },
                "imgUrl": {
                    "type": "String"
                },
                "phone":{
                    "type": "String"
                },
                "address": {
                    "type": "String"
                }
            },
            "courses": {
                "type": [
                    "String"
                ]
            }         

        });

    module.exports = mongoose.model('Student', studentSchema);
Enter fullscreen mode Exit fullscreen mode

GraphQl object data type

/******************************************
 * student profile object for graphql
 * 
 * ***************************************/
const profile_type = new GraphQLObjectType({
    name: 'profile',
    fields: () => ({
      name: { type: GraphQLString },
      imgUrl: { type: GraphQLString },
      phone: { type: GraphQLString },
      address: { type: GraphQLString },
    }),
  });
Enter fullscreen mode Exit fullscreen mode

GraphQl all data types

const StudentType = new GraphQLObjectType({
    name: 'Student',
    fields: () => ({
        id: { type: GraphQLID  },
        year: { type: GraphQLInt },
        semester: { type: GraphQLString },
        profile: { type: profile_type },
        courses: { type: new GraphQLList(GraphQLString) }, // for array of String
    })
});
Enter fullscreen mode Exit fullscreen mode

For more tutorials pls connect

Connect with me

Top comments (0)