{"id":13689424,"url":"https://github.com/taylorgoolsby/graphql-directive-sql","last_synced_at":"2025-10-27T18:30:42.452Z","repository":{"id":33425492,"uuid":"158330066","full_name":"taylorgoolsby/graphql-directive-sql","owner":"taylorgoolsby","description":"Unify your SQL schema and your GraphQL Schema. Use GraphQL SDL as the lingua franca to define your data requirements.","archived":false,"fork":false,"pushed_at":"2023-01-04T21:43:48.000Z","size":1043,"stargazers_count":30,"open_issues_count":12,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-09-28T21:41:17.079Z","etag":null,"topics":["code-generator","directive","generate","generator","graphql","graphql-schema","graphql-schema-language","graphql-to-sql","language","mysql","schema","sdl","sql"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/taylorgoolsby.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-20T04:18:52.000Z","updated_at":"2023-05-12T04:31:43.000Z","dependencies_parsed_at":"2023-01-15T00:52:05.740Z","dependency_job_id":null,"html_url":"https://github.com/taylorgoolsby/graphql-directive-sql","commit_stats":null,"previous_names":["taylrun/graphql-directive-sql","taylorgoolsby/graphql-directive-sql","mgs485/graphql-directive-sql"],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taylorgoolsby%2Fgraphql-directive-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taylorgoolsby%2Fgraphql-directive-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taylorgoolsby%2Fgraphql-directive-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taylorgoolsby%2Fgraphql-directive-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taylorgoolsby","download_url":"https://codeload.github.com/taylorgoolsby/graphql-directive-sql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219860762,"owners_count":16556011,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-generator","directive","generate","generator","graphql","graphql-schema","graphql-schema-language","graphql-to-sql","language","mysql","schema","sdl","sql"],"created_at":"2024-08-02T15:01:47.303Z","updated_at":"2025-10-27T18:30:37.107Z","avatar_url":"https://github.com/taylorgoolsby.png","language":"TypeScript","readme":"# graphql-directive-sql\n\nUnify your SQL schema and your GraphQL Schema. Use GraphQL SDL as the lingua franca to define your data requirements.\n\nGiven a GraphQL schema defined in SDL, this function will output a schema script which, when ran on your database, will create all the tables in your database.\n\n## Example\n\n`node generate-sql.js`\n```js\n// generate-sql.js\nconst gql = require('graphql-tag')\nconst {\n  makeSqlSchema,\n  getSchemaDirectives\n} = require('graphql-to-sql')\n\nconst typeDefs = gql`\n  directive @sql (\n    unicode: Boolean\n    constraints: String\n    auto: Boolean\n    default: String\n    index: Boolean\n    nullable: Boolean\n    primary: Boolean\n    type: String\n    unique: Boolean\n  ) on OBJECT | FIELD_DEFINITION\n\n  # See graphql-directive-private\n  directive @private on OBJECT | FIELD_DEFINITION\n\n  type User @sql(unicode: true) {\n    userId: String @sql(type: \"BINARY(16)\", primary: true)\n    uniqueColumn: Int @sql(unique: true)\n    databaseOnlyField: Int @sql @private\n    \n    graphqlOnlyField: String\n    posts: [Post]\n  }\n\n  type Post {\n    postId: Int @sql(primary: true, auto: true)\n    userId: String @sql(type: \"BINARY(16)\", index: true)\n    content: String @sql(type: \"VARCHAR(300)\", unicode: true, nullable: true)\n    likes: Int @sql\n    dateCreated: String @sql(type: \"TIMESTAMP\", default: \"CURRENT_TIMESTAMP\")\n  }\n  \n  type UserPair @sql(constraints: \"UNIQUE(parentUserId, childUserId),\\\\n  FOREIGN KEY (parentUserId) REFERENCES User(userId)\") {\n    userPairId: String @sql(type: \"BINARY(16)\", primary: true)\n    parentUserId: String @sql(type: \"BINARY(16)\", index: true)\n    childUserId: String @sql(type: \"BINARY(16)\", index: true)\n  }\n`\n\nconst outputFilepath = 'schemaScript.sql'\nconst directives = getSchemaDirectives()\nmakeSqlSchema({\n  typeDefs,\n  schemaDirectives: directives,\n  outputFilepath,\n  databaseName: 'dbname',\n  tablePrefix: 'test_',\n})\n```\nThe script above will produce this file:\n```sql\n-- schemaScript.sql\nCREATE TABLE `dbname`.`test_User` (\n  `userId` BINARY(16) NOT NULL,\n  `uniqueColumn` INT NOT NULL UNIQUE,\n  `databaseOnlyField` INT NOT NULL,\n  PRIMARY KEY (`userId`)\n) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n\nCREATE TABLE `dbname`.`test_Post` (\n  `postId` INT NOT NULL AUTO_INCREMENT,\n  `userId` BINARY(16) NOT NULL,\n  `content` VARCHAR(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,\n  `likes` INT NOT NULL,\n  `dateCreated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`postId`),\n  INDEX `USERIDINDEX` (`userId` ASC)\n) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n\nCREATE TABLE `dbname`.`test_UserPair` (\n  `userPairId` BINARY(16) NOT NULL,\n  `parentUserId` BINARY(16) NOT NULL,\n  `childUserId` BINARY(16) NOT NULL,\n  PRIMARY KEY (`userPairId`),\n  INDEX `PARENTUSERIDINDEX` (`parentUserId` ASC),\n  INDEX `CHILDUSERIDINDEX` (`childUserId` ASC),\n  UNIQUE(parentUserId, childUserId),\n  FOREIGN KEY (parentUserId) REFERENCES User(userId)\n);\n```\n\nAlso see [main-test.ts](__tests__/main-test.ts) for a working example.\n\n## MySQL Syntax\nFor reference:\n```sql\nCREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name\n    (create_definition,...)\n    [table_options]\n    [partition_options]\n```\n\n## Arguments for `@sql()`:\nON OBJECT:\n* unicode\n  * Adds `CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci` as *table_option*.\n* constraints\n  * Any text written in here will be appended \"as is\" to the *create_definition*.  \n\nON FIELD_DEFINITION:\n* auto\n  * Marks a column as being AUTO_INCREMENT.\n  * Column with \"auto\" must have INT or SERIAL type.\n  * \"default\" is not allowed with \"auto\".\n  * \"unicode\" is not allowed with \"auto\".\n* default\n  * Sets the DEFAULT clause.\n* index\n  * Creates an index for the column.\n* nullable\n  * Marks the column with NULL. By default, all columns are NOT NULL.\n* primary\n  * Creates a PRIMARY KEY clause for the column.\n  * At least one column needs to be marked as \"primary\".\n  * \"primary\" is not allowed with \"nullable\".\n* type\n  * Specify the column type.\n  * If type is not specified, the MySQL type will be inferred from the GraphQL type.\n  * GraphQLString types must be explicitly defined using \"type\".\n* unicode\n  * If any column is marked with \"unicode\", then the table will have `CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`.\n* unique\n  * Marks a column with the UNIQUE keyword.\n* generated\n  * Marks a generated column.\n  * Example: `@sql(generated: \"(data-\u003e\u003e'$.test')\")`. See [main-test.ts](__tests__/main-test.ts#L137) for more examples.\n\n## SQL Features Supported\u003csup\u003e1\u003c/sup\u003e:\n- [x] Auto Increment\n- [x] Default\n- [x] Index\n- [x] Not Null\n- [x] Primary Key\n- [x] Unicode\n- [x] Unique\n- [ ] Check\n- [X] Constraints\n- [X] Foreign Key (via @constraints)\n- [x] Generated Columns\n\n\u003csup\u003e1\u003c/sup\u003eOnly MySQL is supported at the moment.\n","funding_links":[],"categories":["graphql"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaylorgoolsby%2Fgraphql-directive-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaylorgoolsby%2Fgraphql-directive-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaylorgoolsby%2Fgraphql-directive-sql/lists"}