Convert Graphql to SQL?

We have existing SQL Server database and we are using C#. Lets say our mobile client send a graphql to server. How can I convert this SQL, so that my client get the data what he expect?

Upvotes: 27

Views: 24483

Answers (9)

extinctsion
extinctsion

Reputation: 93

I have been working with a backend of mobile app, We used postgresQL (sql) as our database, .NET 8 (C#) as our backend API which implemented GraphQL APIs.

There is a nuget library called hotchocolate in .NET which provides GraphQL implementation in .NET (C#). It was very easy to implement for our backend, using hotchocolate you will be able to create schema stitching between your client and the backend servers(You can also stitch an existing REST APIs backend with GraphQL or any other backend using schema stitching )

Hence, Yes using GraphQL APIs you will be able to send data to database(sql) and it might be a solution you can choose for your backend.

If you are interested in GraphQL using hotchocolate, you can check their official website. https://chillicream.com/docs/hotchocolate/v13

Upvotes: 0

David Ventimiglia
David Ventimiglia

Reputation: 61

You can convert a GraphQL query to SQL for Microsoft SQL Server using Hasura. Hasura has an open-source tool that compiles GraphQL queries to a suitable representation for a given data source. For relational databases, that will be SQL, and for a particular relational database, that will be the SQL dialect for that database. Of course, typically Hasura is used as the GraphQL server itself, in which case it will also execute the query and respond with the results, modulo Non-Functional Requirements (NFRs) like caching, horizontal scalability, security, and observability. Having said that, if you prefer to execute the SQL on your own, that can be arranged. Among Hasura's API endpoints, there is an Explain endpoint that can be used to obtain the SQL that Hasura generates.

Full disclosure: I work for Hasura. Therefore, I am not encouraging you to use any particular solution. I'm just giving one answer (there are others) to the question, "How can I convert GraphQL to SQL for Microsoft SQL Server."

Upvotes: 2

Ozymandias
Ozymandias

Reputation: 2778

I suggest that you look into implementing a C# version of the Join Monster repository https://github.com/join-monster/join-monster

It attempts to convert a client-provided GraphQL query into the most efficient possible SQL query. It does not currently support many-to-many relationships or union types (polymorphic associations), but those goals are part of the repository's road map.

EDIT 2019

There now exist additional options such as Hasura and Postgraphile. Also Prisma may eventually be available in C#

Upvotes: 15

Dmitriy
Dmitriy

Reputation: 124

Well, GraphQL it's just a syntax or query format for API calls as guys wrote previously. Btw, one of the lib that was mentioned above is GraphQL .NET and I know the component that uses it to do direct calls to all popular dbs (like MSSQL, MySQL, PostgreSQL, or even ElasticSearch) - just describe datatables/fields in JSON file and ready to use. ping me in comment or it's easily googling (NReco.GraphQL .net api sql)

Upvotes: 0

Christian Findlay
Christian Findlay

Reputation: 7712

Easy. Use GraphQL.EntityFramework.

https://github.com/SimonCropp/GraphQL.EntityFramework

Upvotes: 2

Vadorequest
Vadorequest

Reputation: 18079

The most mature open source implementation I've found for doing this is https://join-monster.readthedocs.io/en/latest/

What Is It? Join Monster is a query planner between GraphQL and SQL for the Node.js graphql-js reference implementation. It's a function that takes a GraphQL query and dynamically translates GraphQL to SQL for efficient, batched data retrieval before resolution. It fetches only the data you need - nothing more, nothing less.

Why? It solves the problem of making too many database queries, i.e. the "round-trip" problem or "N+1" problem, where the round-trips are requests for data over the TCP/IP stack between your API server and your SQL database. Think of it as an alternative to Facebook's DataLoader, but with more specificity toward SQL, making it more powerful and simpler to use with SQL.

It is NOT a tool for automatically creating a schema for your GraphQL from your database or vice versa. You retain the freedom and power to define your schemas how you want. Join Monster simply "compiles" a GraphQL query to a SQL query based on the existing schemas. It fits into existing applications and can be seamlessly removed later or used to varying degree. It is a little opinionated, but not a full ORM.

It looks exactly like what you're looking for.


Also, https://github.com/graphile/postgraphile (which is mentioned as an answer here) is another possibility. It basically bring GraphQL capabilities to a Postgre DB.

Both solutions may fit your needs.

Upvotes: 2

Eli Goldberg
Eli Goldberg

Reputation: 41

You could also take a look at an implementation such as Postgraphile

It basically inspects each GraphQL request and combine the nested fields' resolvers into a single SQL join, which then resolves back recursively.

Upvotes: 4

LordDave
LordDave

Reputation: 1128

No, unfortunately you can't convert GraphQL to SQL.

If your client is sending you a request in GraphQL, you have no choice but to create your GraphQL server and query the database from there.

GraphQL is not SQL. While SQL is database query language, GraphQL is query language from client side to middleware.

Upvotes: -5

Alex Anderson
Alex Anderson

Reputation: 670

GraphQL and SQL, while sounding similar, solve different problems. SQL is used to query a database directly. GraphQL is used to query data sources of any kind, such as databases (through SQL or client libraries), APIs, and static files. GraphQL can be compared to REST or ad-hoc API endpoints.

One solution would be to create the GraphQL implementation yourself. GraphQL.org has a lot of great information about how to implement and use a GraphQL server. If that's too much work, you could piggy back off of this project: GraphQL .NET

Also consider looking at other GraphQL implementations, such as ApolloStack. If you can have your GraphQL server separate from your .NET server, you could use ApolloStack or another Javascript GraphQL server to host your data.

Upvotes: 19

Related Questions