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
Input Your GraphQL Query
Copy a query from your GraphiQL playground, Apollo Explorer or your application code.
.graphql or .gql fileRead the SQL
This is how the GraphQL parts are mapped:
status: "active" becomes WHERE status = 'active', id: [1, 2] becomes id IN (1, 2), limit: 25 becomes LIMIT 25user_id, with a comment saying soCopy or Download
.sqlFrequently 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.
Related Tools
GraphQL Query Formatter
Format and beautify GraphQL queries and schemas with syntax highlighting
GraphQL Schema Validator
Validate GraphQL schemas with syntax and structure checking
GraphQL Fixer
Fix broken GraphQL queries and schemas automatically, repair syntax errors and malformed types
GraphQL to TypeScript
Generate TypeScript types from GraphQL schemas and queries
GraphQL Minifier
Compress and optimize GraphQL queries for production
GraphQL to JSON Schema
Convert GraphQL schemas to JSON Schema format