sqlc-graphql
This is a plugin for the sqlc compiler (https://sqlc.dev/).
It adds the generation of GraphQL schema from the database schema.
This plugin was created using the codebase of sqlc-gen-go plugin (https://github.com/debugger84/sqlc-graphql).
It can generate the GraphQL schema from the database schema and the GraphQL queries from the SQL queries.
The generated code is compatible with the gqlgen library (https://gqlgen.com/).
Features
- Generates GraphQL schema from the database schema
- Generates GraphQL enums
- Generates comments for the GraphQL queries
- Generates queries for the GraphQL schema using the SQL queries as a base.
How to use
- Install sqlc (https://docs.sqlc.dev/en/latest/overview/install.html)
- Make a project with sqlc (https://docs.sqlc.dev/en/latest/tutorials/getting-started-postgresql.html)
- Change the sqlc.yaml file to use plugins instead of direct generation
version: '2'
plugins:
- name: graphql
wasm:
url: https://github.com/debugger84/sqlc-graphql/releases/download/v0.1.0/sqlc-graphql.wasm
sha256: c31faaf0309f1f5b9cc34128322f227cf8a1df86d1fbed5f2f17023a2597606f
- name: golang
wasm:
url: https://downloads.sqlc.dev/plugin/sqlc-gen-go_1.3.0.wasm
sha256: e8206081686f95b461daf91a307e108a761526c6768d6f3eca9781b0726b7ec8
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
codegen:
- plugin: graphql
out: "./graphql"
options:
## the full package name pointing to the code generated by golang plugin
package: "tutorial/tutorial"
## generate GraphQL enums
emit_all_enum_values: true
## create several default types and directives to work in conjunction with the gqlgen library https://gqlgen.com/
gen_common_parts: true
## override a column type with a custom GraphQL type
## the type should be described manually in the extended.graphql file
overrides:
- column: "test.img"
gql_type: "Image"
nullable: true
## exclude columns from the generated schema
## Test - is the generated Graphql object
## and CreatedAt is the column name to be excluded
exclude:
- "Test.CreatedAt"
## options for the default golang generation plugin https://github.com/sqlc-dev/sqlc-gen-go
- plugin: golang
out: "./"
options:
package: "tutorial"
sql_package: "pgx/v4"
emit_json_tags: true
emit_all_enum_values: true
json_tags_case_style: "camel"
out: "./"
overrides:
- column: "test.img"
go_type: "tutorial/tutorial.NullImage"
nullable: true
- Run the sqlc command
sqlc -f ./tutorial/sqlc.yaml generate
It will generate the GraphQL schema and the queries in the graphql folder.
See the examples folder for more information.