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

Tag mapping #212

Closed
theoks opened this issue Jul 21, 2018 · 6 comments
Closed

Tag mapping #212

theoks opened this issue Jul 21, 2018 · 6 comments

Comments

@theoks
Copy link

theoks commented Jul 21, 2018

In every model for my database I have these 3 fields:

_id
_key
_rev

In order for the marshalling and unmarshalling to work correctly I need to place these three tags on each golang struct:

json:"_id"
json:"_key"
json:"_rev"

(Note that these three properties are required by the database, so I cannot change the tags)

When I autogenerate the graphql type the json tags are also autogenerated. The issue here is that the database variables start with underscore, and all types in Go that start with an underscore are not exported (thus I can't declare a field to start with an underscore, so as that I could have an autogenerated json tag starting with underscore)

Is there any way to bypass this issue? In case there isn't, would it be possible to have an entry in the configuration file, where we could map certain fields with certain tags (and have the rest autogenerated)?

GraphQL type:

type User {
  id: ID!
  key: ID!
  rev: String!
}

Autogenerated Go struct:

type User struct {
	ID          string     `json:"id"`
	Key         string     `json:"key"`
	Rev         string     `json:"rev"`
}
@vektah
Copy link
Collaborator

vektah commented Jul 23, 2018

Model generation isn't really set up to cover every use case. If you want something unusual you'll have to build the models yourself and reference them in the config.

At some point in the future the templates might be configurable, but for now there are other features that have a higher priority (and have less conflicting requirements from different people)

@theoks
Copy link
Author

theoks commented Jul 23, 2018

Unfortunately many databases use properties starting with underscore (MongoDB also uses _id) and this practice collides with Go's marshalling and unmarshalling process.

@vektah
Copy link
Collaborator

vektah commented Jul 23, 2018

You are free to build the struct yourself however you want:

type User struct {
   ID string  json:"_id"
   Key string json:"_key"
   Rev string json:"_rev"
}

and bind it to a graphql object from schema

type User {
  id: ID!
  key: ID!
  rev: String!
}

with the config:

models:
  User:
    model: github.com/my/app/models.User

This shouldn't a blocker for anything, it just means you need to write out the structs yourself.

@theoks
Copy link
Author

theoks commented Jul 23, 2018

Yes I figured, however we use a couple of models with more than a hundred fields, and we will then have to manually keep them in sync with the GraphQL models (which have the same number of fields) and the error margin increases this way (that's why we found the autogenerate feature really nice).

We may try to provide a PR. Maybe an extra field in the config yaml file named GlobalTagMapping. It may be fairly simple to provide some field names that require different tags across the whole project and have object generation take that into consideration.

@vvakame
Copy link
Collaborator

vvakame commented Oct 25, 2018

Is this Issue over?

@vektah vektah closed this as completed Oct 25, 2018
@domsn
Copy link

domsn commented Sep 20, 2019

@vvakame You can use inline-config-with-directives, fox example:

type User {
  _id: String! @goField(name: "id")
}

# regenerate using below command
# go generate ./...
# and you will see the generated code

type User struct {
    ID       string  `json:"_id"`
}

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

No branches or pull requests

4 participants