Convert Graphql to SQL?
Asked Answered
S

8

26

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?

Semipermeable answered 22/9, 2016 at 15:47 Comment(3)
One of the possible soultion could be graphql.net + Entity Framework.Riverine
@Riverine any exampleSemipermeable
Are you using Entity Framework? Please provide one example for you Graphql query so I can give you an exact example of the conversion.Ima
I
19

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.

Irate answered 22/9, 2016 at 16:16 Comment(3)
alex the question is simple. How can I get my data already is in my SQL SERVER database if client send me data in graphql format. Is this is possible or not?Semipermeable
Here's an example GraphQL server that pulls from SQL using Node.js: github.com/apollostack/GitHunt-API/tree/master/api/sqlVying
Node.js has many more libraries in the GraphQL ecosystem, so building a Node service would give you many more options. Join Monster is a library that tries to generate SQL queries from a given GraphQL request and schema definition.Premature
C
15

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#

Claudette answered 4/2, 2017 at 9:16 Comment(2)
Thanks for the mention! Join Monster does in fact support many-to-many relationships. The data you can get from it is limited, but that is currently being worked through.Premature
I didn't know! Currently I'd really like to use it, but I'm working with Apollo Server so I'm thinking I'll either need to wait until Apollo is supported or possibly help in getting it supported.Claudette
A
4

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.

Allomorphism answered 31/10, 2018 at 20:16 Comment(0)
B
2

Easy. Use GraphQL.EntityFramework.

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

Byington answered 19/7, 2020 at 4:45 Comment(0)
L
2

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

Limit answered 29/7, 2024 at 20:9 Comment(0)
E
1

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.

Explore answered 26/5, 2019 at 20:27 Comment(1)
How is this answer different from above? Both library have been mentioned.Terina
L
0

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)

Liquorish answered 19/3, 2019 at 19:49 Comment(0)
M
-5

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.

Mortician answered 12/10, 2016 at 9:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.