Skip to content

Instantly share code, notes, and snippets.

@Vibhu-Agarwal
Last active April 26, 2020 12:55
Show Gist options
  • Save Vibhu-Agarwal/a63f75098d0d58ff488541a585a8933d to your computer and use it in GitHub Desktop.
Save Vibhu-Agarwal/a63f75098d0d58ff488541a585a8933d to your computer and use it in GitHub Desktop.
API Design for particular use-case

I've to design APIs, with MongoDB being used as storage.
Will be using Node/ExpressJS for now.

So ... here's my use case.
There would be a lot of attributes/fields in each MongoDB document (more than 20)
But anytime I'd fetch the document, I'd only want 2-3 fields ... based on arguments
Also, there'll be filters passed on to the back-end
Seems like a perfect use case for GraphQL

But the problem is that only a few attributes (~5-6) would be known beforehand.
Other attributes fields will be added into the DB as the application grows.

I've never used GraphQL in a proper project, so just consider me a newbie.
So, as far as I've looked, GraphQL is completely statically typed and we have to specify the types of fields in schema beforehand.
In my case, that won't be possible ... because I've to query on all the other dynamic attributes too.

Right now, the approach that I've thought of is ... switch to REST completely
I'd dynamically maintain the structure per document (like maintaining information about keys and their types): [{field: "color", type: "String"}, {field: "parts", type: "Array(String)"}]
While using APIs, I'd expect an array in fields arg which would contain which fields they want
And an array of filters something like: [["field1", "less_than", 100], ["field2_array", "contains", "value"]]
Then I'd map those filter names to the mongoDB queries and finally return the result
This, however doesn't look a very neat approach ... more of a juggad, really

I was wondering what would be the best way to solve my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment