Loading GraphQL to SQL Converter...
Please wait a moment

Turn GraphQL Queries into SQL SELECT Statements

Have a GraphQL query and want to see the SQL behind it? Paste it above. Field selections become columns, arguments become WHERE conditions, and nested fields get their own SELECT with a comment saying how it is linked to the parent table. The query is read in your browser.

How to Convert GraphQL to SQL — Step-by-Step

Step 1

Input Your GraphQL Query

Copy a query from your GraphiQL playground, Apollo Explorer or your application code.

•
Paste: the SQL appears about half a second after you stop typing
•
Upload: Select a .graphql or .gql file
•
Sample: a query with a filter, a sort, a limit and a nested field
Step 2

Read the SQL

This is how the GraphQL parts are mapped:

•
Root fields: become the table in the FROM clause
•
Field selections: become SELECT columns, aliases become AS
•
Arguments: status: "active" becomes WHERE status = 'active', id: [1, 2] becomes id IN (1, 2), limit: 25 becomes LIMIT 25
•
Nested fields: become a separate SELECT linked through an assumed column such as user_id, with a comment saying so
Step 3

Copy or Download

•
Copy: Paste into your SQL client or ORM
•
Download: Save as .sql

Frequently Asked Questions

How does a GraphQL query turn into SQL?

Each top-level field is read as a table, and the fields you select inside it become the columns, so users(status: "active") { id name } becomes SELECT id, name FROM users WHERE status = 'active'. An alias such as fullName: name becomes name AS fullName. Fragments and inline fragments are expanded into the fields they contain, and comments are ignored.

Which arguments become WHERE conditions?

A plain value is compared with =, a list becomes IN (…), and null becomes IS NULL. An object of operators like age: { gte: 18, lt: 65 } becomes a range, and contains, startsWith, endsWith, like, in, nin and isNull work too, in plain, Hasura (_gte) or Prisma spelling. A where or filter argument can hold several columns at once, with AND, OR and NOT for grouping. orderBy and sortBy become ORDER BY, limit and first become LIMIT, offset and skip become OFFSET.

What happens to nested fields like orders inside users?

SQL has no nested results, so a nested selection is written as a second SELECT for its own table. It is tied to the parent rows with an IN (…) subquery on a guessed column: orders under users assumes orders.user_id holds users.id. A comment above the query says which names were assumed, so you can change them to match your schema or turn it into a JOIN.

Is the SQL safe to run, and are numbers kept exactly?

String values are put in single quotes with any quote inside doubled, so a name like O'Brien or a value such as x'); DROP TABLE y; -- stays a plain string. Column names that are SQL keywords, like select or order, go in double quotes, and orderBy text is checked before it is used. Numbers are copied as you typed them, so an ID like 8901240544102066246 stays a bare number with every digit, 14.50 stays 14.50 and "00501" stays the string '00501'.

Which databases does the output work with?

The output is standard SQL. It runs as written in PostgreSQL and SQLite and needs little or no change elsewhere. MySQL users should know two things: names in double quotes need ANSI_QUOTES or backticks, and a string containing a backslash is read differently unless NO_BACKSLASH_ESCAPES is on; the output adds a comment when either applies.

Can it convert mutations or queries with variables?

Only queries. A mutation changes data and would need INSERT, UPDATE or DELETE, so it is refused with a message. A variable like $id has no value inside the query text, so you are asked to put the real value in its place first. The query is read with the graphql-js library in your browser and is not sent anywhere.