Skip to content

Commit

Permalink
feat!: parameterize generation of the HTTP client
Browse files Browse the repository at this point in the history
  • Loading branch information
ctison committed Sep 4, 2022
1 parent 07d0a72 commit 323313a
Show file tree
Hide file tree
Showing 30 changed files with 1,032 additions and 3,245 deletions.
11 changes: 0 additions & 11 deletions .eslintrc.js

This file was deleted.

22 changes: 9 additions & 13 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ on:
workflow_dispatch:

concurrency:
group: cicd-${{ github.ref }}
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@a12a3943b4bdde767164f792f33f40b04645d846 # tag=v3
- uses: actions/setup-node@9ced9a43a244f3ac94f13bfd896db8c8f30da67a # tag=v3
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version-file: .nvmrc

- name: Install & Build & Test
run: |
corepack enable
pnpm install --frozen-lockfile --strict-peer-dependencies
pnpm install --frozen-lockfile
pnpm run build
pnpm run gen
pnpm run gen:rickandmorty
pnpm run test:rickandmorty
- name: Compute package version
id: version
Expand All @@ -40,22 +41,17 @@ jobs:

- name: Publish NPM package
if: startsWith(github.ref, 'refs/tags/v')
working-directory: packages/graphql-codegen-golang
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
npm version --no-git-tag-version '${{ steps.version.outputs.version }}'
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
npm publish --access public
rm .npmrc
pnpm publish --access public
- name: Github Release
if: startsWith(github.ref, 'refs/tags/v')
uses: actions/create-release@0cb9c9b65d5d1901c1f53e5e66eaf4afd303e70e # tag=v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
uses: softprops/action-gh-release@v1
with:
tag_name: v${{ steps.version.outputs.version }}
release_name: v${{ steps.version.outputs.version }}
body: |
## NPM Package
https://www.npmjs.com/package/graphql-codegen-golang/v/${{ steps.version.outputs.version }}
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
node_modules/
*.tsbuildinfo
*.log
/dist/
/pkg/*/graphql.go
/pkg/*/schema.graphql
!/pkg/graphql/schema.graphql
/packages/*/dist/
/examples/*/graphql.go
/examples/*/schema.graphql
!/examples/graphql/schema.graphql
.turbo/
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//registry.npmjs.org/:_authToken=$NPM_TOKEN
auto-install-peers=true
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/dist/
dist/
pnpm-lock.yaml
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"golang.go",
"redhat.vscode-yaml"
"redhat.vscode-yaml",
"GraphQL.vscode-graphql"
]
}
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[css][graphql][html][javascript][javascriptreact][json][jsonc][markdown][mdx][scss][typescript][typescriptreact][yaml]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.formatOnSave": true,
"editor.tabSize": 2,
"editor.codeActionsOnSave": {
Expand All @@ -11,5 +13,6 @@
"**/*.json": "jsonc"
},
"files.eol": "\n",
"files.insertFinalNewline": true
"files.insertFinalNewline": true,
"cSpell.words": ["codegen", "pnpm", "nvmrc", "rickandmorty", "liquidjs"]
}
53 changes: 0 additions & 53 deletions README.md

This file was deleted.

1 change: 1 addition & 0 deletions README.md
3 changes: 3 additions & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module examples

go 1.18
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
go 1.18

use ./pkg/rickandmorty
use ./examples/
15 changes: 8 additions & 7 deletions graphql.config.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
projects:
default:
schema: pkg/graphql/schema.graphql
documents: pkg/graphql/!(schema).graphql
schema: examples/graphql/schema.graphql
documents: examples/graphql/!(schema).graphql
extensions:
codegen:
generates:
pkg/graphql/graphql.go:
examples/graphql/graphql.go:
hooks:
afterOneFileWrite: go fmt
plugins:
- dist/index.js
- packages/graphql-codegen-golang/dist/index.js
rickandmorty:
schema: https://rickandmortyapi.com/graphql/
documents: pkg/rickandmorty/document.graphql
documents: examples/rickandmorty/document.graphql
extensions:
codegen:
generates:
pkg/rickandmorty/graphql.go:
examples/rickandmorty/graphql.go:
hooks:
afterOneFileWrite: go fmt
plugins:
- dist/index.js:
- packages/graphql-codegen-golang/dist/index.js:
packageName: rickandmorty
generateHTTPClient: true
50 changes: 10 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
{
"name": "graphql-codegen-golang",
"description": "Graphql Code Generator plugin for generating Golang",
"keywords": [
"graphql-codegen",
"graphql",
"golang"
],
"repository": {
"type": "git",
"url": "https://github.com/ctison/graphql-codegen-golang.git"
},
"license": "MIT",
"main": "dist/index.js",
"files": [
"README.md",
"dist/"
],
"private": true,
"scripts": {
"build": "tsc",
"build": "turbo run build",
"test": "turbo run test",
"lint": "turbo run lint",
"format": "prettier --write .",
"gen": "graphql-codegen",
"lint": "eslint --ext .js,.jsx,.ts,.tsx .",
"mock": "graphql-inspector serve pkg/graphql/schema.graphql",
"test": "go test -v ./pkg/rickandmorty/"
"gen:rickandmorty": "pnpm run gen -p rickandmorty",
"test:rickandmorty": "go test -v ./examples/rickandmorty"
},
"husky": {
"hooks": {
Expand All @@ -33,9 +18,6 @@
"*.{js,jsx,ts,tsx,json,yaml,gql,md,html,css}": [
"prettier --write"
],
"*.{js,jsx,ts,tsx}": [
"eslint"
],
"package.json": [
"sort-package-json"
]
Expand All @@ -45,28 +27,16 @@
"semi": false,
"singleQuote": true
},
"dependencies": {
"@graphql-codegen/plugin-helpers": "2.4.2",
"graphql": "16.3.0",
"liquidjs": "9.36.0"
},
"devDependencies": {
"@graphql-codegen/cli": "2.6.2",
"@graphql-inspector/cli": "3.1.1",
"@graphql-inspector/config": "3.1.1",
"@graphql-tools/utils": "8.6.3",
"@rushstack/eslint-config": "2.5.2",
"@typescript-eslint/eslint-plugin": "5.15.0",
"@typescript-eslint/parser": "5.15.0",
"eslint": "8.11.0",
"eslint-plugin-import": "2.25.4",
"@graphql-codegen/cli": "2.12.0",
"husky": "7.0.4",
"lint-staged": "12.3.7",
"prettier": "2.6.0",
"sort-package-json": "1.54.0",
"typescript": "4.6.2"
"turbo": "1.4.5",
"typescript": "4.8.2"
},
"packageManager": "pnpm@7.9.5",
"packageManager": "pnpm@7.10.0",
"renovate": {
"extends": [
"github>ctison/renovate"
Expand Down
51 changes: 51 additions & 0 deletions packages/graphql-codegen-golang/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# GraphQL Code Generator plugin for generating Golang

## Roadmap

- [x] Generate types
- [x] Generate queries and mutations
- [ ] Generate subscriptions with gorilla websocket
- [ ] Generate Terraform providers
- [ ] Generate CLI
- [ ] Add more configuration options
- [ ] Avoid possible naming collisions

This package generates Golang types from GraphQL schema, and can optionally generate:

- An HTTP client
- A Terraform provider wrapping the generated HTTP client (TODO)
- A cobra CLI wrapping the generated HTTP client (TODO)

## Install

The package is published to [graphql-codegen-golang](https://www.npmjs.com/package/graphql-codegen-golang).

```sh
pnpm add -DE graphql-codegen-golang
yarn add -DE graphql-codegen-golang
npm install -DE graphql-codegen-golang
```

## Usage: `codegen.yaml`

```yaml
schema: examples/graphql/schema.graphql
documents: examples/graphql/!(schema).graphql
generates:
examples/graphql/graphql.go:
hooks:
afterOneFileWrite: go fmt
plugins:
- graphql-codegen-golang:
packageName: graphql # default
generateHTTPClient: false # default
```
## Configuration
Configuration source is at [src/config.ts](src/config.ts)
| Name | Type | Default | Description |
| ------------------ | ------- | ------- | ------------------------------------------ |
| packageName | string | graphql | Name of the generated Golang package. |
| generateHTTPClient | boolean | false | Should an GraphQL HTTP client be generated |
32 changes: 32 additions & 0 deletions packages/graphql-codegen-golang/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "graphql-codegen-golang",
"description": "Graphql Code Generator plugin for generating Golang",
"keywords": [
"graphql-codegen",
"graphql",
"golang"
],
"repository": {
"type": "git",
"url": "https://github.com/ctison/graphql-codegen-golang.git"
},
"license": "MIT",
"files": [
"README.md",
"dist/"
],
"dependencies": {
"@graphql-codegen/plugin-helpers": "2.7.0",
"graphql": "16.6.0",
"liquidjs": "9.42.0"
},
"devDependencies": {
"typescript": "4.8.2"
},
"scripts": {
"build": "rm -rf dist/ && tsc -p tsconfig.build.json",
"dev": "rm -rf dist/ && tsc -p tsconfig.build.json -w"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ export interface IGolangPluginConfig {
* ```
*/
packageName?: string

/**
* Define if an HTTP client should be generated
* @default false
*/
generateHTTPClient?: boolean
}
Loading

0 comments on commit 323313a

Please sign in to comment.