Skip to content
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

Support multiple data formats with one struct #824

Closed
hboylan opened this issue Aug 9, 2019 · 2 comments
Closed

Support multiple data formats with one struct #824

hboylan opened this issue Aug 9, 2019 · 2 comments
Labels

Comments

@hboylan
Copy link

hboylan commented Aug 9, 2019

What happened?

My resolver returns JSON data using snake case, but my GQL data adheres to the standard camel case format. This forces me to define 2 separate types for each model in order to support the schema for my resolver and GQL client.

What did you expect?

The documentation mentions the use of a gqlgen struct tag to bind tags, but the json tag appears to override this feature. Basically, I expected the ability to define one model to support data in (json) and data out (gqlgen).

expected models.go

type Account struct {
	ID              int       `json:"id"`
	Name            string    `json:"name"`
	CreatedAt       time.Time `json:"created_at" gqlgen:"createdAt"`
	CreatedBy       string    `json:"created_by" gqlgen:"createdBy"`
}

Minimal graphql.schema and models to reproduce

schema.graphql

type Account {
  id Int!
  name String!
  createdAt String!
  createdBy String!
  ...
}

type Query {
  accounts: [Account!]!
}

models.go

type Account struct {
	ID              int       `json:"id"`
	Name            string    `json:"name"`
	CreatedAt       time.Time `json:"created_at"`
	CreatedBy       string    `json:"created_by"`
}

type GQLAccount struct {
	ID              int       `json:"id"`
	Name            string    `json:"name"`
	CreatedAt       time.Time `json:"createdAt"`
	CreatedBy       string    `json:"createdBy"`
}

Versions

  • gqlgen version? v0.9.1
  • go version? go version go1.12.5 darwin/amd64
  • dep or go modules? go modules
@stale
Copy link

stale bot commented Oct 8, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 8, 2019
@stale stale bot closed this as completed Oct 15, 2019
@AienTech
Copy link

any updated on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants