git.sr.ht/~sircmpwn/gqlgen@v0.0.0-20200522192042-c84d29a1c940/README.md (about) 1 # gqlgen [![Continuous Integration](https://git.sr.ht/~sircmpwn/gqlgen/workflows/Continuous%20Integration/badge.svg)](https://git.sr.ht/~sircmpwn/gqlgen/actions) [![Read the Docs](https://badgen.net/badge/docs/available/green)](http://gqlgen.com/) 2 3 ## What is gqlgen? 4 5 [gqlgen](https://git.sr.ht/~sircmpwn/gqlgen) is a Go library for building GraphQL servers without any fuss. gqlgen is: 6 7 - **Schema first** — Define your API using the GraphQL [Schema Definition Language](http://graphql.org/learn/schema/). 8 - **Type safe** — You should never see `map[string]interface{}` here. 9 - **Codegen** — Let us generate the boring bits, so you can build your app quickly. 10 11 [Feature Comparison](https://gqlgen.com/feature-comparison/) 12 13 ## Getting Started 14 15 First work your way through the [Getting Started](https://gqlgen.com/getting-started/) tutorial. 16 17 If you can't find what your looking for, look at our [examples](https://git.sr.ht/~sircmpwn/gqlgen/tree/master/example) for example usage of gqlgen. 18 19 ## Reporting Issues 20 21 If you think you've found a bug, or something isn't behaving the way you think it should, please raise an [issue](https://git.sr.ht/~sircmpwn/gqlgen/issues) on GitHub. 22 23 ## Contributing 24 25 Read our [Contribution Guidelines](https://git.sr.ht/~sircmpwn/gqlgen/blob/master/CONTRIBUTING.md) for information on how you can help out gqlgen. 26 27 ## Frequently asked questions 28 29 ### How do I prevent fetching child objects that might not be used? 30 31 When you have nested or recursive schema like this: 32 33 ```graphql 34 type User { 35 id: ID! 36 name: String! 37 friends: [User!]! 38 } 39 ``` 40 41 You need to tell gqlgen that we should only fetch friends if the user requested it. There are two ways to do this. 42 43 #### Custom Models 44 45 Write a custom model that omits the Friends model: 46 47 ```go 48 type User struct { 49 ID int 50 Name string 51 } 52 ``` 53 54 And reference the model in `gqlgen.yml`: 55 56 ```yaml 57 # gqlgen.yml 58 models: 59 User: 60 model: github.com/you/pkg/model.User # go import path to the User struct above 61 ``` 62 63 #### Explicit Resolvers 64 65 If you want to Keep using the generated model: mark the field as requiring a resolver explicitly in `gqlgen.yml`: 66 67 ```yaml 68 # gqlgen.yml 69 models: 70 User: 71 fields: 72 friends: 73 resolver: true # force a resolver to be generated 74 ``` 75 76 After doing either of the above and running generate we will need to provide a resolver for friends: 77 78 ```go 79 func (r *userResolver) Friends(ctx context.Context, obj *User) ([]*User, error) { 80 // select * from user where friendid = obj.ID 81 return friends, nil 82 } 83 ``` 84 85 ### IDs are strings but I like ints, why cant I have ints? 86 87 You can by remapping it in config: 88 89 ```yaml 90 models: 91 ID: # The GraphQL type ID is backed by 92 model: 93 - git.sr.ht/~sircmpwn/gqlgen/graphql.IntID # An go integer 94 - git.sr.ht/~sircmpwn/gqlgen/graphql.ID # or a go string 95 ``` 96 97 This means gqlgen will be able to automatically bind to strings or ints for models you have written yourself, but the 98 first model in this list is used as the default type and it will always be used when: 99 100 - Generating models based on schema 101 - As arguments in resolvers 102 103 There isnt any way around this, gqlgen has no way to know what you want in a given context. 104 105 ## Other Resources 106 107 - [Christopher Biscardi @ Gophercon UK 2018](https://youtu.be/FdURVezcdcw) 108 - [Introducing gqlgen: a GraphQL Server Generator for Go](https://99designs.com.au/blog/engineering/gqlgen-a-graphql-server-generator-for-go/) 109 - [Dive into GraphQL by Iván Corrales Solera](https://medium.com/@ivan.corrales.solera/dive-into-graphql-9bfedf22e1a) 110 - [Sample Project built on gqlgen with Postgres by Oleg Shalygin](https://github.com/oshalygin/gqlgen-pg-todo-example)