Skip to content

Commit fcf98f0

Browse files
authored
entgql: add annotation Deprecated for deprecated field (#595)
1 parent bb8f307 commit fcf98f0

File tree

15 files changed

+106
-10
lines changed

15 files changed

+106
-10
lines changed

Diff for: entgql/extension.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"os"
2020
"path/filepath"
21+
"slices"
2122

2223
"entgo.io/ent/entc"
2324
"entgo.io/ent/entc/gen"
@@ -244,7 +245,23 @@ func (e *Extension) genSchemaHook() gen.Hook {
244245
if err = next.Generate(g); err != nil {
245246
return err
246247
}
247-
248+
for _, t := range g.Nodes {
249+
for _, f := range t.DeprecatedFields() {
250+
ant, err := annotation(f.Annotations)
251+
if err != nil {
252+
return err
253+
}
254+
if !slices.ContainsFunc(ant.Directives, func(d Directive) bool {
255+
return d.Name == "deprecated"
256+
}) {
257+
ant.Directives = append(ant.Directives, Deprecated(f.DeprecationReason()))
258+
if f.Annotations == nil {
259+
f.Annotations = make(map[string]interface{})
260+
}
261+
f.Annotations[ant.Name()] = ant
262+
}
263+
}
264+
}
248265
if !(e.genSchema || e.genWhereInput || e.genMutations) {
249266
return nil
250267
}

Diff for: entgql/internal/todo/ent.graphql

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type Category implements Node {
7777
types: CategoryTypes
7878
duration: Duration
7979
count: Uint64
80-
strings: [String!]
80+
strings: [String!] @deprecated(reason: "use `string` instead")
8181
todos(
8282
"""
8383
Returns the elements in the list that come after the specified cursor.

Diff for: entgql/internal/todo/ent/category.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todo/ent/category/category.go

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todo/ent/schema/category.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ func (Category) Fields() []ent.Field {
7676
entgql.Type("Uint64"),
7777
),
7878
field.Strings("strings").
79-
Optional(),
79+
Optional().
80+
Deprecated("use `string` instead"),
8081
}
8182
}
8283

Diff for: entgql/internal/todo/gen_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2019-present Facebook
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package todo_test
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"testing"
21+
22+
"entgo.io/contrib/entgql"
23+
"entgo.io/ent/entc"
24+
"entgo.io/ent/entc/gen"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestGeneratedSchema(t *testing.T) {
29+
tempDir := t.TempDir()
30+
gqlcfg, err := os.ReadFile("./gqlgen.yml")
31+
require.NoError(t, err)
32+
err = os.WriteFile(filepath.Join(tempDir, "gqlgen.yml"), gqlcfg, 0644)
33+
require.NoError(t, err)
34+
ex, err := entgql.NewExtension(
35+
entgql.WithConfigPath(filepath.Join(tempDir, "gqlgen.yml")),
36+
entgql.WithSchemaGenerator(),
37+
entgql.WithSchemaPath(filepath.Join(tempDir, "ent.graphql")),
38+
entgql.WithWhereInputs(true),
39+
entgql.WithNodeDescriptor(true),
40+
)
41+
require.NoError(t, err)
42+
err = entc.Generate("./ent/schema", &gen.Config{
43+
Target: tempDir,
44+
Features: []gen.Feature{
45+
gen.FeatureModifier,
46+
},
47+
}, entc.Extensions(ex))
48+
require.NoError(t, err)
49+
expected, err := os.ReadFile("./ent.graphql")
50+
require.NoError(t, err)
51+
actual, err := os.ReadFile(filepath.Join(tempDir, "ent.graphql"))
52+
require.NoError(t, err)
53+
require.Equal(t, string(expected), string(actual))
54+
}

Diff for: entgql/internal/todogotype/ent/category.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todogotype/ent/category/category.go

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todogotype/generated.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todopulid/ent/category.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todopulid/ent/category/category.go

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todopulid/generated.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todouuid/ent/category.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todouuid/ent/category/category.go

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: entgql/internal/todouuid/generated.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)