github.com/99designs/gqlgen@v0.17.45/docs/content/config.md (about)

     1  ---
     2  linkTitle: Configuration
     3  title: How to configure gqlgen using gqlgen.yml
     4  description: How to configure gqlgen using gqlgen.yml
     5  menu: main
     6  weight: -5
     7  ---
     8  
     9  gqlgen can be configured using a `gqlgen.yml` file, by default it will be loaded from the current directory, or any parent directory.
    10  
    11  Example:
    12  
    13  ```yml
    14  # Where are all the schema files located? globs are supported eg  src/**/*.graphqls
    15  schema:
    16    - graph/*.graphqls
    17  
    18  # Where should the generated server code go?
    19  exec:
    20    layout: follow-schema
    21    dir: graph/generated
    22    package: generated
    23  
    24  # Enable Apollo federation support
    25  federation:
    26    filename: graph/federation.go
    27    package: graph
    28  
    29  # Where should any generated models go?
    30  model:
    31    filename: graph/model/models_gen.go
    32    package: model
    33    # Optional: Pass in a path to a new gotpl template to use for generating the models
    34    # model_template: [your/path/model.gotpl]
    35  
    36  # Where should the resolver implementations go?
    37  resolver:
    38    layout: follow-schema
    39    dir: graph
    40    package: graph
    41    filename_template: "{name}.resolvers.go"
    42    # Optional: turn on to not generate template comments above resolvers
    43    # omit_template_comment: false
    44    # Optional: Pass in a path to a new gotpl template to use for generating resolvers
    45    # resolver_template: [your/path/resolver.gotpl]
    46  
    47  # Optional: turn on use ` + "`" + `gqlgen:"fieldName"` + "`" + ` tags in your models
    48  # struct_tag: json
    49  
    50  # Optional: turn on to use []Thing instead of []*Thing
    51  # omit_slice_element_pointers: false
    52  
    53  # Optional: turn on to omit Is<Name>() methods to interface and unions
    54  # omit_interface_checks : true
    55  
    56  # Optional: turn on to skip generation of ComplexityRoot struct content and Complexity function
    57  # omit_complexity: false
    58  
    59  # Optional: turn on to not generate any file notice comments in generated files
    60  # omit_gqlgen_file_notice: false
    61  
    62  # Optional: turn on to exclude the gqlgen version in the generated file notice. No effect if `omit_gqlgen_file_notice` is true.
    63  # omit_gqlgen_version_in_file_notice: false
    64  
    65  # Optional: turn on to exclude root models such as Query and Mutation from the generated models file.
    66  # omit_root_models: false
    67  
    68  # Optional: turn on to exclude resolver fields from the generated models file.
    69  # omit_resolver_fields: false
    70  
    71  # Optional: turn off to make struct-type struct fields not use pointers
    72  # e.g. type Thing struct { FieldA OtherThing } instead of { FieldA *OtherThing }
    73  # struct_fields_always_pointers: true
    74  
    75  # Optional: turn off to make resolvers return values instead of pointers for structs
    76  # resolvers_always_return_pointers: true
    77  
    78  # Optional: turn on to return pointers instead of values in unmarshalInput
    79  # return_pointers_in_unmarshalinput: false
    80  
    81  # Optional: wrap nullable input fields with Omittable
    82  # nullable_input_omittable: true
    83  
    84  # Optional: turn on to return pointers instead of values in unmarshalInput
    85  # return_pointers_in_unmarshalinput: false
    86  
    87  # Optional: set to speed up generation time by not performing a final validation pass.
    88  # skip_validation: true
    89  
    90  # Optional: set to skip running `go mod tidy` when generating server code
    91  # skip_mod_tidy: true
    92  
    93  # Optional: set build tags that will be used to load packages
    94  # go_build_tags:
    95  #  - private
    96  #  - enterprise
    97  
    98  # Optional: set to modify the initialisms regarded for Go names
    99  # go_initialisms:
   100  #   replace_defaults: false # if true, the default initialisms will get dropped in favor of the new ones instead of being added
   101  #   initialisms: # List of initialisms to for Go names
   102  #     - 'CC'
   103  #     - 'BCC'
   104  
   105  # gqlgen will search for any type names in the schema in these go packages
   106  # if they match it will use them, otherwise it will generate them.
   107  # autobind:
   108  #   - "github.com/[YOUR_APP_DIR]/graph/model"
   109  
   110  # This section declares type mapping between the GraphQL and go type systems
   111  #
   112  # The first line in each type will be used as defaults for resolver arguments and
   113  # modelgen, the others will be allowed when binding to fields. Configure them to
   114  # your liking
   115  models:
   116    ID:
   117      model:
   118        - github.com/99designs/gqlgen/graphql.ID
   119        - github.com/99designs/gqlgen/graphql.Int
   120        - github.com/99designs/gqlgen/graphql.Int64
   121        - github.com/99designs/gqlgen/graphql.Int32
   122    Int:
   123      model:
   124        - github.com/99designs/gqlgen/graphql.Int
   125        - github.com/99designs/gqlgen/graphql.Int64
   126        - github.com/99designs/gqlgen/graphql.Int32
   127    UUID:
   128      model:
   129        - github.com/99designs/gqlgen/graphql.UUID
   130  ```
   131  
   132  Everything has defaults, so add things as you need.
   133  
   134  ## Inline config with directives
   135  
   136  gqlgen ships with some builtin directives that make it a little easier to manage wiring.
   137  
   138  To start using them you first need to define them:
   139  
   140  ```graphql
   141  directive @goModel(
   142  	model: String
   143  	models: [String!]
   144  	forceGenerate: Boolean
   145  ) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION
   146  
   147  directive @goField(
   148  	forceResolver: Boolean
   149  	name: String
   150  	omittable: Boolean
   151  ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
   152  
   153  directive @goTag(
   154  	key: String!
   155  	value: String
   156  ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
   157  ```
   158  
   159  > Here be dragons
   160  >
   161  > gqlgen doesnt currently support user-configurable directives for SCALAR, ENUM, INTERFACE or UNION. This only works
   162  > for internal directives. You can track the progress [here](https://github.com/99designs/gqlgen/issues/760)
   163  
   164  Now you can use these directives when defining types in your schema:
   165  
   166  ```graphql
   167  type User @goModel(model: "github.com/my/app/models.User") {
   168  	id: ID! @goField(name: "todoId")
   169  	name: String!
   170  		@goField(forceResolver: true)
   171  		@goTag(key: "xorm", value: "-")
   172  		@goTag(key: "yaml")
   173  }
   174  
   175  # This make sense when autobind activated.
   176  type Person @goModel(forceGenerate: true) {
   177  	id: ID!
   178  	name: String!
   179  }
   180  ```
   181  
   182  The builtin directives `goField`, `goModel` and `goTag` are automatically registered to `skip_runtime`. Any directives registered as `skip_runtime` will not exposed during introspection and are used during code generation only.
   183  
   184  If you have created a new code generation plugin using a directive which does not require runtime execution, the directive will need to be set to `skip_runtime`.
   185  
   186  e.g. a custom directive called `constraint` would be set as `skip_runtime` using the following configuration
   187  
   188  ```yml
   189  # custom directives which are not exposed during introspection. These directives are
   190  # used for code generation only
   191  directives:
   192    constraint:
   193      skip_runtime: true
   194  ```