Skip to content
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

Reusing models, is it savvy? #78

Closed
rhymes opened this issue Apr 5, 2018 · 5 comments
Closed

Reusing models, is it savvy? #78

rhymes opened this issue Apr 5, 2018 · 5 comments

Comments

@rhymes
Copy link

rhymes commented Apr 5, 2018

This is not strictly an issue, just a question about usage.

I saw in the doc you can point, through types.json, to a model struct and define it yourself.

I was wondering if it's a good idea to point it, and hence use, to the same models structs we're already using for the domain logic.

The app already has a REST interface and we want to add a separate GraphQL interface to it, so it would be very handy not to have to duplicate the domain objects across the two layers.

Do you see any reasons not to?

Thanks

@vektah
Copy link
Collaborator

vektah commented Apr 5, 2018

Model generation is super handy for input objects and enums, which probably only exist in the graph.

As for other types, we bind directly to our domain objects. For example we have a package for our user microservice that knows how to fetch users and returns its own User type. The user type has a few utility methods that know how to check roles etc and the graph binds directly to it (and only the fields that should be exposed).

On one hand, if you bind to the model you risk someone changing it and doesn't realize they are changing the API, but custom types, enums, and a good test suite help here.

On the other hand, if you don't bind to the model you end up writing a lot of boilerplate which can easily fall out of date or introduce typos but again a good test suite helps.

Everything is strictly typed with the generated code (unlike the reflection based libraries), so breaks should be easy to pick up. I would recommend binding to your existing models if you have them until you cant for whatever reason.

Maybe others have some thoughts?

@rhymes
Copy link
Author

rhymes commented Apr 5, 2018

Yes, I agree. I would definitely define input objects and enums relating to the GraphQL business logic in there.

Glad to see that domain models can still be kept outside. The code generator looks great, thanks for the effort!

@Teddy-Schmitz
Copy link

@vektah I saw you mentioned before not to bind directly to your DB models. Can you elaborate on why you wouldn't want to do that?

@vektah
Copy link
Collaborator

vektah commented Apr 6, 2018

I don't think that was me. Maybe #21?

@Teddy-Schmitz
Copy link

o lol, sorry the generic github user icons look so alike i didnt see that it was a different user. Thanks.

@vektah vektah closed this as completed Apr 18, 2018
cgxxv pushed a commit to cgxxv/gqlgen that referenced this issue Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants