github.com/spread-ai/gqlgen@v0.0.0-20221124102857-a6c8ef538a1d/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/generated/federation.go
    27    package: generated
    28  
    29  # Where should any generated models go?
    30  model:
    31    filename: graph/model/models_gen.go
    32    package: model
    33  
    34  # Where should the resolver implementations go?
    35  resolver:
    36    layout: follow-schema
    37    dir: graph
    38    package: graph
    39    filename_template: "{name}.resolvers.go"
    40  
    41  # Optional: turn on use ` + "`" + `gqlgen:"fieldName"` + "`" + ` tags in your models
    42  # struct_tag: json
    43  
    44  # Optional: turn on to use []Thing instead of []*Thing
    45  # omit_slice_element_pointers: false
    46  
    47  # Optional: turn off to make struct-type struct fields not use pointers
    48  # e.g. type Thing struct { FieldA OtherThing } instead of { FieldA *OtherThing }
    49  # struct_fields_always_pointers: true
    50  
    51  # Optional: turn off to make resolvers return values instead of pointers for structs
    52  # resolvers_always_return_pointers: true
    53  
    54  # Optional: turn on to return pointers instead of values in unmarshalInput
    55  # return_pointers_in_unmarshalinput: false
    56  
    57  # Optional: set to speed up generation time by not performing a final validation pass.
    58  # skip_validation: true
    59  
    60  # Optional: set to skip running `go mod tidy` when generating server code
    61  # skip_mod_tidy: true
    62  
    63  # gqlgen will search for any type names in the schema in these go packages
    64  # if they match it will use them, otherwise it will generate them.
    65  # autobind:
    66  #   - "github.com/[YOUR_APP_DIR]/graph/model"
    67  
    68  # This section declares type mapping between the GraphQL and go type systems
    69  #
    70  # The first line in each type will be used as defaults for resolver arguments and
    71  # modelgen, the others will be allowed when binding to fields. Configure them to
    72  # your liking
    73  models:
    74    ID:
    75      model:
    76        - github.com/spread-ai/gqlgen/graphql.ID
    77        - github.com/spread-ai/gqlgen/graphql.Int
    78        - github.com/spread-ai/gqlgen/graphql.Int64
    79        - github.com/spread-ai/gqlgen/graphql.Int32
    80    Int:
    81      model:
    82        - github.com/spread-ai/gqlgen/graphql.Int
    83        - github.com/spread-ai/gqlgen/graphql.Int64
    84        - github.com/spread-ai/gqlgen/graphql.Int32
    85  ```
    86  
    87  Everything has defaults, so add things as you need.
    88  
    89  ## Inline config with directives
    90  
    91  gqlgen ships with some builtin directives that make it a little easier to manage wiring.
    92  
    93  To start using them you first need to define them:
    94  
    95  ```graphql
    96  directive @goModel(
    97  	model: String
    98  	models: [String!]
    99  ) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION
   100  
   101  directive @goField(
   102  	forceResolver: Boolean
   103  	name: String
   104  ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
   105  
   106  directive @goTag(
   107  	key: String!
   108  	value: String
   109  ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
   110  ```
   111  
   112  > Here be dragons
   113  >
   114  > gqlgen doesnt currently support user-configurable directives for SCALAR, ENUM, INTERFACE or UNION. This only works
   115  > for internal directives. You can track the progress [here](https://github.com/spread-ai/gqlgen/issues/760)
   116  
   117  Now you can use these directives when defining types in your schema:
   118  
   119  ```graphql
   120  type User @goModel(model: "github.com/my/app/models.User") {
   121  	id: ID! @goField(name: "todoId")
   122  	name: String!
   123  		@goField(forceResolver: true)
   124  		@goTag(key: "xorm", value: "-")
   125  		@goTag(key: "yaml")
   126  }
   127  ```
   128  
   129  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.
   130  
   131  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`.
   132  
   133  e.g. a custom directive called `constraint` would be set as `skip_runtime` using the following configuration
   134  ```yml
   135  # custom directives which are not exposed during introspection. These directives are
   136  # used for code generation only
   137  directives:
   138    constraint:
   139      skip_runtime: true
   140  ```