Skip to content

Commit

Permalink
entgql: add annotation Deprecated for deprecated field (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
luantranminh authored Jul 18, 2024
1 parent bb8f307 commit fcf98f0
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 10 deletions.
19 changes: 18 additions & 1 deletion entgql/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"

"entgo.io/ent/entc"
"entgo.io/ent/entc/gen"
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion entgql/internal/todo/ent.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions entgql/internal/todo/ent/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion entgql/internal/todo/ent/category/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion entgql/internal/todo/ent/schema/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ func (Category) Fields() []ent.Field {
entgql.Type("Uint64"),
),
field.Strings("strings").
Optional(),
Optional().
Deprecated("use `string` instead"),
}
}

Expand Down
54 changes: 54 additions & 0 deletions entgql/internal/todo/gen_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
2 changes: 2 additions & 0 deletions entgql/internal/todogotype/ent/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion entgql/internal/todogotype/ent/category/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion entgql/internal/todogotype/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions entgql/internal/todopulid/ent/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion entgql/internal/todopulid/ent/category/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion entgql/internal/todopulid/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions entgql/internal/todouuid/ent/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion entgql/internal/todouuid/ent/category/category.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion entgql/internal/todouuid/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fcf98f0

Please sign in to comment.