graphql-go / graphql-go/graphql

constructing graphql schema using reflection

Open
#515 7 comments 1 reaction 0 assignees View on GitHub
Dominant language
Go
Stars
10.1k
Forks
845
PR merge metrics
No merged PRs in 30d

Description

I'm always shocked by the boilerplate I need to write to add simple structs to my schema.

What I did is: I started to write some helper methods to analyze my structs via reflection and generate the gql types from my go structs.

Looks like this:
```
type Model struct {
Id int64 `gorm:"type:bigserial;primary_key" json:"id"`
CreatedAt time.Time `sql:"DEFAULT:NOW()" json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

type IntegrationHttp struct {
Model
App *model.AppInstance `gorm:"not null;foreignkey:id;association_foreignkey:AppId" json:"app"`
AppId int64 `gorm:"not null" json:"appId"`

Method string `json:"method"`
Url string `json:"url"`
Skipped string `json:"-"`
}

func TestNewTypeFromStruct(t *testing.T) {
conf := schema.NewTypeFromStruct(IntegrationHttp{})

assert.Equal(t, "IntegrationHttp", conf.Name())

confFields := conf.Fields()
assert.Equal(t, 7, len(confFields))
}
```

Yet it takes the fields names from the `json` tag, could be configurable as well.
I also added a custom tag `gql` where you can override the type of a field with e.g. `gql:"type:String"`.

I can Imagine a bunch of more features. Since the code runs only once at startup time, I do not see any performance issues but reducing a lot of boilerplate code.

Now my questions are:
* **Is this kind of struct generation interesting for this library?**
* **Is it interesting in general (I could start another repository)?**

Or do you see any general issues with that and I should keep it private? Else I would love to share the implementation and would be happy to further improve it.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.