github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/README.md (about) 1 # gqlgen [![Continuous Integration](https://github.com/99designs/gqlgen/workflows/Continuous%20Integration/badge.svg)](https://github.com/99designs/gqlgen/actions) [![Read the Docs](https://badgen.net/badge/docs/available/green)](http://gqlgen.com/) [![GoDoc](https://godoc.org/github.com/99designs/gqlgen?status.svg)](https://godoc.org/github.com/99designs/gqlgen) 2 3 ![gqlgen](https://user-images.githubusercontent.com/46195831/89802919-0bb8ef00-db2a-11ea-8ba4-88e7a58b2fd2.png) 4 5 ## What is gqlgen? 6 7 [gqlgen](https://github.com/99designs/gqlgen) is a Go library for building GraphQL servers without any fuss.<br/> 8 9 - **gqlgen is based on a Schema first approach** — You get to Define your API using the GraphQL [Schema Definition Language](http://graphql.org/learn/schema/). 10 - **gqlgen prioritizes Type safety** — You should never see `map[string]interface{}` here. 11 - **gqlgen enables Codegen** — We generate the boring bits, so you can focus on building your app quickly. 12 13 Still not convinced enough to use **gqlgen**? Compare **gqlgen** with other Go graphql [implementations](https://gqlgen.com/feature-comparison/) 14 15 ## Getting Started 16 - To install gqlgen run the command `go get github.com/99designs/gqlgen` in your project directory.<br/> 17 - You could initialize a new project using the recommended folder structure by running this command `go run github.com/99designs/gqlgen init`. 18 19 You could find a more comprehensive guide to help you get started [here](https://gqlgen.com/getting-started/).<br/> 20 We also have a couple of real-world [examples](https://github.com/99designs/gqlgen/tree/master/example) that show how to GraphQL applications with **gqlgen** seamlessly, 21 You can see these [examples](https://github.com/99designs/gqlgen/tree/master/example) here or visit [godoc](https://godoc.org/github.com/99designs/gqlgen). 22 23 ## Reporting Issues 24 25 If you think you've found a bug, or something isn't behaving the way you think it should, please raise an [issue](https://github.com/99designs/gqlgen/issues) on GitHub. 26 27 ## Contributing 28 29 We welcome contributions, Read our [Contribution Guidelines](https://github.com/99designs/gqlgen/blob/master/CONTRIBUTING.md) to learn more about contributing to **gqlgen** 30 ## Frequently asked questions 31 32 ### How do I prevent fetching child objects that might not be used? 33 34 When you have nested or recursive schema like this: 35 36 ```graphql 37 type User { 38 id: ID! 39 name: String! 40 friends: [User!]! 41 } 42 ``` 43 44 You need to tell gqlgen that it should only fetch friends if the user requested it. There are two ways to do this; 45 46 - #### Using Custom Models 47 48 Write a custom model that omits the friends field: 49 50 ```go 51 type User struct { 52 ID int 53 Name string 54 } 55 ``` 56 57 And reference the model in `gqlgen.yml`: 58 59 ```yaml 60 # gqlgen.yml 61 models: 62 User: 63 model: github.com/you/pkg/model.User # go import path to the User struct above 64 ``` 65 66 - #### Using Explicit Resolvers 67 68 If you want to Keep using the generated model, mark the field as requiring a resolver explicitly in `gqlgen.yml` like this: 69 70 ```yaml 71 # gqlgen.yml 72 models: 73 User: 74 fields: 75 friends: 76 resolver: true # force a resolver to be generated 77 ``` 78 79 After doing either of the above and running generate we will need to provide a resolver for friends: 80 81 ```go 82 func (r *userResolver) Friends(ctx context.Context, obj *User) ([]*User, error) { 83 // select * from user where friendid = obj.ID 84 return friends, nil 85 } 86 ``` 87 88 You can also use inline config with directives to achieve the same result 89 90 ```graphql 91 directive @goModel(model: String, models: [String!]) on OBJECT 92 | INPUT_OBJECT 93 | SCALAR 94 | ENUM 95 | INTERFACE 96 | UNION 97 98 directive @goField(forceResolver: Boolean, name: String) on INPUT_FIELD_DEFINITION 99 | FIELD_DEFINITION 100 101 type User @goModel(model: "github.com/you/pkg/model.User") { 102 id: ID! @goField(name: "todoId") 103 friends: [User!]! @goField(forceResolver: true) 104 } 105 ``` 106 107 ### Can I change the type of the ID from type String to Type Int? 108 109 Yes! You can by remapping it in config as seen below: 110 111 ```yaml 112 models: 113 ID: # The GraphQL type ID is backed by 114 model: 115 - github.com/99designs/gqlgen/graphql.IntID # An go integer 116 - github.com/99designs/gqlgen/graphql.ID # or a go string 117 ``` 118 119 This means gqlgen will be able to automatically bind to strings or ints for models you have written yourself, but the 120 first model in this list is used as the default type and it will always be used when: 121 122 - Generating models based on schema 123 - As arguments in resolvers 124 125 There isn't any way around this, gqlgen has no way to know what you want in a given context. 126 127 ## Other Resources 128 129 - [Christopher Biscardi @ Gophercon UK 2018](https://youtu.be/FdURVezcdcw) 130 - [Introducing gqlgen: a GraphQL Server Generator for Go](https://99designs.com.au/blog/engineering/gqlgen-a-graphql-server-generator-for-go/) 131 - [Dive into GraphQL by Iván Corrales Solera](https://medium.com/@ivan.corrales.solera/dive-into-graphql-9bfedf22e1a) 132 - [Sample Project built on gqlgen with Postgres by Oleg Shalygin](https://github.com/oshalygin/gqlgen-pg-todo-example)