github.com/Kartograf/gqlgen@v0.7.2/docs/content/reference/resolvers.md (about) 1 --- 2 linkTitle: Resolvers 3 title: Resolving grapqhQL requests 4 description: Different ways of binding graphQL requests to resolvers 5 menu: { main: { parent: 'reference' } } 6 --- 7 8 There are multiple ways that a graphQL type can be bound to a Go struct that allows for many usecases. 9 10 11 ## Bind directly to struct field names 12 This is the most common use case where the names of the fields on the Go struct match the names of the 13 fields in the graphQL type. If a Go struct field is unexported, it will not be bound to the graphQL type. 14 15 ```go 16 type Car struct { 17 Make string 18 Model string 19 Color string 20 OdometerReading int 21 } 22 ``` 23 24 And then in your graphQL schema: 25 ```graphql 26 type Car { 27 make: String! 28 model: String! 29 color: String! 30 odometerReading: Int! 31 } 32 ``` 33 34 And in the gqlgen config file: 35 ```yaml 36 models: 37 Car: 38 model: github.com/my/app/models.Car 39 ``` 40 41 In this case, each filed in the graphQL type will be bound to the respective field on the go struct 42 ignoring the case of the fields 43 44 45 ## Bind to a method name 46 47 This is also very common use case that comes up where we want to bind a graphQL field to a Go struct method 48 49 ```go 50 type Person { 51 Name string 52 } 53 54 type Car struct { 55 Make string 56 Model string 57 Color string 58 OwnerID *string 59 OdometerReading int 60 } 61 62 func (c *Car) Owner() (*Person) { 63 // get the car owner 64 //.... 65 return owner 66 } 67 ``` 68 69 And then in your graphQL schema: 70 ```graphql 71 type Car { 72 make: String! 73 model: String! 74 color: String! 75 odometerReading: Int! 76 owner: Person 77 } 78 ``` 79 80 And in the gqlgen config file: 81 ```yaml 82 models: 83 Car: 84 model: github.com/my/app/models.Car 85 Person: 86 model: github.com/my/app/models.Person 87 ``` 88 89 Here, we see that there is a method on car with the name ```Owner```, thus the ```Owner``` function will be called if 90 a graphQL request includes that field to be resolved. 91 92 Model methods can optionally take a context as their first argument. If a 93 context is required, the model method will also be run in parallel. 94 95 ## Bind when the field names do not match 96 97 There are two ways you can bind to fields when the the Go struct and the graphQL type do not match. 98 99 100 The first way is you can bind resolvers to a struct based off of struct tags like the following: 101 102 ```go 103 type Car struct { 104 Make string 105 ShortState string 106 LongState string `gqlgen:"state"` 107 Model string 108 Color string 109 OdometerReading int 110 } 111 ``` 112 113 And then in your graphQL schema: 114 ```graphql 115 type Car { 116 make: String! 117 model: String! 118 state: String! 119 color: String! 120 odometerReading: Int! 121 } 122 ``` 123 124 And in the gqlgen config file add the line: 125 ```yaml 126 struct_tag: gqlgen 127 128 models: 129 Car: 130 model: github.com/my/app/models.Car 131 ``` 132 133 Here even though the graphQL type and Go struct have different field names, there is a Go struct tag field on ```longState``` 134 that matches and thus ```state``` will be bound to ```LongState```. 135 136 137 The second way you can bind fields is by adding a line into the config file such as: 138 ```go 139 type Car struct { 140 Make string 141 ShortState string 142 LongState string 143 Model string 144 Color string 145 OdometerReading int 146 } 147 ``` 148 149 And then in your graphQL schema: 150 ```graphql 151 type Car { 152 make: String! 153 model: String! 154 state: String! 155 color: String! 156 odometerReading: Int! 157 } 158 ``` 159 160 And in the gqlgen config file add the line: 161 ```yaml 162 models: 163 Car: 164 model: github.com/my/app/models.Car 165 fields: 166 state: 167 fieldName: LongState 168 ``` 169 170 ## Binding to Anonymous or Embedded Structs 171 All of the rules from above apply to a struct that has an embedded struct. 172 Here is an example 173 ```go 174 type Truck { 175 Car 176 177 Is4x4 bool 178 } 179 180 type Car struct { 181 Make string 182 ShortState string 183 LongState string 184 Model string 185 Color string 186 OdometerReading int 187 } 188 ``` 189 190 And then in your graphQL schema: 191 ```graphql 192 type Truck { 193 make: String! 194 model: String! 195 state: String! 196 color: String! 197 odometerReading: Int! 198 is4x4: Bool! 199 } 200 ``` 201 202 Here all the fields from the Go struct Car will still be bound to the respective fields in the graphQL schema that match 203 204 Embedded structs are a good way to create thin wrappers around data access types an example would be: 205 206 ```go 207 type Cat struct { 208 db.Cat 209 //... 210 } 211 212 func (c *Cat) ID() string { 213 // return a custom id based on the db shard and the cat's id 214 return fmt.Sprintf("%d:%d", c.Shard, c.Id) 215 } 216 ``` 217 218 Which would correlate with a gqlgen config file of: 219 ```yaml 220 models: 221 Cat: 222 model: github.com/my/app/models.Cat 223 ``` 224 225 ## Binding Priority 226 If a ```struct_tags``` config exists, then struct tag binding has the highest priority over all other types of binding. 227 In all other cases, the first Go struct field found that matches the graphQL type field will be the field that is bound.