{"id":24461606,"url":"https://github.com/jkrajniak/graphql-codegen-go","last_synced_at":"2025-04-13T05:45:39.660Z","repository":{"id":142805738,"uuid":"221666707","full_name":"jkrajniak/graphql-codegen-go","owner":"jkrajniak","description":"Generate Go structs from graphql schema","archived":false,"fork":false,"pushed_at":"2023-10-11T23:40:58.000Z","size":194,"stargazers_count":23,"open_issues_count":4,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T05:45:34.815Z","etag":null,"topics":["golang","graphql","graphql-tools","hacktoberfest","hacktoberfest2024"],"latest_commit_sha":null,"homepage":"","language":"Go","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/jkrajniak.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-14T10:06:55.000Z","updated_at":"2024-10-03T20:39:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"f21aec3e-85ea-4b82-ac5b-2d740b3213eb","html_url":"https://github.com/jkrajniak/graphql-codegen-go","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jkrajniak%2Fgraphql-codegen-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jkrajniak%2Fgraphql-codegen-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jkrajniak%2Fgraphql-codegen-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jkrajniak%2Fgraphql-codegen-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jkrajniak","download_url":"https://codeload.github.com/jkrajniak/graphql-codegen-go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248670518,"owners_count":21142901,"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":["golang","graphql","graphql-tools","hacktoberfest","hacktoberfest2024"],"created_at":"2025-01-21T04:29:02.455Z","updated_at":"2025-04-13T05:45:39.640Z","avatar_url":"https://github.com/jkrajniak.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Go Report Card](https://goreportcard.com/badge/github.com/jkrajniak/graphql-codegen-go)](https://goreportcard.com/report/github.com/jkrajniak/graphql-codegen-go)\n\n# graphql-codegen-go\nGenerate Go structs from your GraphQL schema.\n\nThis [code generator](https://blog.golang.org/generate) helps you derive Go structures directly from [GraphQL](https://graphql.org/) schema. The schema\ncan be located either locally or can be fetched from GIT repository.\n\nInstall it using `go get`:\n\n```bash\n$ go get -u github.com/jkrajniak/graphql-codegen-go\n```\n## Quick start\n\nSimply, define the GQL schema\n```graphql\ntype Person {\n  name: String!\n  age: Int!\n  weight: Int\n  likes: [String]\n  donts: [String!]\n}\n```\nand save it, e.g. in `schema.gql` file. Then run the code generator\n\n```bash\n$ graphql-codegen-go -schemas schema.gql -packageName pkg -out models.go\n```\nAs a result, you will get a `models.go` file with the following Go code\n\n```go\n// Code generated by go generate; DO NOT EDIT.\n// This file was generated from GraphQL schema schema.gql\n\npackage pkg\n\ntype Person struct {\n\tName   string    `json:\"name\"`\n\tAge    int64     `json:\"age\"`\n\tWeight *int64    `json:\"weight\"`\n\tLikes  []*string `json:\"likes\"`\n\tDonts  []string  `json:\"donts\"`\n}\n```\n\nNotice that not required (`weight`) fields are converted to the pointers. The `packageName` option is optional. The generator\nwill try to derive the package name from the current running path of the code, or if run by `go:generate` from `$GOPACKAGE` env variable.\n\n### GIT\n\nThe schema does not have to be located locally. The program supports also Git repositories.\nLet's assume that you the `schema.gql` file is placed in `github.com/orange/repo1` repository, inside a `deployment` directory.\nThen, to create the structures you can run the generator as follows\n\n```bash\n$ graphql-codegen-go -schemas https://github.com/orange/repo1.git/deployment/schema.gql -packageName pkg -out models.go\n```\n\nor via ssh\n\n```\n$ graphql-codegen-go -schemas git@github.com:orange/repo1.git/deployment/schema.gql -packageName pkg -out models.go\n```\n\nBy default, the schema is pulled from the `HEAD`. To point a specific commit, you can place a commit hash after the file name, e.g.,\n\n```\n$ graphql-codegen-go -schemas git@github.com:orange/repo1.git/deployment/schema.gql#a56351vc -packageName pkg -out models.go\n```\n\nMoreover, you can also point the specific branch or tag by using `@` sign\n\n```\n$ graphql-codegen-go -schemas git@github.com:orange/repo1.git/deployment/schema.gql@branch -packageName pkg -out models.go\n```\n\nor\n\n```\n$ graphql-codegen-go -schemas git@github.com:orange/repo1.git/deployment/schema.gql@tag1 -packageName pkg -out models.go\n```\n\n### Entities\n\nBy default generator will output structures for all of the entities found in the schema. To output only a subset of structures\nyou can use `-entities` option.\n\nFor example\n```bash\n$ graphql-codegen-go -schemas schema.gql -packageName pkg -out models.go -entities Person\n```\nwill create file `models.go` only with a single structure `Person`, and all related dependent structures and enums.\n\n\n### YAML Config\n\nInstead of command line parameters, the generator supports also a config file (`-config`). The example of the file can be found in `examples/config/config.yml`.\n\nThe structure of the YAML file is\n\n```yaml\n\nschema: A list of schema files (it will be combined into one schema before parsing)\ngenerates: A key-value map, where key is the name of the output Go file\n    \u003coutput-file\u003e:\n      config:\n        packageName: A package name\n        entities: A list of entities to be included into the output Go file\n```\n\n#### Example\n\n```yaml\nschema:\n    - ./schema.graphql\n    - ./types.graphql\n    - https://github.com/jkrajniak/sc.git/schema1.gql\ngenerates:\n    internal/models.go:\n      config:\n        packageName: internal\n        entities:\n          - User\n          - Person\n    internal/abc/models.go:\n      config:\n        packageName: abc\n        entities:\n          - Action\n```\n\nAfter execution of the above config you will find two `.go` files (`internal/models.go` and `internal/abc/models.go`);\nthe first will contain structures `User` and `Person`, and the second `Action`.\n\nThe GQL schema will be read from two local files, from GIT repository.\n\n### go:generate\n\nThe generator can work perfectly fine with the ```go:generate``` directive. The examples of how to include it can be found in `examples/` directory.\n\n\n## Union\n\nThe [union type](https://graphql.org/learn/schema/#union-types) is supported in the following way. Let's consider a schema\n```graphql\ntype Et {\n  value: String\n}\n\ntype Pt {\n  label: String!\n}\n\nunion Ett = Et | Pt\n\ntype Entity {\n  id: String!\n  etpt: Ett\n}\n```\n\nThe union type `Ett` is converted into a Go struct `Ett`\n```go\ntype Et struct {\n\tValue *string `json:\"value\"`\n}\n\ntype Pt struct {\n\tLabel string `json:\"label\"`\n}\n\ntype Ett struct {\n\tTypeName string `json:\"__typeName\"`\n\tEt\n\tPt\n}\n\ntype Entity struct {\n\tId   string `json:\"id\"`\n\tEtpt *Ett   `json:\"etpt\"`\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjkrajniak%2Fgraphql-codegen-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjkrajniak%2Fgraphql-codegen-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjkrajniak%2Fgraphql-codegen-go/lists"}