github.com/geneva/gqlgen@v0.17.7-0.20230801155730-7b9317164836/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 off to make struct-type struct fields not use pointers 66 # e.g. type Thing struct { FieldA OtherThing } instead of { FieldA *OtherThing } 67 # struct_fields_always_pointers: true 68 69 # Optional: turn off to make resolvers return values instead of pointers for structs 70 # resolvers_always_return_pointers: true 71 72 # Optional: turn on to return pointers instead of values in unmarshalInput 73 # return_pointers_in_unmarshalinput: false 74 75 # Optional: wrap nullable input fields with Omittable 76 # nullable_input_omittable: true 77 78 # Optional: turn on to return pointers instead of values in unmarshalInput 79 # return_pointers_in_unmarshalinput: false 80 81 # Optional: set to speed up generation time by not performing a final validation pass. 82 # skip_validation: true 83 84 # Optional: set to skip running `go mod tidy` when generating server code 85 # skip_mod_tidy: true 86 87 # Optional: set to modify the initialisms regarded for Go names 88 # go_initialisms: 89 # replace_defaults: false # if true, the default initialisms will get dropped in favor of the new ones instead of being added 90 # initialisms: # List of initialisms to for Go names 91 # - 'CC' 92 # - 'BCC' 93 94 # gqlgen will search for any type names in the schema in these go packages 95 # if they match it will use them, otherwise it will generate them. 96 # autobind: 97 # - "github.com/[YOUR_APP_DIR]/graph/model" 98 99 # This section declares type mapping between the GraphQL and go type systems 100 # 101 # The first line in each type will be used as defaults for resolver arguments and 102 # modelgen, the others will be allowed when binding to fields. Configure them to 103 # your liking 104 models: 105 ID: 106 model: 107 - github.com/99designs/gqlgen/graphql.ID 108 - github.com/99designs/gqlgen/graphql.Int 109 - github.com/99designs/gqlgen/graphql.Int64 110 - github.com/99designs/gqlgen/graphql.Int32 111 Int: 112 model: 113 - github.com/99designs/gqlgen/graphql.Int 114 - github.com/99designs/gqlgen/graphql.Int64 115 - github.com/99designs/gqlgen/graphql.Int32 116 ``` 117 118 Everything has defaults, so add things as you need. 119 120 ## Inline config with directives 121 122 gqlgen ships with some builtin directives that make it a little easier to manage wiring. 123 124 To start using them you first need to define them: 125 126 ```graphql 127 directive @goModel( 128 model: String 129 models: [String!] 130 ) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION 131 132 directive @goField( 133 forceResolver: Boolean 134 name: String 135 omittable: Boolean 136 ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION 137 138 directive @goTag( 139 key: String! 140 value: String 141 ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION 142 ``` 143 144 > Here be dragons 145 > 146 > gqlgen doesnt currently support user-configurable directives for SCALAR, ENUM, INTERFACE or UNION. This only works 147 > for internal directives. You can track the progress [here](https://github.com/99designs/gqlgen/issues/760) 148 149 Now you can use these directives when defining types in your schema: 150 151 ```graphql 152 type User @goModel(model: "github.com/my/app/models.User") { 153 id: ID! @goField(name: "todoId") 154 name: String! 155 @goField(forceResolver: true) 156 @goTag(key: "xorm", value: "-") 157 @goTag(key: "yaml") 158 } 159 ``` 160 161 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. 162 163 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`. 164 165 e.g. a custom directive called `constraint` would be set as `skip_runtime` using the following configuration 166 167 ```yml 168 # custom directives which are not exposed during introspection. These directives are 169 # used for code generation only 170 directives: 171 constraint: 172 skip_runtime: true 173 ```