From SQL tables to a GraphQL schema
Starting a GraphQL API on top of an existing database usually means writing a type for every table by hand. Paste the CREATE TABLE statements instead and you get those types, with nullability taken from NOT NULL and the scalar types picked from the column types. Add a SELECT and it also becomes a GraphQL query, with the WHERE conditions as arguments. Everything happens in your browser.
How to Convert SQL to GraphQL
Paste your SQL
Paste CREATE TABLE statements, a SELECT query or both, upload a .sql file, or click Sample to see a table and a query together. Here is a small table and a query that reads it:
CREATE TABLE sims ( iccid BIGINT PRIMARY KEY, zip CHAR(5) NOT NULL, monthly_fee DECIMAL(6, 2), active BOOLEAN NOT NULL ); SELECT iccid, monthly_fee FROM sims WHERE zip = '00501' AND monthly_fee <= 14.50 LIMIT 10;
Read the GraphQL
The schema appears on the right about half a second after you stop typing: custom scalars, a type per table, the Query type and, for each SELECT, a query with its arguments. Read the # Notes lines for anything that was left out or guessed. For the SQL above the output is:
# Notes:
# - Decimal is a custom scalar declared below, used for argument monthly_fee_lte, Sim.monthly_fee.
# - The Query type is a starting point: a list field per table with limit and offset, and a lookup by primary key. Rename or remove fields to fit your API.
# Query
query GetSims {
sims(zip: "00501", monthly_fee_lte: 14.50, limit: 10) {
iccid
monthly_fee
}
}
# Schema
"A SQL DECIMAL or NUMERIC value. Implement it on your server as a decimal scalar that keeps every digit, for example by sending it as text such as \"14.50\"."
scalar Decimal
type Sim {
iccid: ID!
zip: String!
monthly_fee: Decimal
active: Boolean!
}
type Query {
sims(
limit: Int
offset: Int
zip: String
"SQL filter: monthly_fee <= 14.50"
monthly_fee_lte: Decimal
): [Sim!]!
sim(iccid: ID!): Sim
}
BIGINT PRIMARY KEY became ID!, DECIMAL became the declared Decimal scalar, and the filter values "00501" and 14.50 were copied exactly as written.
Copy or download
Copy the result into your server's type definitions, or download it as schema.graphql. Then write the resolvers that run your SQL. Going the other way, or working with other stores? Try GraphQL to SQL, MongoDB to GraphQL, JSON to GraphQL or the GraphQL Formatter.
Frequently Asked Questions
What SQL can I paste?
CREATE TABLE statements and SELECT queries, one or many, separated by semicolons. A whole schema dump works too: CREATE INDEX, INSERT, SET and similar lines are skipped and listed in a note, and an ALTER TABLE … ADD PRIMARY KEY (the way pg_dump writes keys) is applied to its table. MySQL backticks, PostgreSQL double quotes, SQL Server [brackets], comments and PostgreSQL $$ bodies are all read properly.
How are column types mapped?
SMALLINT, INT and SERIAL become Int; BIGINT and BIGSERIAL become a Long scalar; FLOAT, REAL and DOUBLE become Float; DECIMAL, NUMERIC and MONEY become a Decimal scalar; BOOLEAN becomes Boolean; CHAR, VARCHAR, TEXT, UUID and ENUM become String; DATE, TIMESTAMP and TIME become Date, DateTime and Time scalars; JSON becomes a JSON scalar and PostgreSQL arrays become lists. NOT NULL adds the !, a single-column PRIMARY KEY becomes ID!, and a MySQL COMMENT turns into the field's description. Every custom scalar the schema uses is declared in it, with a line saying how to implement it.
Why is BIGINT a Long scalar instead of Int?
GraphQL's Int is a 32-bit number that stops at 2147483647, so a BIGINT column holding an ID like 8901240544102066246 doesn't fit. Long (and BigInt for BIGINT UNSIGNED) tells your server to treat the value as a bigger integer, ideally sent as text. Values in your WHERE clause are copied exactly as you wrote them, so 8901240544102066246 keeps every digit, 14.50 stays 14.50 and '00501' stays the string "00501".
What happens to the WHERE clause?
Each condition joined by AND becomes an argument on the query field: = keeps the column name, and the others get a suffix such as price_gte, status_in, name_like or deleted_at_is_null. ORDER BY becomes an orderBy list with ASC or DESC, and LIMIT and OFFSET become limit and offset. Conditions joined by OR, comparisons between two columns and function calls can't be plain arguments, so they are left out and named in a # Notes line at the top.
Does it write resolvers or mutations?
No. You get type definitions, a starting Query type (a list field per table and a lookup by primary key) and, for each SELECT, a GraphQL query that calls it. Your server still needs resolvers that run the SQL. INSERT, UPDATE and DELETE would be mutations, which this page doesn't write.
What can't it convert?
JOINs, UNION, subqueries, window functions and CASE stop with a message, because they have no single-field GraphQL form; write one SELECT per table instead. GROUP BY, HAVING and DISTINCT are left out with a note. Foreign keys stay plain fields with a "References" description, so add nested fields for related types yourself.
Is my SQL sent anywhere?
No. The conversion runs in your browser, the SQL is not uploaded, and nothing is kept: a reload starts with an empty editor.
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