-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds docs for how resolvers are bound #338
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
--- | ||
linkTitle: Resolvers | ||
title: Resolving grapqhQL requests | ||
description: Different ways of binding graphQL requests to resolvers | ||
menu: { main: { parent: 'reference' } } | ||
--- | ||
|
||
There are multiple ways that a graphQL type can be bound to a Go struct that allows for many usecases. | ||
|
||
|
||
## Bind directly to struct field names | ||
This is the most common use case where the names of the fields on the Go struct match the names of the | ||
fields in the graphQL type. If a Go struct field is unexported, it will not be bound to the graphQL type. | ||
|
||
```go | ||
type Car struct { | ||
Make string | ||
Model string | ||
Color string | ||
OdometerReading int | ||
} | ||
``` | ||
|
||
And then in your graphQL schema: | ||
```graphql | ||
type Car { | ||
make: String! | ||
model: String! | ||
color: String! | ||
odometerReading: Int! | ||
} | ||
``` | ||
|
||
And in the gqlgen config file: | ||
```yaml | ||
models: | ||
Car: | ||
model: github.com/my/app/models.Car | ||
``` | ||
|
||
In this case, each filed in the graphQL type will be bound to the respective field on the go struct | ||
ignoring the case of the fields | ||
|
||
|
||
## Bind to a method name | ||
|
||
This is also very common use case that comes up where we want to bind a graphQL field to a Go struct method | ||
|
||
```go | ||
type Person { | ||
Name string | ||
} | ||
|
||
type Car struct { | ||
Make string | ||
Model string | ||
Color string | ||
OwnerID *string | ||
OdometerReading int | ||
} | ||
|
||
func (c *Car) Owner() (*Person) { | ||
// get the car owner | ||
//.... | ||
return owner | ||
} | ||
``` | ||
|
||
And then in your graphQL schema: | ||
```graphql | ||
type Car { | ||
make: String! | ||
model: String! | ||
color: String! | ||
odometerReading: Int! | ||
owner: Person | ||
} | ||
``` | ||
|
||
And in the gqlgen config file: | ||
```yaml | ||
models: | ||
Car: | ||
model: github.com/my/app/models.Car | ||
Person: | ||
model: github.com/my/app/models.Person | ||
``` | ||
|
||
Here, we see that there is a method on car with the name ```Owner```, thus the ```Owner``` function will be called if | ||
a graphQL request includes that field to be resolved | ||
|
||
## Bind when the field names do not match | ||
|
||
There are two ways you can bind to fields when the the Go struct and the graphQL type do not match. | ||
|
||
|
||
The first way is you can bind resolvers to a struct based off of struct tags like the following: | ||
|
||
```go | ||
type Car struct { | ||
Make string | ||
ShortState string | ||
LongState string `gqlgen:"state"` | ||
Model string | ||
Color string | ||
OdometerReading int | ||
} | ||
``` | ||
|
||
And then in your graphQL schema: | ||
```graphql | ||
type Car { | ||
make: String! | ||
model: String! | ||
state: String! | ||
color: String! | ||
odometerReading: Int! | ||
} | ||
``` | ||
|
||
And in the gqlgen config file add the line: | ||
```yaml | ||
struct_tag: gqlgen | ||
|
||
models: | ||
Car: | ||
model: github.com/my/app/models.Car | ||
``` | ||
|
||
Here even though the graphQL type and Go struct have different field names, there is a Go struct tag field on ```longState``` | ||
That matches and thus ```state``` will be bound to ```LongState``` | ||
|
||
|
||
The second way you can bind fields is by adding a line into the config file such as: | ||
```go | ||
type Car struct { | ||
Make string | ||
ShortState string | ||
LongState string | ||
Model string | ||
Color string | ||
OdometerReading int | ||
} | ||
``` | ||
|
||
And then in your graphQL schema: | ||
```graphql | ||
type Car { | ||
make: String! | ||
model: String! | ||
state: String! | ||
color: String! | ||
odometerReading: Int! | ||
} | ||
``` | ||
|
||
And in the gqlgen config file add the line: | ||
```yaml | ||
models: | ||
Car: | ||
model: github.com/my/app/models.Car | ||
fields: | ||
state: | ||
fieldName: LongState | ||
``` | ||
|
||
## Binding to Anonymous or Embedded Structs | ||
All of the rules from above apply to a struct that has an embedded struct. | ||
|
||
```go | ||
type Truck { | ||
car | ||
|
||
is4x4 bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
} | ||
|
||
type Car struct { | ||
Make string | ||
ShortState string | ||
LongState string | ||
Model string | ||
Color string | ||
OdometerReading int | ||
} | ||
``` | ||
|
||
And then in your graphQL schema: | ||
```graphql | ||
type Truck { | ||
make: String! | ||
model: String! | ||
state: String! | ||
color: String! | ||
odometerReading: Int! | ||
is4x4: Bool! | ||
} | ||
``` | ||
vektah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Here all the fields from the Go struct Car will still be bound to the respective fields in the graphQL schema that match | ||
|
||
## Binding Priority | ||
If a ```struct_tags``` config exists, then struct tag binding has the highest priority over all other types of binding. | ||
In all other cases, the first Go struct field found that matches the graphQL type field will be the field that is bound. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
Car
?