diff --git a/entgql/extension.go b/entgql/extension.go index 5d11b5331..4f3de3434 100644 --- a/entgql/extension.go +++ b/entgql/extension.go @@ -18,6 +18,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "entgo.io/ent/entc" "entgo.io/ent/entc/gen" @@ -244,7 +245,23 @@ func (e *Extension) genSchemaHook() gen.Hook { if err = next.Generate(g); err != nil { return err } - + for _, t := range g.Nodes { + for _, f := range t.DeprecatedFields() { + ant, err := annotation(f.Annotations) + if err != nil { + return err + } + if !slices.ContainsFunc(ant.Directives, func(d Directive) bool { + return d.Name == "deprecated" + }) { + ant.Directives = append(ant.Directives, Deprecated(f.DeprecationReason())) + if f.Annotations == nil { + f.Annotations = make(map[string]interface{}) + } + f.Annotations[ant.Name()] = ant + } + } + } if !(e.genSchema || e.genWhereInput || e.genMutations) { return nil } diff --git a/entgql/internal/todo/ent.graphql b/entgql/internal/todo/ent.graphql index a3f3b1076..c1d5b1595 100644 --- a/entgql/internal/todo/ent.graphql +++ b/entgql/internal/todo/ent.graphql @@ -77,7 +77,7 @@ type Category implements Node { types: CategoryTypes duration: Duration count: Uint64 - strings: [String!] + strings: [String!] @deprecated(reason: "use `string` instead") todos( """ Returns the elements in the list that come after the specified cursor. diff --git a/entgql/internal/todo/ent/category.go b/entgql/internal/todo/ent/category.go index 29d15bf28..1cdf05f3f 100644 --- a/entgql/internal/todo/ent/category.go +++ b/entgql/internal/todo/ent/category.go @@ -46,6 +46,8 @@ type Category struct { // Count holds the value of the "count" field. Count uint64 `json:"count,omitempty"` // Strings holds the value of the "strings" field. + // + // Deprecated: use `string` instead Strings []string `json:"strings,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the CategoryQuery when eager-loading is set. diff --git a/entgql/internal/todo/ent/category/category.go b/entgql/internal/todo/ent/category/category.go index 8033932d5..3bc62e10d 100644 --- a/entgql/internal/todo/ent/category/category.go +++ b/entgql/internal/todo/ent/category/category.go @@ -70,7 +70,6 @@ var Columns = []string{ FieldTypes, FieldDuration, FieldCount, - FieldStrings, } var ( @@ -86,6 +85,11 @@ func ValidColumn(column string) bool { return true } } + for _, f := range [...]string{FieldStrings} { + if column == f { + return true + } + } return false } diff --git a/entgql/internal/todo/ent/schema/category.go b/entgql/internal/todo/ent/schema/category.go index fdfd5709a..b8251d21c 100644 --- a/entgql/internal/todo/ent/schema/category.go +++ b/entgql/internal/todo/ent/schema/category.go @@ -76,7 +76,8 @@ func (Category) Fields() []ent.Field { entgql.Type("Uint64"), ), field.Strings("strings"). - Optional(), + Optional(). + Deprecated("use `string` instead"), } } diff --git a/entgql/internal/todo/gen_test.go b/entgql/internal/todo/gen_test.go new file mode 100644 index 000000000..d447c25b4 --- /dev/null +++ b/entgql/internal/todo/gen_test.go @@ -0,0 +1,54 @@ +// Copyright 2019-present Facebook +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package todo_test + +import ( + "os" + "path/filepath" + "testing" + + "entgo.io/contrib/entgql" + "entgo.io/ent/entc" + "entgo.io/ent/entc/gen" + "github.com/stretchr/testify/require" +) + +func TestGeneratedSchema(t *testing.T) { + tempDir := t.TempDir() + gqlcfg, err := os.ReadFile("./gqlgen.yml") + require.NoError(t, err) + err = os.WriteFile(filepath.Join(tempDir, "gqlgen.yml"), gqlcfg, 0644) + require.NoError(t, err) + ex, err := entgql.NewExtension( + entgql.WithConfigPath(filepath.Join(tempDir, "gqlgen.yml")), + entgql.WithSchemaGenerator(), + entgql.WithSchemaPath(filepath.Join(tempDir, "ent.graphql")), + entgql.WithWhereInputs(true), + entgql.WithNodeDescriptor(true), + ) + require.NoError(t, err) + err = entc.Generate("./ent/schema", &gen.Config{ + Target: tempDir, + Features: []gen.Feature{ + gen.FeatureModifier, + }, + }, entc.Extensions(ex)) + require.NoError(t, err) + expected, err := os.ReadFile("./ent.graphql") + require.NoError(t, err) + actual, err := os.ReadFile(filepath.Join(tempDir, "ent.graphql")) + require.NoError(t, err) + require.Equal(t, string(expected), string(actual)) +} diff --git a/entgql/internal/todogotype/ent/category.go b/entgql/internal/todogotype/ent/category.go index 3000f603d..c86e72f5e 100644 --- a/entgql/internal/todogotype/ent/category.go +++ b/entgql/internal/todogotype/ent/category.go @@ -47,6 +47,8 @@ type Category struct { // Count holds the value of the "count" field. Count uint64 `json:"count,omitempty"` // Strings holds the value of the "strings" field. + // + // Deprecated: use `string` instead Strings []string `json:"strings,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the CategoryQuery when eager-loading is set. diff --git a/entgql/internal/todogotype/ent/category/category.go b/entgql/internal/todogotype/ent/category/category.go index 869878539..f594b4990 100644 --- a/entgql/internal/todogotype/ent/category/category.go +++ b/entgql/internal/todogotype/ent/category/category.go @@ -71,7 +71,6 @@ var Columns = []string{ FieldTypes, FieldDuration, FieldCount, - FieldStrings, } var ( @@ -87,6 +86,11 @@ func ValidColumn(column string) bool { return true } } + for _, f := range [...]string{FieldStrings} { + if column == f { + return true + } + } return false } diff --git a/entgql/internal/todogotype/generated.go b/entgql/internal/todogotype/generated.go index c5de4f902..1296acb99 100644 --- a/entgql/internal/todogotype/generated.go +++ b/entgql/internal/todogotype/generated.go @@ -1478,7 +1478,7 @@ type Category implements Node { types: CategoryTypes duration: Duration count: Uint64 - strings: [String!] + strings: [String!] @deprecated(reason: "use ` + "`" + `string` + "`" + ` instead") todos( """ Returns the elements in the list that come after the specified cursor. diff --git a/entgql/internal/todopulid/ent/category.go b/entgql/internal/todopulid/ent/category.go index 978d17cf0..2e8adabfa 100644 --- a/entgql/internal/todopulid/ent/category.go +++ b/entgql/internal/todopulid/ent/category.go @@ -47,6 +47,8 @@ type Category struct { // Count holds the value of the "count" field. Count uint64 `json:"count,omitempty"` // Strings holds the value of the "strings" field. + // + // Deprecated: use `string` instead Strings []string `json:"strings,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the CategoryQuery when eager-loading is set. diff --git a/entgql/internal/todopulid/ent/category/category.go b/entgql/internal/todopulid/ent/category/category.go index 2b58d1801..61bfa2e0b 100644 --- a/entgql/internal/todopulid/ent/category/category.go +++ b/entgql/internal/todopulid/ent/category/category.go @@ -71,7 +71,6 @@ var Columns = []string{ FieldTypes, FieldDuration, FieldCount, - FieldStrings, } var ( @@ -87,6 +86,11 @@ func ValidColumn(column string) bool { return true } } + for _, f := range [...]string{FieldStrings} { + if column == f { + return true + } + } return false } diff --git a/entgql/internal/todopulid/generated.go b/entgql/internal/todopulid/generated.go index 694dc9172..dc1e44ad7 100644 --- a/entgql/internal/todopulid/generated.go +++ b/entgql/internal/todopulid/generated.go @@ -1484,7 +1484,7 @@ type Category implements Node { types: CategoryTypes duration: Duration count: Uint64 - strings: [String!] + strings: [String!] @deprecated(reason: "use ` + "`" + `string` + "`" + ` instead") todos( """ Returns the elements in the list that come after the specified cursor. diff --git a/entgql/internal/todouuid/ent/category.go b/entgql/internal/todouuid/ent/category.go index d00f6f9d0..3825ce53a 100644 --- a/entgql/internal/todouuid/ent/category.go +++ b/entgql/internal/todouuid/ent/category.go @@ -47,6 +47,8 @@ type Category struct { // Count holds the value of the "count" field. Count uint64 `json:"count,omitempty"` // Strings holds the value of the "strings" field. + // + // Deprecated: use `string` instead Strings []string `json:"strings,omitempty"` // Edges holds the relations/edges for other nodes in the graph. // The values are being populated by the CategoryQuery when eager-loading is set. diff --git a/entgql/internal/todouuid/ent/category/category.go b/entgql/internal/todouuid/ent/category/category.go index 87e83cd8d..d9bd85976 100644 --- a/entgql/internal/todouuid/ent/category/category.go +++ b/entgql/internal/todouuid/ent/category/category.go @@ -71,7 +71,6 @@ var Columns = []string{ FieldTypes, FieldDuration, FieldCount, - FieldStrings, } var ( @@ -87,6 +86,11 @@ func ValidColumn(column string) bool { return true } } + for _, f := range [...]string{FieldStrings} { + if column == f { + return true + } + } return false } diff --git a/entgql/internal/todouuid/generated.go b/entgql/internal/todouuid/generated.go index 3e6af2252..92795d3d6 100644 --- a/entgql/internal/todouuid/generated.go +++ b/entgql/internal/todouuid/generated.go @@ -1485,7 +1485,7 @@ type Category implements Node { types: CategoryTypes duration: Duration count: Uint64 - strings: [String!] + strings: [String!] @deprecated(reason: "use ` + "`" + `string` + "`" + ` instead") todos( """ Returns the elements in the list that come after the specified cursor.