From 4466fc10e4f306ebe5f7e0e8301cd576744ac7ee Mon Sep 17 00:00:00 2001 From: Eugene <39888178+Zhenya750@users.noreply.github.com> Date: Mon, 19 Aug 2024 09:01:57 +0300 Subject: [PATCH] Generate slices containing all enum values (#350) It would be useful to generate slices containing all enum values for each enum type. Then there will be no need to maintain such kind of slices manually. Co-authored-by: dikov.e --- docs/CHANGELOG.md | 1 + .../TestGenerate-InputEnum.graphql-InputEnum.graphql.go | 5 +++++ ...TestGenerate-InputObject.graphql-InputObject.graphql.go | 5 +++++ ...ultipleDirectives.graphql-MultipleDirectives.graphql.go | 5 +++++ .../TestGenerate-Omitempty.graphql-Omitempty.graphql.go | 5 +++++ .../TestGenerate-Pointers.graphql-Pointers.graphql.go | 5 +++++ ...nerate-PointersInline.graphql-PointersInline.graphql.go | 5 +++++ ...nerate-QueryWithEnums.graphql-QueryWithEnums.graphql.go | 5 +++++ ...stGenerate-StructOption.graphql-StructOption.graphql.go | 5 +++++ ...Generate-UsesEnumTwice.graphql-UsesEnumTwice.graphql.go | 5 +++++ .../TestGenerate-unexported.graphql-unexported.graphql.go | 5 +++++ ...thConfig-EnumRawCasingAll-testdata-queries-generated.go | 5 +++++ ...fig-EnumRawCasingSpecific-testdata-queries-generated.go | 5 +++++ ...thConfig-StructReferences-testdata-queries-generated.go | 5 +++++ ...erencesAndOptionalPointer-testdata-queries-generated.go | 5 +++++ generate/types.go | 7 +++++++ internal/integration/generated.go | 5 +++++ 17 files changed, 83 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index aee24ee3..dd69c6f8 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -30,6 +30,7 @@ When releasing a new version: - genqlient now supports subscriptions; the websocket protocol is by default `graphql-transport-ws` but can be set to another value. See the [documentation](FAQ.md) for how to `subscribe to an API 'subscription' endpoint`. - genqlient now supports double-star globs for schema and query files; see [`genqlient.yaml` docs](genqlient.yaml) for more. +- genqlient now generates slices containing all enum values for each enum type. ### Bug fixes: diff --git a/generate/testdata/snapshots/TestGenerate-InputEnum.graphql-InputEnum.graphql.go b/generate/testdata/snapshots/TestGenerate-InputEnum.graphql-InputEnum.graphql.go index 8abae869..66f6f0fb 100644 --- a/generate/testdata/snapshots/TestGenerate-InputEnum.graphql-InputEnum.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-InputEnum.graphql-InputEnum.graphql.go @@ -46,6 +46,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // __InputEnumQueryInput is used internally by genqlient type __InputEnumQueryInput struct { Role Role `json:"role"` diff --git a/generate/testdata/snapshots/TestGenerate-InputObject.graphql-InputObject.graphql.go b/generate/testdata/snapshots/TestGenerate-InputObject.graphql-InputObject.graphql.go index bcf810c8..a38570fb 100644 --- a/generate/testdata/snapshots/TestGenerate-InputObject.graphql-InputObject.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-InputObject.graphql-InputObject.graphql.go @@ -51,6 +51,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // UserQueryInput is the argument to Query.users. // // Ideally this would support anything and everything! diff --git a/generate/testdata/snapshots/TestGenerate-MultipleDirectives.graphql-MultipleDirectives.graphql.go b/generate/testdata/snapshots/TestGenerate-MultipleDirectives.graphql-MultipleDirectives.graphql.go index 59461917..432700cd 100644 --- a/generate/testdata/snapshots/TestGenerate-MultipleDirectives.graphql-MultipleDirectives.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-MultipleDirectives.graphql-MultipleDirectives.graphql.go @@ -192,6 +192,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // UserQueryInput is the argument to Query.users. // // Ideally this would support anything and everything! diff --git a/generate/testdata/snapshots/TestGenerate-Omitempty.graphql-Omitempty.graphql.go b/generate/testdata/snapshots/TestGenerate-Omitempty.graphql-Omitempty.graphql.go index 5ad9ffef..cfa7e9ce 100644 --- a/generate/testdata/snapshots/TestGenerate-Omitempty.graphql-Omitempty.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-Omitempty.graphql-Omitempty.graphql.go @@ -77,6 +77,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // UserQueryInput is the argument to Query.users. // // Ideally this would support anything and everything! diff --git a/generate/testdata/snapshots/TestGenerate-Pointers.graphql-Pointers.graphql.go b/generate/testdata/snapshots/TestGenerate-Pointers.graphql-Pointers.graphql.go index 37dce390..90df12dd 100644 --- a/generate/testdata/snapshots/TestGenerate-Pointers.graphql-Pointers.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-Pointers.graphql-Pointers.graphql.go @@ -93,6 +93,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // UserQueryInput is the argument to Query.users. // // Ideally this would support anything and everything! diff --git a/generate/testdata/snapshots/TestGenerate-PointersInline.graphql-PointersInline.graphql.go b/generate/testdata/snapshots/TestGenerate-PointersInline.graphql-PointersInline.graphql.go index aa1877e0..6c46b270 100644 --- a/generate/testdata/snapshots/TestGenerate-PointersInline.graphql-PointersInline.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-PointersInline.graphql-PointersInline.graphql.go @@ -93,6 +93,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // UserQueryInput is the argument to Query.users. // // Ideally this would support anything and everything! diff --git a/generate/testdata/snapshots/TestGenerate-QueryWithEnums.graphql-QueryWithEnums.graphql.go b/generate/testdata/snapshots/TestGenerate-QueryWithEnums.graphql-QueryWithEnums.graphql.go index e92ac516..a3fd598e 100644 --- a/generate/testdata/snapshots/TestGenerate-QueryWithEnums.graphql-QueryWithEnums.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-QueryWithEnums.graphql-QueryWithEnums.graphql.go @@ -62,6 +62,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // The query executed by QueryWithEnums. const QueryWithEnums_Operation = ` query QueryWithEnums { diff --git a/generate/testdata/snapshots/TestGenerate-StructOption.graphql-StructOption.graphql.go b/generate/testdata/snapshots/TestGenerate-StructOption.graphql-StructOption.graphql.go index 8c6edec3..7c01e243 100644 --- a/generate/testdata/snapshots/TestGenerate-StructOption.graphql-StructOption.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-StructOption.graphql-StructOption.graphql.go @@ -24,6 +24,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // StructOptionResponse is returned by StructOption on success. type StructOptionResponse struct { Root StructOptionRootTopic `json:"root"` diff --git a/generate/testdata/snapshots/TestGenerate-UsesEnumTwice.graphql-UsesEnumTwice.graphql.go b/generate/testdata/snapshots/TestGenerate-UsesEnumTwice.graphql-UsesEnumTwice.graphql.go index 4769c044..2b826f1d 100644 --- a/generate/testdata/snapshots/TestGenerate-UsesEnumTwice.graphql-UsesEnumTwice.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-UsesEnumTwice.graphql-UsesEnumTwice.graphql.go @@ -20,6 +20,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // UsesEnumTwiceQueryMeUser includes the requested fields of the GraphQL type User. // The GraphQL type's documentation follows. // diff --git a/generate/testdata/snapshots/TestGenerate-unexported.graphql-unexported.graphql.go b/generate/testdata/snapshots/TestGenerate-unexported.graphql-unexported.graphql.go index 8bbaabea..c4583342 100644 --- a/generate/testdata/snapshots/TestGenerate-unexported.graphql-unexported.graphql.go +++ b/generate/testdata/snapshots/TestGenerate-unexported.graphql-unexported.graphql.go @@ -25,6 +25,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // UserQueryInput is the argument to Query.users. // // Ideally this would support anything and everything! diff --git a/generate/testdata/snapshots/TestGenerateWithConfig-EnumRawCasingAll-testdata-queries-generated.go b/generate/testdata/snapshots/TestGenerateWithConfig-EnumRawCasingAll-testdata-queries-generated.go index 2117bc99..e9a59f3f 100644 --- a/generate/testdata/snapshots/TestGenerateWithConfig-EnumRawCasingAll-testdata-queries-generated.go +++ b/generate/testdata/snapshots/TestGenerateWithConfig-EnumRawCasingAll-testdata-queries-generated.go @@ -64,6 +64,11 @@ const ( Role_TEACHER Role = "TEACHER" ) +var AllRole = []Role{ + Role_STUDENT, + Role_TEACHER, +} + // The query executed by QueryWithEnums. const QueryWithEnums_Operation = ` query QueryWithEnums { diff --git a/generate/testdata/snapshots/TestGenerateWithConfig-EnumRawCasingSpecific-testdata-queries-generated.go b/generate/testdata/snapshots/TestGenerateWithConfig-EnumRawCasingSpecific-testdata-queries-generated.go index 2117bc99..e9a59f3f 100644 --- a/generate/testdata/snapshots/TestGenerateWithConfig-EnumRawCasingSpecific-testdata-queries-generated.go +++ b/generate/testdata/snapshots/TestGenerateWithConfig-EnumRawCasingSpecific-testdata-queries-generated.go @@ -64,6 +64,11 @@ const ( Role_TEACHER Role = "TEACHER" ) +var AllRole = []Role{ + Role_STUDENT, + Role_TEACHER, +} + // The query executed by QueryWithEnums. const QueryWithEnums_Operation = ` query QueryWithEnums { diff --git a/generate/testdata/snapshots/TestGenerateWithConfig-StructReferences-testdata-queries-generated.go b/generate/testdata/snapshots/TestGenerateWithConfig-StructReferences-testdata-queries-generated.go index 507f60ff..771a13e1 100644 --- a/generate/testdata/snapshots/TestGenerateWithConfig-StructReferences-testdata-queries-generated.go +++ b/generate/testdata/snapshots/TestGenerateWithConfig-StructReferences-testdata-queries-generated.go @@ -100,6 +100,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // UserQueryInput is the argument to Query.users. // // Ideally this would support anything and everything! diff --git a/generate/testdata/snapshots/TestGenerateWithConfig-StructReferencesAndOptionalPointer-testdata-queries-generated.go b/generate/testdata/snapshots/TestGenerateWithConfig-StructReferencesAndOptionalPointer-testdata-queries-generated.go index 7e1b2b13..7f866d32 100644 --- a/generate/testdata/snapshots/TestGenerateWithConfig-StructReferencesAndOptionalPointer-testdata-queries-generated.go +++ b/generate/testdata/snapshots/TestGenerateWithConfig-StructReferencesAndOptionalPointer-testdata-queries-generated.go @@ -100,6 +100,11 @@ const ( RoleTeacher Role = "TEACHER" ) +var AllRole = []Role{ + RoleStudent, + RoleTeacher, +} + // UserQueryInput is the argument to Query.users. // // Ideally this would support anything and everything! diff --git a/generate/types.go b/generate/types.go index 01f619f5..980b9d4f 100644 --- a/generate/types.go +++ b/generate/types.go @@ -147,6 +147,13 @@ func (typ *goEnumType) WriteDefinition(w io.Writer, g *generator) error { val.GoName, typ.GoName, val.GraphQLName) } fmt.Fprintf(w, ")\n") + + // Add slice with all enums. + fmt.Fprintf(w, "var All%s = []%s{\n", typ.GoName, typ.GoName) + for _, val := range typ.Values { + fmt.Fprintf(w, "%s,\n", val.GoName) + } + fmt.Fprintf(w, "}\n") return nil } diff --git a/internal/integration/generated.go b/internal/integration/generated.go index 9725a7d3..8a8f70cb 100644 --- a/internal/integration/generated.go +++ b/internal/integration/generated.go @@ -928,6 +928,11 @@ const ( SpeciesCoelacanth Species = "COELACANTH" ) +var AllSpecies = []Species{ + SpeciesDog, + SpeciesCoelacanth, +} + // UserFields includes the GraphQL fields of User requested by the fragment UserFields. type UserFields struct { Id string `json:"id"`