0

I use gqlgen library in my Golang project to generate resolvers and models from GraphQl files. In my project, I use a mechanism to generate a query from models generated by gqlgen.

All things are true when that model doesn't have relational fields with himself(circle).

I'm looking for some way to don't generate those fields, In other words, I don't need that fields to be in the model. I need only its resolvers.

for example, this is my GraphQl file:

type Menu {
    ID: Int!
    SubMenu: Menu!
    }

and this is the generated model:

type Menu struct {
    ID           int            `json:"ID"`
    SubMenu      *Menu       `json:"Product"`
}

I need only the resolvers for the SubMenu field. So my gqlgen.yml file is the same as:


models:
  Menu:
    fields:
      SubMenu:
          resolver: true


Is there any way to tell the gqlgen to skip some fields in generating models? Or do you have another solution to handle it?

  • I'm not sure this is possible, because at some point the result has to be serialized to JSON and returned to the client. What's your use-case for omitting fields from the model? – blackgreen Jul 29 '21 at 08:06

1 Answers1

2

You can configure where the model will be generated in your gqlgen.yml such as:

model:
  filename: graph/model/models_gen.go
  package: model

But it does not mean you cannot add your own files in the same package as the one generated(here model). Therefore you can customize your model in these files and they will stay untouched during model generation. Also gqlgen code generation will not try to generate model struct that already exist in the package.

For instance, with the above example, if you declare your own Menu struct within the package model in a different file than the one specified in the gqlgen.yml it will not generate code for the model struct Menu in the generated file.

Example

https://github.com/99designs/gqlgen/tree/master/example/starwars/models

Matthieu
  • 36
  • 3