Graphql-js: Build schema with `buildSchema` or programmatically?

Created on 18 Nov 2016  路  6Comments  路  Source: graphql/graphql-js

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.

Most helpful comment

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

All 6 comments

Very interesting question.

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

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.

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 ...

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

Thanks @stubailo!

@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. .

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ablbol picture ablbol  路  4Comments

adriano-di-giovanni picture adriano-di-giovanni  路  3Comments

sudheerj picture sudheerj  路  3Comments

vslinko picture vslinko  路  4Comments

scf4 picture scf4  路  3Comments