sqlc-graphql

module
v0.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2024 License: MIT

README

Graphql Schema Generator for SQLC

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/sqlc-dev/sqlc-gen-go).

It has the capability to create a GraphQL schema based on the database schema, as well as to derive GraphQL queries from SQL queries. The produced 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.

TODO

  • Make direct transformation of the SQL column type to the GraphQL field type. Now it is possible only by defining the table and column types.
            - db_type: "pg_catalog.timestamp"
              gql_type: "Time"
  • Generate query with not required parameter field
  • Fix error if the primary key of dataloader is of type Enum defined in the schema
  • Return own error instead of pgx.ErrNoRows from dataloader if the row is not found
  • Manage dataloader cache
  • Add directives to queries
  • Set fields that should have own resolvers after generation in the gqlgen (use directives)
  • Set config to remove parameters from the query
# remove id from the query
    author(id: UUID!): Author!
  • Add the ability to rename Row type names by golang type names
  • Exclude types from the schema generation
  • Add config to generate everything in one file
  • Make the ability to generate query from comment that is like this
-- name: getAuthor :one
-- gql: Query.author(id: UUID!): Author! @resolver(name: "GetAuthor")
  • Create a fixture if the primary key is not named as id

How to use

  1. Install sqlc (https://docs.sqlc.dev/en/latest/overview/install.html)
  2. Make a project with sqlc (https://docs.sqlc.dev/en/latest/tutorials/getting-started-postgresql.html)
  3. 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.2/sqlc-graphql.wasm
      sha256: fe1bbdf7679a24c18cdbef48d2519c9ef517db6316d264f4857a21ef8d3b4e9f

  - 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
          directives:
            - model: "Test"
              field: "CreatedAt"
              directive: "json"
          ## override a column type with a custom GraphQL type
          ## the type should be described manually in the extended.graphql file 
          overrides:
            ## override a column type with a custom GraphQL type
            - column: "test.img"
              gql_type: "Image"
              nullable: true
            ## override SQL type with a custom GraphQL type
            - db_type: "pg_catalog.timestamp"
              gql_type: "Time"
            ## override GO type with a custom GraphQL type
            - go_type: "tutorial/tutorial.NullImage"
              gql_type: "Image"
          ## 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

  1. Run the sqlc command
sqlc -f ./tutorial/sqlc.yaml generate

It will generate the GraphQL schema and the queries in the graphql folder. Also generated types will be linked to the generated by sqlc golang code. After that you can generate the resolvers using gqlgen library (https://gqlgen.com/), and link resolvers to SQL queries.

Examples of output:

# The generated schema
enum AuthorStatus  @goModel(model: "simple/storage.AuthorStatus") {
    active
    inactive
    deleted
}

type Author @goModel(model: "simple/storage.Author") {
    id: Int!
    name: String!
    bio: String
    status: AuthorStatus!
}
# The generated queries
extend type Mutation {
    createAuthor(request: CreateAuthorInput!): Author!
}

input CreateAuthorInput @goModel(model: "simple/storage.CreateAuthorParams") {
    name: String!
    bio: String
}

See the examples folder for more information.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳