JSON Schema to GraphQL schema converters

16,717

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
16,717

Related videos on Youtube

Rax Wunter
Author by

Rax Wunter

Updated on June 04, 2022

Comments

  • Rax Wunter
    Rax Wunter almost 2 years

    Are there any adapters which are converting JSON Schema schemas (e.g from Swagger) to GraphQL schemas? There is even an official article about wrapping around REST http://graphql.org/blog/rest-api-graphql-wrapper/, but usually REST already described and Swagger is the most popular format for it. Wouldn't like to write it by my own if there is already existing implementation.

    • Joshua Cook
      Joshua Cook almost 3 years
      Perfectly reasonable question.
  • yucer
    yucer about 5 years
    But that's only for the types right ? The full contract (message parameters, etc.) can be mapped ?
  • Rax Wunter
    Rax Wunter about 5 years
    Everything which is needed to make API working is mapped
  • Marcin Rzepecki
    Marcin Rzepecki about 3 years
    Does anyone know if there is a similar tool for Java? I looked all over the web and found nothing.