Add extendWithModelMutations function to builder#43
Add extendWithModelMutations function to builder#43timhuff wants to merge 1 commit intoVincit:masterfrom
extendWithModelMutations function to builder#43Conversation
|
On my fork of this I added a feature that you might be interested in but wasn't sure if it was appropriate for the pull request. |
|
Hi @timhuff, Do you think it's a good idea to add mutations in objection models as static properties? I think doing so you are violating the separation of concerns a little since you are extending your models with functionality which is very specific to the API implementation (GraphQL in that case). |
|
@nasushkov That's a fair point but for me it's a matter of organization. Could the same question be asked of this library in general? We already have the models pretty tied up in the API implementation via the json schema. When building out mutations, it's nice to have the mutations right there in the model. I've been working with this patch quite a bit and I would say it's been an improvement. Functionally, it's not much different than having the functions pulled out to dedicated files (they're static functions, after all) but it results in a lot less "jumping around the file tree". If you're writing a |
|
@nasushkov That |
|
I'm also looking forward to have this feature. It will be much easier to define mutations in model, especially when you have lots of models (like in my case) |
|
FWIW, we put our mutations IN our models without any major code changes. Let me explain... I may need to publish a blog on this ;) I realize this thread is pretty old, but perhaps this could help someone. First, we have a common class that all models extend named BaseModel. In base model we have two important functions: (note: we use FLOW so you will see all the typing in there). Separately, we have a schema.js file that looks like: and the buildMutations looks like: And Viola, you now have create, update and delete mutations for EVERY model! |
Similar to my other pull request, this is just a proof of concept. I'll add tests if we want to move forward. What this enables a user to do is add a
mutationsstatic object property to their objection model that defines mutations that should be added to the schema. This is currently not compatible withextendWithMutations, though it'd be fairly easy to make it so.