
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
graphql-to-sql
Advanced tools
Use GraphQL as the source of truth for schema. The SQL schema script is derived from the GraphQL type definitions.
Unify your SQL schema and your GraphQL schema. Manage schema from a single source of truth.
node generate-sql.js
// generate-sql.js
import sqlDirective from 'graphql-to-sql'
import gql from 'graphql-tag'
const {
sqlDirectiveTypeDefs,
generateSql
} = sqlDirective('sql')
const typeDefs = gql`
directive @sql (
unicode: Boolean
auto: Boolean
default: String
index: Boolean
nullable: Boolean
primary: Boolean
type: String
unique: Boolean
generated: String
constraints: String
) on OBJECT | FIELD_DEFINITION
# See graphql-directive-private
directive @private on OBJECT | FIELD_DEFINITION
type User @sql(unicode: true) {
userId: String @sql(type: "BINARY(16)", primary: true)
uniqueColumn: Int @sql(unique: true)
databaseOnlyField: Int @sql @private
graphqlOnlyField: String
posts: [Post]
}
type Post {
postId: Int @sql(primary: true, auto: true)
userId: String @sql(type: "BINARY(16)", index: true)
content: String @sql(type: "VARCHAR(300)", unicode: true, nullable: true)
likes: Int @sql
dateCreated: String @sql(type: "TIMESTAMP", default: "CURRENT_TIMESTAMP")
}
type UserPair @sql(constraints: "UNIQUE(parentUserId, childUserId),\\n FOREIGN KEY (parentUserId) REFERENCES User(userId)") {
userPairId: String @sql(type: "BINARY(16)", primary: true)
parentUserId: String @sql(type: "BINARY(16)", index: true)
childUserId: String @sql(type: "BINARY(16)", index: true)
}
`
const sql = generateSql({typeDefs: [typeDefs, sqlDirectiveTypeDefs]}, {
databaseName: 'public', // for postgres, keeping public is recommended.
tablePrefix: 'test', // or test_
dbType: 'mysql' // or postgres
})
console.log('sql', sql)
The script above will produce this string:
CREATE TABLE IF NOT EXISTS `public`.`test_User` (
`userId` BINARY(16) NOT NULL,
`uniqueColumn` INT NOT NULL UNIQUE,
`databaseOnlyField` INT NOT NULL,
PRIMARY KEY (`userId`)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `public`.`test_UserPair` (
`userPairId` BINARY(16) NOT NULL,
`parentUserId` BINARY(16) NOT NULL,
`childUserId` BINARY(16) NOT NULL,
PRIMARY KEY (`userPairId`),
INDEX `PARENTUSERIDINDEX` (`parentUserId` ASC),
INDEX `CHILDUSERIDINDEX` (`childUserId` ASC),
UNIQUE(parentUserId, childUserId),
FOREIGN KEY (parentUserId) REFERENCES User(userId)
);
CREATE TABLE IF NOT EXISTS `public`.`test_Post` (
`postId` INT NOT NULL AUTO_INCREMENT,
`userId` BINARY(16) NOT NULL,
`content` VARCHAR(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,
`likes` INT NOT NULL,
`dateCreated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`postId`),
INDEX `USERIDINDEX` (`userId` ASC)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Also see main-test.ts for a working example.
@sql()
:ON OBJECT:
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
as table_option.ON FIELD_DEFINITION:
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
.@sql(generated: "(data->>'$.test')")
. See main-test.ts for more examples.MySQL and PostgreSQL are supported.
FAQs
Use GraphQL as the source of truth for schema. The SQL schema script is derived from the GraphQL type definitions.
We found that graphql-to-sql demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.