Skip to content

Commit ec45fcb

Browse files
committed
Generate queries and mutations
1 parent ff116cd commit ec45fcb

18 files changed

+648
-334
lines changed

.gitignore

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
node_modules/
2+
*.tsbuildinfo
3+
*.log
24
/dist/
3-
/pkg/
4-
*.tsbuildinfo
5+
/pkg/*/graphql.go
6+
/pkg/*/schema.graphql
7+
!/pkg/graphql/schema.graphql

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/dist/

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# GraphQL Code Generator plugin for generating Golang
22

3-
> This package is work in progress. Contributions are welcome <3.
3+
> This package is work in progress.
4+
5+
## Roadmap
6+
7+
- [x] Generate types
8+
- [x] Generate queries and mutations
9+
- [ ] Generate subscriptions with gorilla websocket
10+
- [ ] Add more configuration options
11+
- [ ] Avoid collisions ?
12+
- [ ] Write tests
413

514
This package generates Golang types and requests which use:
615

graphql.config.yaml

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
schema: graphql/schema.graphql
2-
documents:
3-
- graphql/!(schema).graphql
4-
- main.go
5-
extensions:
6-
codegen:
7-
generates:
8-
pkg/graphql/graphql.go:
9-
hooks:
10-
afterOneFileWrite: go fmt
11-
plugins:
12-
- dist/index.js:
13-
# packageName: graphql # default
1+
projects:
2+
default:
3+
schema: pkg/graphql/schema.graphql
4+
documents: pkg/graphql/!(schema).graphql
5+
extensions:
6+
codegen:
7+
generates:
8+
pkg/graphql/graphql.go:
9+
hooks:
10+
afterOneFileWrite: go fmt
11+
plugins:
12+
- dist/index.js
13+
rickandmorty:
14+
schema: https://rickandmortyapi.com/graphql/
15+
documents: pkg/rickandmorty/document.graphql
16+
extensions:
17+
codegen:
18+
generates:
19+
pkg/rickandmorty/graphql.go:
20+
hooks:
21+
afterOneFileWrite: go fmt
22+
plugins:
23+
- dist/index.js:
24+
packageName: rickandmorty

graphql/document.graphql

Lines changed: 0 additions & 5 deletions
This file was deleted.

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
"name": "graphql-codegen-golang",
33
"description": "Graphql Code Generator plugin for generating Golang",
44
"scripts": {
5+
"dev": "yarn run build && yarn run codegen",
56
"build": "tsc",
6-
"codegen": "yarn run build && graphql-codegen",
7+
"codegen": "yarn run graphql-codegen",
8+
"test": "go test -v ./pkg/rickandmorty/",
79
"mock": "graphql-inspector serve graphql/schema.graphql",
810
"format": "prettier --write '**/*.{js,jsx,ts,tsx,json,yaml,gql,md,html,css}'",
911
"lint": "eslint --ext .js,.jsx,.ts,.tsx .",
10-
"build:precommit": "yarn run codegen && :"
12+
"build:precommit": "yarn run dev && :"
1113
},
1214
"dependencies": {
1315
"@graphql-codegen/plugin-helpers": "1.17.8",
14-
"graphql": "15.3.0"
16+
"graphql": "15.3.0",
17+
"liquidjs": "9.15.0"
1518
},
1619
"devDependencies": {
1720
"@graphql-codegen/cli": "1.17.8",
@@ -44,7 +47,6 @@
4447
},
4548
"prettier": {
4649
"arrowParens": "avoid",
47-
"printWidth": 100,
4850
"semi": false,
4951
"singleQuote": true
5052
},

pkg/graphql/document.graphql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
query GetCloudProviders($order_by: [CloudProviders_order_by!]) {
2+
CloudProviders(limit: 1, order_by: $order_by) {
3+
id
4+
}
5+
}
6+
7+
query GetUsers($limit: Int = 1) {
8+
Users(limit: $limit) {
9+
id
10+
}
11+
}

graphql/schema.graphql renamed to pkg/graphql/schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
scalar test_format
2+
13
schema {
24
query: query_root
35
subscription: subscription_root
@@ -9,6 +11,10 @@ type CloudProviders {
911
name: name!
1012
}
1113

14+
type test_format_object {
15+
test_format_field: test_format
16+
}
17+
1218
# Boolean expression to filter rows from the table "CloudProviders". All fields are combined with a logical 'AND'.
1319
input CloudProviders_bool_exp {
1420
_and: [CloudProviders_bool_exp]

pkg/rickandmorty/document.graphql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
query GetCharacters($page: Int) {
2+
characters(page: $page) {
3+
results {
4+
id
5+
name
6+
}
7+
}
8+
}

pkg/rickandmorty/graphql_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package rickandmorty
2+
3+
import (
4+
"testing"
5+
)
6+
7+
const URL_API = "https://rickandmortyapi.com/graphql"
8+
9+
var client = NewClient(URL_API)
10+
11+
func TestGetRicks(t *testing.T) {
12+
chars, err := client.GetCharacters(nil)
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
if chars.Characters.Results == nil || len(*chars.Characters.Results) == 0 {
17+
t.Fatalf("result is empty: %v", chars.Characters.Results)
18+
}
19+
t.Log((*chars.Characters.Results)[0])
20+
}

src/config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/**
2-
* GolangPluginConfig lets you configure how GolangVisitor generates
2+
* GolangPluginConfig lets you configure how GolangGenerator generates
33
* Golang code from GraphQL schemas and documents.
44
*/
55
export interface GolangPluginConfig {
66
/**
7-
* Name of the generated Golang package. Defaults to `graphql`.
7+
* Name of the generated Golang package.
8+
* @default graphql
9+
* @example
810
* ```go
911
* package graphql
1012
* ```

src/formatter.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)