Hello Folks!
Back in time, I was preparing for a live-code session where I demonstrated adding a layer of GraphQL to an existing REST API. The session went well, and after that, I noticed my way of writing code and organizing files have really helped me adding the GraphQL layer very smoothly, the codebase architecture really made it feel like a hot knife on butter.
You can find the code repo here (do star it! :P) rest-and-graphql
Let's explore the codebase.
The source files are inside the /src
folder. Let's define the roles of the folders inside /src
-
config
: contains the.env
files -
controllers
: contains the route handlers using methods insideroutes
-
db
: contains the database connection logic -
graphql
: contains the resolvers and definitions -
routes
: contains the definition of the routes usingservices
-
services
: contains the logic to fetch data from DB -
utils
: contains the globally used util functions
As you can see, the codebase is organized as per the entities. What really helped me to scale here is the services
folder, it is the real gem here
I wrote the services only once for the REST APIs and due to the patterns I followed made the construction of the GraphQL layer very easy. I only had to call the service inside the resolvers instead of writing it again and again for different layers.
One added benefit of this kind of codebase is that you can easily split it into independent microservices just by taking out the entities from routes, services, controllers folder
Feel free to explore the repo on Github: rest-and-graphql, you can also browse my other projects. And with time I will try to extend this article in detail.
Let me know your thoughts!
Top comments (0)