JSON Schema to GraphQL schema converters

Meanwhile several tools have been implemented to convert a JSON Schema to GraphQL Schema:

  • json-schema-to-graphql-types
  • jsonschema2graphql
  • json-schema-to-graphql-types-decorated

Also I wrote a library, which allows you to wrap your existing REST API if you use Swagger.

https://github.com/yarax/swagger-to-graphql

It basically does Swagger schema mapping to GraphQL types.

And there is an article about this approach and the library https://medium.com/@raxwunter/moving-existing-api-from-rest-to-graphql-205bab22c184


I actually put some time into trying this out a few months ago. You can read the my post detailing the results here: https://medium.com/apollo-stack/will-graphql-replace-rest-documentation-f1a55092ef9d#.m50im46o0

After looking at a lot of the Swagger schemas available online, I think that Swagger or similar API description languages can be a good starting point for defining a GraphQL schema, but they often don't contain enough information to generate a schema on their own. Specifically, there is usually not enough data about relationships between objects.

If you want to start from a JSON-formatted schema description, all you need to do is write some code that loops over your different data types in Swagger, and generate GraphQLObjectType objects. You can see a simple approach to this in the example repository for the blog post I linked above: https://github.com/apollostack/swapi-rest-graphql/blob/951e50ec29732c93e7aa0bc6880210fdd1816a2f/schema.js#L28

Basically, you are just converting one format of data into another, and then you need to add some relationships between the data (foreign keys, IDs, and such), and add some root queries to create an entry point. In the case of a REST API, it often makes sense to have your single and multiple resource endpoints act as your root query fields.