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

Build schema with buildSchema or programmatically? #596

Closed
xpepermint opened this issue Nov 18, 2016 · 6 comments
Closed

Build schema with buildSchema or programmatically? #596

xpepermint opened this issue Nov 18, 2016 · 6 comments

Comments

@xpepermint
Copy link

I built my first GraphQL server months ago. Back then I followed @leebyron's proposal: for more complex schema we recommend building the schema programmatically. After a few months I visited graphql.org website again and I saw that the first example on how to build a schema is using the buildSchema method and is not done programmatically.

I wonder ... is using buildSchema a preferred way for creating a schema? I still find the programmatic way easier for production-level products.

@langpavel
Copy link
Contributor

langpavel commented Nov 21, 2016

Very interesting question.

If you can, you should use buildSchema. If you feel it will limits you then you should create schema programmatically.

@jamesgorman2
Copy link
Contributor

jamesgorman2 commented Nov 21, 2016

I'm in the process of putting together a module based type/schema repository. It's goal is to allow service teams to manage their own types, and put back-references into upstream types using the extension syntax. Still building the graph parser, then there's unmarshalling, and there's no readme, but the tests for the config should give you an idea as to how the bits are specified.

Be good to know if this is the sort of thing you're looking for.

@xpepermint
Copy link
Author

Thanks @langpavel for your reply, it helps! I've been playing with this new approach this weekend and I came up with a very nice solution which I'd like to share with you.
For months I've been working on a package called contextable.js where the main goal is to help us write code within a GraphQL resolver function (or Express route action function) in a way that we write a Rails/ActiveRecord model (input data validation, error hendling, less code etc).
I think it was yesterday when I found an example in the official GraphQL documentation where a rootValue resolver is using classes. I realised that the contextable.js, which btw is a sort of unopinionated ActiveRecord model system for javascript, can directly be used as a GraphQL resovler - you just create an instance of a model (using contextable) and pass it as a rootValue - eureka!
I've updated the contextable.js documentation on how to use it with GraphQL. There is also an example app which includes that. I'm sure this will help others.

@jamesgorman2 thanks for sharing! As I can see from your tests, you found pretty similar solution to what I described above! This only confirms that this is the right way to do it (using models/classes) and using buildSchema now seams to be more natural approach - very nice!

Thank you both for directions! Closing ...

@stubailo
Copy link
Contributor

We have developed a package that makes it easy to use the schema language while also writing resolvers: http://dev.apollodata.com/tools/graphql-tools/index.html

@xpepermint
Copy link
Author

Thanks @stubailo!

@sepehr500
Copy link

@stubailo Can you point me to a large project that uses buildSchema? The Apollo documentation does not show how resolvers are modularized along with the schema strings, and I am not convinced buildSchema is better than building the schema programmatically for large projects. .

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

5 participants