-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update extra fields type definition and plus docs about the feature (#…
…2655) * Update extra fields type definition and plus docs about the feature * Update docs
- Loading branch information
Showing
10 changed files
with
148 additions
and
42 deletions.
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
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,47 @@ | ||
--- | ||
title: "Generation of additional extra fields for internal use" | ||
description: Pass implementation specific fields to the children resolvers without being forced to define your own types for a GraphQL model. | ||
linkTitle: Generated Model Extra Fields | ||
menu: { main: { parent: 'recipes' } } | ||
--- | ||
|
||
Extra fields allows you to generate additional fields for your models. | ||
These fields can be used at runtime when implementing field resolvers. | ||
|
||
## Extending your models | ||
Imagine you have a model named User and you want to extend a generated struct with additional data used in your service. | ||
|
||
The schema is: | ||
|
||
```graphql | ||
type User { | ||
id: ID! | ||
name: String! | ||
} | ||
``` | ||
|
||
Extra fields can be defined in gqlgen.yaml configuration: | ||
|
||
```yaml | ||
models: | ||
User: | ||
extraFields: | ||
Session: | ||
description: "A Session used by this user" | ||
type: "github.com/author/mypkg.Session" | ||
``` | ||
|
||
The generated code would look like: | ||
|
||
```go | ||
// Code generated by github.com/99designs/gqlgen, DO NOT EDIT. | ||
|
||
type User struct { | ||
ID string | ||
Name string | ||
// A Session used by this user. | ||
Session mypkg.Session | ||
} | ||
``` | ||
|
||
After these steps you have an extra field for your server implementation and the field is not being exposed to a caller. |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 1 addition & 8 deletions
9
plugin/modelgen/out_enable_model_json_omitempty_tag_false/generated.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,47 @@ | ||
package modelgen | ||
|
||
import ( | ||
"go/types" | ||
"strings" | ||
) | ||
|
||
// buildType constructs a types.Type for the given string (using the syntax | ||
// from the extra field config Type field). | ||
func buildType(typeString string) types.Type { | ||
switch { | ||
case typeString[0] == '*': | ||
return types.NewPointer(buildType(typeString[1:])) | ||
case strings.HasPrefix(typeString, "[]"): | ||
return types.NewSlice(buildType(typeString[2:])) | ||
default: | ||
return buildNamedType(typeString) | ||
} | ||
} | ||
|
||
// buildNamedType returns the specified named or builtin type. | ||
// | ||
// Note that we don't look up the full types.Type object from the appropriate | ||
// package -- gqlgen doesn't give us the package-map we'd need to do so. | ||
// Instead we construct a placeholder type that has all the fields gqlgen | ||
// wants. This is roughly what gqlgen itself does, anyway: | ||
// https://github.com/99designs/gqlgen/blob/master/plugin/modelgen/models.go#L119 | ||
func buildNamedType(fullName string) types.Type { | ||
dotIndex := strings.LastIndex(fullName, ".") | ||
if dotIndex == -1 { // builtinType | ||
return types.Universe.Lookup(fullName).Type() | ||
} | ||
|
||
// type is pkg.Name | ||
pkgPath := fullName[:dotIndex] | ||
typeName := fullName[dotIndex+1:] | ||
|
||
pkgName := pkgPath | ||
slashIndex := strings.LastIndex(pkgPath, "/") | ||
if slashIndex != -1 { | ||
pkgName = pkgPath[slashIndex+1:] | ||
} | ||
|
||
pkg := types.NewPackage(pkgPath, pkgName) | ||
// gqlgen doesn't use some of the fields, so we leave them 0/nil | ||
return types.NewNamed(types.NewTypeName(0, pkg, typeName, nil), nil, nil) | ||
} |