DEV Community

masem
masem

Posted on

Introducing gq-to-sql: Convert Graph-Style URL Queries into SQL

As a developer working with Graph-like APIs and SQL databases, I often wished for an easy way to parse $filter, $select, and similar query strings and convert them into SQL. So I built gq-to-sql β€” a lightweight, safe utility for just that.

Features

βœ… Parse Graph-style query strings: $filter, $select, $orderby, $top, $skip

βœ… Safe SQL output with parameters (?)

βœ… Nested condition support ((age gt 20 or age lt 10))

Example

Input:

?$filter=age gt 20 and city eq 'Vienna'&$select=name,age&$orderby=age desc

Output:

SELECT name, age FROM users WHERE age > ? AND city = ? ORDER BY age desc

Try It Out

npm install gq-to-sql

πŸ‘‰ View it on GitHub: https://github.com/masem1899/graphql-to-sql
πŸ‘‰ Website: https://masem.at/s/devto
πŸ‘‰ Blog Article: https://www.masem.at/blog/introducing-gqtosql-graphstyle-url-filtering-for-sql

Top comments (3)

Collapse
 
xwero profile image
david duymelinck

I understand the convenience of a tool like that. But as a backend specialized developer I'm not comfortable with directly linking user input with SQL. it is a whole lot of power you give to the people that visit your website.

Is it possible you didn't make the repository public? I get a 404 when I click on the link. And i don't see it on your profile.

Collapse
 
masem profile image
masem

Hi, it's public now.

Collapse
 
nevodavid profile image
Nevo David

pretty cool honestly, stuff like this saves headaches for me- ever run into weird edge cases with complex nested filters or does it handle those fine?