github.com/shippio/gqlgen@v0.0.0-20220912092219-633ea699ef07/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: set to speed up generation time by not performing a final validation pass. 55 # skip_validation: true 56 57 # Optional: set to skip running `go mod tidy` when generating server code 58 # skip_mod_tidy: true 59 60 # gqlgen will search for any type names in the schema in these go packages 61 # if they match it will use them, otherwise it will generate them. 62 # autobind: 63 # - "github.com/[YOUR_APP_DIR]/graph/model" 64 65 # This section declares type mapping between the GraphQL and go type systems 66 # 67 # The first line in each type will be used as defaults for resolver arguments and 68 # modelgen, the others will be allowed when binding to fields. Configure them to 69 # your liking 70 models: 71 ID: 72 model: 73 - github.com/99designs/gqlgen/graphql.ID 74 - github.com/99designs/gqlgen/graphql.Int 75 - github.com/99designs/gqlgen/graphql.Int64 76 - github.com/99designs/gqlgen/graphql.Int32 77 Int: 78 model: 79 - github.com/99designs/gqlgen/graphql.Int 80 - github.com/99designs/gqlgen/graphql.Int64 81 - github.com/99designs/gqlgen/graphql.Int32 82 ``` 83 84 Everything has defaults, so add things as you need. 85 86 ## Inline config with directives 87 88 gqlgen ships with some builtin directives that make it a little easier to manage wiring. 89 90 To start using them you first need to define them: 91 92 ```graphql 93 directive @goModel( 94 model: String 95 models: [String!] 96 ) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION 97 98 directive @goField( 99 forceResolver: Boolean 100 name: String 101 ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION 102 103 directive @goTag( 104 key: String! 105 value: String 106 ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION 107 ``` 108 109 > Here be dragons 110 > 111 > gqlgen doesnt currently support user-configurable directives for SCALAR, ENUM, INTERFACE or UNION. This only works 112 > for internal directives. You can track the progress [here](https://github.com/99designs/gqlgen/issues/760) 113 114 Now you can use these directives when defining types in your schema: 115 116 ```graphql 117 type User @goModel(model: "github.com/my/app/models.User") { 118 id: ID! @goField(name: "todoId") 119 name: String! 120 @goField(forceResolver: true) 121 @goTag(key: "xorm", value: "-") 122 @goTag(key: "yaml") 123 } 124 ``` 125 126 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. 127 128 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`. 129 130 e.g. a custom directive called `constraint` would be set as `skip_runtime` using the following configuration 131 ```yml 132 # custom directives which are not exposed during introspection. These directives are 133 # used for code generation only 134 directives: 135 constraint: 136 skip_runtime: true 137 ```