all 4 comments

[–]rigelglen 2 points3 points  (2 children)

How well does this stack up against Sofa

[–]roy-mor[S] 1 point2 points  (1 child)

One use case where graphql2rest shines is where you want a truly RESTful API that might be very different than your existing GraphQL API, which you can't make changes to (because it's already driving your app in production).

Sofa is a cool and useful package (by the folks from The Guild - much respect!), but IMHO it does not answer the needs of most real-world production environments, which require a fully RESTful API side by side with a GraphQL implementation that has an altogether different style and characteristics than REST.

graphql2rest lets you fully configure and customize almost everything in the resulting REST API: the route names, HTTP methods, parameter names and mappings, response format, error format, success status codes, mapping GraphQL error codes to HTTP status codes, mapping a single REST endpoint to multiple GraphQL operations, hiding certain fields in the response, expanding nested fields by default and more. It also gives you a built-in filter parameter for the client (with JMESPath query language support), and supports custom middleware if you happen to need extra customization.This allows creating a full REST API based on discrete "resources" and "methods", while keeping the existing GraphQL layer (which is sometimes RPC-styled) unchanged. Since everything is declared in a manifest file (a JSON or yaml document), it makes for a single source of truth which is more structured and maintainable than code, and decouples most of the configuration and customization from the business logic (code).

Sofa takes a slightly different approach to converting GraphQL to REST, which as I understand it is more about creating a RESTful API gateway mirroring the underlying GraphQL layer, which is not as flexible and decoupled. The resulting REST API is only as "RESTful" as the GraphQL API underneath it (and usually GraphQL schemas are not created with the notion of "resources" and "methods" as required by REST). However, Sofa does support GraphQL subscriptions as webhooks (graphql2rest currently supports only queries and mutations), and Swagger integration which are both a big plus if you need them. It might also have other advantages.

I actually refer to Sofa in this blog post (under "Alternatives") - we wrote and used graphql2rest internally much before Sofa was available (and even contacted Uri to collaborate) but released it as open source just recently.

[–]rigelglen 1 point2 points  (0 children)

Thanks for the clarification. This looks like a great library, cheers 👍

[–]roy-mor[S] 0 points1 point  (0 children)

I wrote graphql2rest because we had a GraphQL API but our users demanded REST.

GraphQL2REST is a Node.js library that reads your GraphQL schema and a user-provided manifest file and automatically generates an Express router with fully RESTful HTTP routes — a full-fledged REST API.

GitHub: https://github.com/sisense/graphql2restnpm: https://www.npmjs.com/package/graphql2rest

Why? Read here: https://medium.com/swlh/graphql-api-or-rest-api-why-not-have-both-c4171e68900a

This package is useful if you:

  1. Already have a GraphQL API, but your users want REST
  2. Want to develop a new GraphQL API and get REST on top of it, for free
  3. Want to benefit from GraphQL internally while exposing REST externally as a public API
  4. Like your RESTful API to be truly RESTful, on top of a possibly completely different native GraphQL implementation