{"id":13514038,"url":"https://github.com/mjarkk/yarql","last_synced_at":"2025-06-21T22:07:34.334Z","repository":{"id":48674264,"uuid":"365241737","full_name":"mjarkk/yarql","owner":"mjarkk","description":"YarQL!! A different approach to making GraphQL servers in Go","archived":false,"fork":false,"pushed_at":"2022-04-18T13:47:13.000Z","size":15443,"stargazers_count":23,"open_issues_count":2,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-13T11:42:04.566Z","etag":null,"topics":["go","golang","graphql","graphql-server"],"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/mjarkk.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}},"created_at":"2021-05-07T13:25:57.000Z","updated_at":"2024-08-07T14:54:37.000Z","dependencies_parsed_at":"2022-08-27T08:22:31.827Z","dependency_job_id":null,"html_url":"https://github.com/mjarkk/yarql","commit_stats":null,"previous_names":["mjarkk/go-graphql"],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/mjarkk/yarql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjarkk%2Fyarql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjarkk%2Fyarql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjarkk%2Fyarql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjarkk%2Fyarql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mjarkk","download_url":"https://codeload.github.com/mjarkk/yarql/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjarkk%2Fyarql/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261200408,"owners_count":23123953,"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":["go","golang","graphql","graphql-server"],"created_at":"2024-08-01T05:00:44.409Z","updated_at":"2025-06-21T22:07:29.314Z","avatar_url":"https://github.com/mjarkk.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"![Banner](https://github.com/mjarkk/yarql/blob/main/banner.png?raw=true)\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/mjarkk/yarql.svg)](https://pkg.go.dev/github.com/mjarkk/yarql)\n[![Go Report Card](https://goreportcard.com/badge/github.com/mjarkk/yarql)](https://goreportcard.com/report/github.com/mjarkk/yarql)\n[![Coverage Status](https://coveralls.io/repos/github/mjarkk/go-graphql/badge.svg?branch=main)](https://coveralls.io/github/mjarkk/go-graphql?branch=main)\n\n# YarQL, A Graphql library for GoLang\n\nJust a different approach to making graphql servers in Go\n\n## Features\n\n- Easy to use and not much code required\n- Schema based on code\n- Build on top of the [graphql spec 2021](https://spec.graphql.org/October2021/)\n- No code generators\n- [Only 1 dependency](go.mod)\n- Easy to implement in many web servers, see the\n  [gin](https://github.com/mjarkk/yarql/blob/main/examples/gin/main.go) and\n  [fiber](https://github.com/mjarkk/yarql/blob/main/examples/fiber/main.go)\n  examples\n- [File upload support](#file-upload)\n- Supports [Apollo tracing](https://github.com/apollographql/apollo-tracing)\n- [Fast](#Performance)\n\n## Example\n\nSee the [/examples](https://github.com/mjarkk/yarql/tree/main/examples) folder\nfor more examples\n\n```go\npackage main\n\nimport (\n    \"log\"\n    \"github.com/mjarkk/yarql\"\n)\n\ntype Post struct {\n\tId    uint `gq:\",ID\"`\n\tTitle string `gq:\"name\"`\n}\n\ntype QueryRoot struct{}\n\nfunc (QueryRoot) ResolvePosts() []Post {\n\treturn []Post{\n\t\t{1, \"post 1\"},\n\t\t{2, \"post 2\"},\n\t\t{3, \"post 3\"},\n\t}\n}\n\ntype MethodRoot struct{}\n\nfunc main() {\n\ts := yarql.NewSchema()\n\n    err := s.Parse(QueryRoot{}, MethodRoot{}, nil)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\n\terrs := s.Resolve([]byte(`\n\t\t{\n\t\t\tposts {\n\t\t\t\tid\n\t\t\t\tname\n\t\t\t}\n\t\t}\n\t`), yarql.ResolveOptions{})\n\tfor _, err := range errs {\n\t\tlog.Fatal(err)\n\t}\n\n    fmt.Println(string(s.Result))\n    // {\"data\": {\n    //   \"posts\": [\n    //     {\"id\": \"1\", \"name\": \"post 1\"},\n    //     {\"id\": \"2\", \"name\": \"post 2\"},\n    //     {\"id\": \"3\", \"name\": \"post 3\"}\n    //   ]\n    // },\"errors\":[],\"extensions\":{}}\n}\n```\n\n## Docs\n\n### Defining a field\n\nAll fields names are by default changed to graphql names, for example `VeryNice`\nchanges to `veryNice`. There is one exception to the rule when the second letter\nis also upper case like `FOO` will stay `FOO`\n\nIn a struct:\n\n```go\nstruct {\n\tA string\n}\n```\n\nA resolver function inside the a struct:\n\n```go\nstruct {\n\tA func() string\n}\n```\n\nA resolver attached to the struct.\n\nName Must start with `Resolver` followed by one uppercase letter\n\n_The resolve identifier is trimmed away in the graphql name_\n\n```go\ntype A struct {}\nfunc (A) ResolveA() string {return \"Ahh yea\"}\n```\n\n### Supported input and output value types\n\nThese go data kinds should be globally accepted:\n\n- `bool`\n- `int` _all bit sizes_\n- `uint` _all bit sizes_\n- `float` _all bit sizes_\n- `array`\n- `ptr`\n- `string`\n- `struct`\n\nThere are also special values:\n\n- `time.Time` _converted from/to ISO 8601_\n- `*multipart.FileHeader` _get file from multipart form_\n\n### Ignore fields\n\n```go\nstruct {\n\t// internal fields are ignored\n\tbar string\n\n\t// ignore public fields\n\tBar string `gq:\"-\"`\n}\n```\n\n### Rename field\n\n```go\nstruct {\n\t// Change the graphql field name to \"bar\"\n\tFoo string `gq:\"bar\"`\n}\n```\n\n### Label as ID field\n\n```go\nstruct Foo {\n\t// Notice the \",\" before the id\n\tId string `gq:\",id\"`\n\n\t// Pointers and numbers are also supported\n\t// NOTE NUMBERS WILL BE CONVERTED TO STRINGS IN OUTPUT\n\tPostId *int `gq:\",id\"`\n}\n\n// Label method response as ID using AttrIsID\n// The value returned for AttrIsID is ignored\n// You can also still just fine append an error: (string, AttrIsID, error)\nfunc (Foo) ResolveExampleMethod() (string, AttrIsID) {\n\treturn \"i'm an ID type\", 0\n}\n```\n\n### Methods and field arguments\n\nAdd a struct to the arguments of a resolver or func field to define arguments\n\n```go\nfunc (A) ResolveUserID(args struct{ Id int }) int {\n\treturn args.Id\n}\n```\n\n### Resolver error response\n\nYou can add an error response argument to send back potential errors.\n\nThese errors will appear in the errors array of the response.\n\n```go\nfunc (A) ResolveMe() (*User, error) {\n\tme, err := fetchMe()\n\treturn me, err\n}\n```\n\n### Context\n\nYou can add `*yarql.Ctx` to every resolver of func field to get more information\nabout the request or user set properties\n\n#### Context values\n\nThe context can store values defined by a key. You can add values by using the\n'SetVelue' method and obtain values using the `GetValue` method\n\n```go\nfunc (A) ResolveMe(ctx *yarql.Ctx) User {\n\tctx.SetValue(\"resolved_me\", true)\n\treturn ctx.GetValue(\"me\").(User)\n}\n```\n\nYou can also provide values to the `RequestOptions`:\n\n```go\nyarql.RequestOptions{\n\tValues: map[string]interface{}{\n\t\t\"key\": \"value\",\n\t},\n}\n```\n\n#### GoLang context\n\nYou can also have a GoLang context attached to our context (`yarql.Ctx`) by\nproviding the `RequestOptions` with a context or calling the `SetContext` method\non our context (`yarql.Ctx`)\n\n```go\nimport \"context\"\n\nyarql.RequestOptions{\n\tContext: context.Background(),\n}\n\nfunc (A) ResolveUser(ctx *yarql.Ctx) User {\n\tc := ctx.GetContext()\n\tc = context.WithValue(c, \"resolved_user\", true)\n\tctx.SetContext(c)\n\n\treturn User{}\n}\n```\n\n### Optional fields\n\nAll types that might be `nil` will be optional fields, by default these fields\nare:\n\n- Pointers\n- Arrays\n\n### Enums\n\nEnums can be defined like so\n\nSide note on using enums as argument, It might return a nullish value if the\nuser didn't provide a value\n\n```go\n// The enum type, everywhere where this value is used it will be converted to an enum in graphql\n// This can also be a: string, int(*) or uint(*)\ntype Fruit uint8\n\nconst (\n\tApple Fruit = iota\n\tPeer\n\tGrapefruit\n)\n\nfunc main() {\n\ts := yarql.NewSchema()\n\n\t// The map key is the enum it's key in graphql\n\t// The map value is the go value the enum key is mapped to or the other way around\n\t// Also the .RegisterEnum(..) method must be called before .Parse(..)\n\ts.RegisterEnum(map[string]Fruit{\n\t\t\"APPLE\":      Apple,\n\t\t\"PEER\":       Peer,\n\t\t\"GRAPEFRUIT\": Grapefruit,\n\t})\n\n\ts.Parse(QueryRoot{}, MethodRoot{}, nil)\n}\n```\n\n### Interfaces\n\nGraphql interfaces can be created using go interfaces\n\nThis library needs to analyze all types before you can make a query and as we\ncannot query all types that implement a interface you'll need to help the\nlibrary with this by calling `Implements` for every implementation. If\n`Implements` is not called for a type the response value for that type when\ninside a interface will always be `null`\n\n```go\ntype QuerySchema struct {\n\tBar      BarWImpl\n\tBaz      BazWImpl\n\tBarOrBaz InterfaceType\n}\n\ntype InterfaceType interface {\n\t// Interface fields\n\tResolveFoo() string\n\tResolveBar() string\n}\n\ntype BarWImpl struct{}\n\n// Implements hints this library to register BarWImpl\n// THIS MUST BE CALLED FOR EVERY TYPE THAT IMPLEMENTS InterfaceType\nvar _ = yarql.Implements((*InterfaceType)(nil), BarWImpl{})\n\nfunc (BarWImpl) ResolveFoo() string { return \"this is bar\" }\nfunc (BarWImpl) ResolveBar() string { return \"This is bar\" }\n\ntype BazWImpl struct{}\nvar _ = yarql.Implements((*InterfaceType)(nil), BazWImpl{})\nfunc (BazWImpl) ResolveFoo() string { return \"this is baz\" }\nfunc (BazWImpl) ResolveBar() string { return \"This is baz\" }\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eRelay Node example\u003c/summary\u003e\n\u003cbr\u003e\n\nFor a full relay example see\n[examples/relay/backend/](./examples/relay/backend/)\n\n```go\ntype Node interface {\n\tResolveId() (uint, yarql.AttrIsID)\n}\n\ntype User struct {\n\tID    uint `gq:\"-\"` // ignored because of (User).ResolveId()\n\tName  string\n}\n\nvar _ = yarql.Implements((*Node)(nil), User{})\n\n// ResolveId implements the Node interface\nfunc (u User) ResolveId() (uint, yarql.AttrIsID) {\n\treturn u.ID, 0\n}\n```\n\n\u003c/details\u003e\n\n### Directives\n\nThese directives are added by default:\n\n- `@include(if: Boolean!)` _on Fields and fragments,\n  [spec](https://spec.graphql.org/October2021/#sec--include)_\n- `@skip(if: Boolean!)` _on Fields and fragments,\n  [spec](https://spec.graphql.org/October2021/#sec--skip)_\n\nTo add custom directives:\n\n```go\nfunc main() {\n\ts := yarql.NewSchema()\n\n\t// Also the .RegisterEnum(..) method must be called before .Parse(..)\n\ts.RegisterDirective(Directive{\n\t\t// What is the name of the directive\n\t\tName: \"skip_2\",\n\n\t\t// Where can this directive be used in the query\n\t\tWhere: []DirectiveLocation{\n\t\t\tDirectiveLocationField,\n\t\t\tDirectiveLocationFragment,\n\t\t\tDirectiveLocationFragmentInline,\n\t\t},\n\n\t\t// This methods's input work equal to field arguments\n\t\t// tough the output is required to return DirectiveModifier\n\t\t// This method is called always when the directive is used\n\t\tMethod: func(args struct{ If bool }) DirectiveModifier {\n\t\t\treturn DirectiveModifier{\n\t\t\t\tSkip: args.If,\n\t\t\t}\n\t\t},\n\n\t\t// The description of the directive\n\t\tDescription: \"Directs the executor to skip this field or fragment when the `if` argument is true.\",\n\t})\n\n\ts.Parse(QueryRoot{}, MethodRoot{}, nil)\n}\n```\n\n### File upload\n\n_NOTE: This is NOT\n[graphql-multipart-request-spec](https://github.com/jaydenseric/graphql-multipart-request-spec)\ntough this is based on\n[graphql-multipart-request-spec #55](https://github.com/jaydenseric/graphql-multipart-request-spec/issues/55)_\n\nIn your go code add `*multipart.FileHeader` to a methods inputs\n\n```go\nfunc (SomeStruct) ResolveUploadFile(args struct{ File *multipart.FileHeader }) string {\n\t// ...\n}\n```\n\nIn your graphql query you can now do:\n\n```gql\nuploadFile(file: \"form_file_field_name\")\n```\n\nIn your request add a form file with the field name: `form_file_field_name`\n\n## Testing\n\nThere is a\n[pkg.go.dev mjarkk/go-graphql/tester](https://pkg.go.dev/github.com/mjarkk/yarql/tester)\npackage available with handy tools for testing the schema\n\n## Performance\n\nBelow shows a benchmark of fetching the graphql schema (query parsing + data\nfetching)\n\n_Note: This benchmark also profiles the cpu and that effects the score by a bit_\n\n```sh\n# go test -benchmem -bench \"^(BenchmarkResolve)\\$\"\n# goos: darwin\n# cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\nBenchmarkResolve-12    \t   13246\t     83731 ns/op\t    1344 B/op\t      47 allocs/op\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eCompared to other libraries\u003c/summary\u003e\n\u003cbr\u003e\n\nInjecting `resolver_benchmark_test.go \u003e BenchmarkHelloWorldResolve` into\n[appleboy/golang-graphql-benchmark](https://github.com/appleboy/golang-graphql-benchmark)\nresults in the following:\n\nTake these results with a big grain of salt, i didn't use the last version of\nthe libraries thus my result might be garbage compared to the others by now!\n\n```sh\n# go test -v -bench=Master -benchmem\n# goos: darwin\n# goarch: amd64\n# pkg: github.com/appleboy/golang-graphql-benchmark\n# cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\nBenchmarkGoGraphQLMaster\nBenchmarkGoGraphQLMaster-12          \t   24992\t     48180 ns/op\t   26895 B/op\t     445 allocs/op\nBenchmarkPlaylyfeGraphQLMaster-12    \t  320289\t      3770 ns/op\t    2797 B/op\t      57 allocs/op\nBenchmarkGophersGraphQLMaster-12     \t  391269\t      3114 ns/op\t    3634 B/op\t      38 allocs/op\nBenchmarkThunderGraphQLMaster-12     \t  708327\t      1707 ns/op\t    1288 B/op\t      30 allocs/op\nBenchmarkMjarkkGraphQLGoMaster-12    \t 2560764\t       466.5 ns/op\t      80 B/op\t       1 allocs/op\n```\n\n\u003c/details\u003e\n\n## Alternatives\n\n- [graph-gophers/graphql-go](https://github.com/graph-gophers/graphql-go)\n  :heart: The library that inspired me to make this one\n- [ccbrown/api-fu](https://github.com/ccbrown/api-fu)\n- [99designs/gqlgen](https://github.com/99designs/gqlgen)\n- [graphql-go/graphql](https://github.com/graphql-go/graphql)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmjarkk%2Fyarql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmjarkk%2Fyarql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmjarkk%2Fyarql/lists"}