-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Models or Connectors? #118
Comments
I think you're right. Here's how I think about it:
It's easy to see this distinction in the GitHunt app:
I think we should improve the documentation to make this concept a lot clearer, thanks for bringing this up. Does the above distinction make sense? |
Thank you! The links were very helpful and allowed me to keep moving in the scaffolding of my project. The concept of In the GitHunt example I can see the methods of the models always return new instances, and not data of a given instance. That's work for I would expect the methods of a |
The Author's model vs repository Let's take the following const author = {
posts: (author, args, context) => {
...
},
}; In my experiments here, the const author = {
posts: (author, args, context) => {
return author.getPosts();
},
}; const author = {
posts: (author, args, context) => {
return (new context.models.Author(author)).getPosts();
},
}; However, if I understand correctly the way Apollo goes, I am supposed to call it like that: const author = {
posts: (author, args, context) => {
return context.models.Post.find({ authorId: author.id })
},
}; ... and that's not a |
Yeah, perhaps the wording is wrong. In Rails they tended to get mixed together. Repository seems really vague though, is there something else we could call it that would be clearer? |
I don't really know, I haven't had the need to use this pattern for a while. Could it be called More important than the naming is to implement So, this is how it could be: const john = new Author({firstName: 'John', lastName: 'Doe'}); // Author
const posts = john.getPosts(); // [Post]
...
const johnAgain = Author.repository.findOne(john._id); // Author
const postsAgain = Post.repository.find({ 'author._id': john._id}); // [Post] |
Oh! Now I remember. Django calls it manager and use the term |
Nice! My only problem with this approach is that this will mean that a model needs to be aware of multiple backends. For example, you can imagine a situation where It would be odd for me if |
will it need? the connector will still be there abstracting it. class Author {
...
getPosts() {
return this.posts || Post.objects.find({ 'author._id': this._id});
}
} It goes this way: Model --> Manager(or Repository) --> Connector --> Backend |
I think |
I think it's a matter of preference. We can change the example to be more model-like if it is helpful. |
I made a proof of concept that I would like to share with you. It is object/model oriented and remove completely the use of My main concern while making this was to keep a clear separation of business logic and application. Business is in All the models have a default manager which receives a default connector as well. Both could be overwritten or expanded, this way e.g. a To keep separation of concerns I assumed the mongodb API as standard to communicate between all managers and connectors. We would need to review this point. The example code can be found in the perfect graphql starter repository and the helpers are inside the brand new graph-object package. |
@stubailo @helfer is there already any sample of a model - connector implementation as you imagine it? Is the connector models design description the latest approach you would recommend?I'm asking because in the samples are several different approaches implemented eg: In the apollo-server-tutorial resolvers directly access the orm models of sequelize. Thereas the the githunt-api resolvers use knex through a model. Is the intention here to make connecters fully exchangeable and standardized?In the githunt example also the database connectors are hard coded into the models. Thereas the connector models design description foresees a more generic approach. Connectors are injected into the model. If every connector would implement the same functionality they would be exchangeable. However I think this may be a lot of work on some connecters and may much easier achieved on business model logic. |
The Repository Pattern should be used to communicate with its Model. So, if you need more than this, you can use the Service Pattern. A service can use other services without being worried about the model. This can scale well, however if the app is small you might over-engineering the app with many layers of abstraction. |
I am confused with the use of the words
models
andconnectors
among all the docs of the Apollo project. It is hard for me to figure out what is currently implemented and what is just a draft proposal.Apparently, right now, the term
connector
actually refers to the combination of what in the future will be split into themodel
and theconnector
concepts. Am I correct?The text was updated successfully, but these errors were encountered: