Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from ConsultingMD/gr-gqlentity-servicename
Browse files Browse the repository at this point in the history
[CARESERV-928] Renaming Entity in gqlgen to avoid type name conflict
  • Loading branch information
AndrewRayCode authored Jun 30, 2021
2 parents 07c1f93 + e4c7206 commit 3dbf121
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 73 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# Grand Rounds Fork of gqlgen

This fork was created by:

1. Forking the repository
1. Resetting master of the fork to the tagged commit of the last stable release
of gqlgen. At the time of writing this, it's tag v0.13.0 and commit 07c1f93.
1. Pushing -f to reset master of this fork the above commit.
1. Creating a branch in this repo and applying the desired patch.
1. Runing `go generate ./...` in the root of this repository.
1. Opening a pull request of the patch we've applied, to this fork
1. Testing the forked code in the downstream repo (bobs) using `replace` in
`go.mod`.
1. Merging the patch into the master branch of this fork.
1. Updating downstream consumers to use the master branch of this fork using
`replace` in `go.mod`

Note that means the master branch of this fork diverges from the official
repository. To update this fork to the latest version of gqlgen:

1. Merge the latest version tagged commit of upstream into master of this fork.
**Don't** merge in the master branch of the main repository, just the commit
of the next tagged version.

# 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)

![gqlgen](https://user-images.githubusercontent.com/46195831/89802919-0bb8ef00-db2a-11ea-8ba4-88e7a58b2fd2.png)
Expand Down Expand Up @@ -85,6 +109,25 @@ func (r *userResolver) Friends(ctx context.Context, obj *User) ([]*User, error)
}
```

You can also use inline config with directives to achieve the same result

```graphql
directive @goModel(model: String, models: [String!]) on OBJECT
| INPUT_OBJECT
| SCALAR
| ENUM
| INTERFACE
| UNION
directive @goField(forceResolver: Boolean, name: String) on INPUT_FIELD_DEFINITION
| FIELD_DEFINITION
type User @goModel(model: "github.com/you/pkg/model.User") {
id: ID! @goField(name: "todoId")
friends: [User!]! @goField(forceResolver: true)
}
```

### Can I change the type of the ID from type String to Type Int?

Yes! You can by remapping it in config as seen below:
Expand Down
15 changes: 15 additions & 0 deletions codegen/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,25 @@ func Call(p *types.Func) string {
return pkg + p.Name()
}

// Federated graphs have the types _Entity and _Service. With default Go struct
// name generation, those become `Service`, which can conflict with users
// of this library who have their own `type Service`. Special case handle the
// federated entity names
var internalNames = map[string]string{
"_Entity": "GqlgenEntity",
"_Service": "GqlgenService",
}

func ToGo(name string) string {
if name == "_" {
return "_"
}

internalName, hasInternalName := internalNames[name]
if hasInternalName {
return internalName
}

runes := make([]rune, 0, len(name))

wordWalker(name, func(info *wordInfo) {
Expand Down
5 changes: 5 additions & 0 deletions codegen/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func TestToGo(t *testing.T) {
require.Equal(t, "RelatedUrls", ToGo("RelatedUrls"))
require.Equal(t, "ITicket", ToGo("ITicket"))
require.Equal(t, "FooTicket", ToGo("fooTicket"))

require.Equal(t, "GqlgenEntity", ToGo("_Entity"))
require.Equal(t, "GqlgenService", ToGo("_Service"))
require.Equal(t, "Entity", ToGo("Entity"))
require.Equal(t, "Service", ToGo("Service"))
}

func TestToGoPrivate(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions example/federation/accounts/graph/generated/federation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions example/federation/accounts/graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/federation/accounts/graph/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions example/federation/products/graph/generated/federation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions example/federation/products/graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/federation/products/graph/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3dbf121

Please sign in to comment.