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”.

So I started to build new CMS for one of my project on top of React, graphQL, Apollo and MongoDb.

Simplified app. architecture looks like the following:

__root

1 |__client

2 |__public

3 |__middleware

4 |__server

Contains all React components. Each component has presentational and connecter “Apollo-client” to pass fetched data and mutation methods to component. Client-side application bundle and other public data. Mongoose: schemas, methods, helpers and graphQL: types, queries, mutations and schema. Server-side code bundle with express server.

I also use webpack to build app and separate client and server side code.

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.

For example we have collection “coupons”:

let couponSchema = mongoose.Schema({

couponCode: Array,

description: String,

discountAmount: String,

minimumAmount: String,

singleUseOnly: Boolean,

createdAt: mongoose.Schema.Types.Date,

updatedAt: mongoose.Schema.Types.Date,

expirationDate: mongoose.Schema.Types.Date

});

Now we have to create graphQL type to be able to fetch or manipulate data from our data-base:

let couponType = new GraphQLObjectType({

name: 'couponType',

description: 'single use coupon',

fields: {

_id: {type: GraphQLString},

couponCode: {type: new GraphQLList(GraphQLString)},

description: {type: GraphQLString},

discountAmount: {type: GraphQLString},

minimumAmount: {type: GraphQLString},

singleUseOnly: {type: GraphQLBoolean},

createdAt: {type: GraphQLString},

updatedAt: {type: GraphQLString},

expirationDate: {type: GraphQLString}

}

});

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!