Skip to content

Commit 2af04e0

Browse files
author
Nick Parker
committed
Use original field names for struct 'json:X' annotations
If a graphql command or fields contain underscores (e.g. snake_case), the generator currently converts both the Go struct field names AND the json annotations to PascalCase. In the process, this removes the underscores, which are significant to the query. For example, the following GraphQL query definition...: ``` query GetExporter($tenant: String!, $name: String!) { exporter_by_pk(tenant: $tenant, name: $name) { tenant name type credential config created_at updated_at } } ``` ...is rendered to Go like this: ``` type GetExporterResponse struct { ExporterByPk struct { Tenant string `json:"Tenant"` Name string `json:"Name"` Type string `json:"Type"` Credential string `json:"Credential"` Config string `json:"Config"` CreatedAt string `json:"CreatedAt"` // was created_at UpdatedAt string `json:"UpdatedAt"` // was updated_at } `json:"ExporterByPk"` // was exporter_by_pk } ``` With this PR, the issue is resolved by using the original names in the `json:X` annotations, while keeping the PascalCase for the Go field names themselves: ``` type GetExporterResponse struct { ExporterByPk struct { Tenant string `json:"tenant"` Name string `json:"name"` Type string `json:"type"` Credential string `json:"credential"` Config string `json:"config"` CreatedAt string `json:"created_at"` // fixed UpdatedAt string `json:"updated_at"` // fixed } `json:"exporter_by_pk"` // fixed } ```
1 parent c489066 commit 2af04e0

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/generator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,13 @@ export class GolangGenerator {
331331
}
332332
w.push('struct {')
333333
} else {
334-
w.push(`string \`json:"${name}"\``)
334+
w.push(`string \`json:"${field.name.value}"\``)
335335
}
336336
l.push(w.join(''))
337337
},
338338
leave: field => {
339339
if (field.selectionSet) {
340-
const name = this.formatName(field.name.value)
341-
l.push(`} \`json:"${name}"\``)
340+
l.push(`} \`json:"${field.name.value}"\``)
342341
}
343342
},
344343
},

0 commit comments

Comments
 (0)