Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added proposed directive changes #543

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion entgql/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ type (

// Directive to apply on the field/type.
Directive struct {
Name string `json:"name,omitempty"`
Name string `json:"name,omitempty"`
Arguments []*ast.Argument `json:"arguments,omitempty"`
SkipTypeGen bool `json:"SkipType,omitempty"`
CreateMutationGen bool `json:"OnCreate,omitempty"`
UpdateMutaionGen bool `json:"OnUpdate,omitempty"`
}

// SkipMode is a bit flag for the Skip annotation.
Expand Down Expand Up @@ -549,6 +552,21 @@ func NewDirective(name string, args ...*ast.Argument) Directive {
}
}

func(d Directive) SkipType() Directive {
d.SkipTypeGen = true
return d
}

func(d Directive) OnCreateInput() Directive {
d.CreateMutationGen = true
return d
}

func(d Directive) OnUpdateInput() Directive {
d.UpdateMutaionGen = true
return d
}

// Deprecated create `@deprecated` directive to apply on the field/type
func Deprecated(reason string) Directive {
var args []*ast.Argument
Expand Down
8 changes: 7 additions & 1 deletion entgql/internal/todo/ent/schema/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package schema
import (
"time"

"entgo.io/contrib/entgql"
"entgo.io/ent"
"entgo.io/ent/dialect"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"

"entgo.io/contrib/entgql"
"entgo.io/contrib/entgql/internal/todo/ent/schema/schematype"
)

Expand Down Expand Up @@ -77,6 +77,12 @@ func (Category) Fields() []ent.Field {
),
field.Strings("strings").
Optional(),
field.String("key").
Annotations(
entgql.Directives(
entgql.Deprecated("this field is no longer used"),
),
),
}
}

Expand Down
10 changes: 8 additions & 2 deletions entgql/internal/todo/ent/schema/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package schema
import (
"time"

"entgo.io/contrib/entgql"
"entgo.io/contrib/entgql/internal/todo/ent/schema/customstruct"
"entgo.io/ent"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"

"entgo.io/contrib/entgql"
"entgo.io/contrib/entgql/internal/todo/ent/schema/customstruct"
)

// Todo defines the todo type schema.
Expand All @@ -49,6 +50,11 @@ func (Todo) Fields() []ent.Field {
Annotations(
entgql.OrderField("STATUS"),
),
field.String("old_status").Annotations(
entgql.Directives(
entgql.Deprecated("use new 'status' field").
OnCreateInput().
OnUpdateInput())),
field.Int("priority").
Default(0).
Annotations(
Expand Down
25 changes: 19 additions & 6 deletions entgql/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (e *schemaGenerator) buildTypes(g *gen.Graph, s *ast.Schema) error {

def := names.ConnectionField(name, hasOrderBy, ant.MultiOrder, hasWhereInput)
def.Description = ant.QueryField.Description
def.Directives = e.buildDirectives(ant.QueryField.Directives)
def.Directives = e.buildDirectives(nil, ant.QueryField.Directives)
queryFields = append(queryFields, def)
}
} else if ant.QueryField != nil {
Expand All @@ -223,7 +223,7 @@ func (e *schemaGenerator) buildTypes(g *gen.Graph, s *ast.Schema) error {
Description: ant.QueryField.Description,
Type: listNamedType(gqlType, false),
}
def.Directives = e.buildDirectives(ant.QueryField.Directives)
def.Directives = e.buildDirectives(nil, ant.QueryField.Directives)
queryFields = append(queryFields, def)
}
}
Expand Down Expand Up @@ -301,7 +301,7 @@ func (e *schemaGenerator) buildType(t *gen.Type, ant *Annotation, gqlType, pkg s
def := &ast.Definition{
Name: gqlType,
Kind: ast.Object,
Directives: e.buildDirectives(ant.Directives),
Directives: e.buildDirectives(nil, ant.Directives),
}
if t.Name != gqlType {
def.Directives = append(def.Directives, goModel(entGoType(t.Name, pkg)))
Expand Down Expand Up @@ -356,9 +356,21 @@ func (e *schemaGenerator) buildType(t *gen.Type, ant *Annotation, gqlType, pkg s
return def, nil
}

func (e *schemaGenerator) buildDirectives(directives []Directive) ast.DirectiveList {
func (e *schemaGenerator) buildDirectives(mc *MutationConfig, directives []Directive) ast.DirectiveList {
list := make(ast.DirectiveList, 0, len(directives))
for _, d := range directives {
// This checks if the mutationinput is create or update and seeing if that is skipped on the directive or not.
if mc != nil {
if mc.IsCreate && !d.CreateMutationGen {
continue
} else if !mc.IsCreate && !d.UpdateMutaionGen {
continue
}
}
if d.SkipTypeGen {
continue
}

list = append(list, &ast.Directive{
Name: d.Name,
Arguments: d.Arguments,
Expand Down Expand Up @@ -454,7 +466,7 @@ func (e *schemaGenerator) buildEdge(node *gen.Type, edge *gen.Edge, edgeAnt *Ann
fieldDef.Type = listNamedType(gqlType, edge.Optional)
}

fieldDef.Directives = e.buildDirectives(edgeAnt.Directives)
fieldDef.Directives = e.buildDirectives(nil, edgeAnt.Directives)
if goFieldName != templates.ToGo(name) {
fieldDef.Directives = append(fieldDef.Directives, goField(structField))
}
Expand Down Expand Up @@ -579,6 +591,7 @@ func (e *schemaGenerator) buildMutationInputs(t *gen.Type, ant *Annotation, gqlT
Name: camel(f.Name),
Type: namedType(scalar, f.Nullable),
Description: f.Comment(),
Directives: e.buildDirectives(&i, ant.Directives),
})
if f.AppendOp {
def.Fields = append(def.Fields, &ast.FieldDefinition{
Expand Down Expand Up @@ -647,7 +660,7 @@ func (e *schemaGenerator) fieldDefinitions(gqlType string, f *gen.Field, ant *An
Name: name,
Type: ft,
Description: f.Comment(),
Directives: e.buildDirectives(ant.Directives),
Directives: e.buildDirectives(nil, ant.Directives),
}
// We check the field name with gqlgen's naming convention.
// To avoid unnecessary @goField directives
Expand Down
6 changes: 6 additions & 0 deletions entgql/testdata/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Category {
duration: Duration
count: Uint64
strings: [String!]
key: String! @deprecated(reason: "this field is no longer used")
todos: [Todo!]
subCategories: [Category!]
}
Expand Down Expand Up @@ -49,6 +50,7 @@ input CreateCategoryInput {
duration: Duration
count: Uint64
strings: [String!]
key: String!
todoIDs: [ID!]
subCategoryIDs: [ID!]
}
Expand All @@ -58,6 +60,7 @@ Input was generated by ent.
"""
input CreateTodoInput {
status: TodoStatus!
oldStatus: String! @deprecated(reason: "use new 'status' field")
priority: Int
text: String!
init: Map
Expand Down Expand Up @@ -135,6 +138,7 @@ type Todo {
id: ID!
createdAt: Time!
status: TodoStatus!
oldStatus: String! @deprecated(reason: "use new 'status' field")
priorityOrder: Int! @goField(name: "Priority", forceResolver: false)
text: String!
categoryID: ID
Expand Down Expand Up @@ -190,6 +194,7 @@ input UpdateCategoryInput {
strings: [String!]
appendStrings: [String!]
clearStrings: Boolean
key: String
addTodoIDs: [ID!]
removeTodoIDs: [ID!]
clearTodos: Boolean
Expand All @@ -212,6 +217,7 @@ Input was generated by ent.
"""
input UpdateTodoInput {
status: TodoStatus
oldStatus: String @deprecated(reason: "use new 'status' field")
priority: Int
text: String
init: Map
Expand Down
34 changes: 34 additions & 0 deletions entgql/testdata/schema_relay.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Category implements Node {
duration: Duration
count: Uint64
strings: [String!]
key: String! @deprecated(reason: "this field is no longer used")
todos(
"""Returns the elements in the list that come after the specified cursor."""
after: Cursor
Expand Down Expand Up @@ -213,6 +214,20 @@ input CategoryWhereInput {
countLTE: Uint64
countIsNil: Boolean
countNotNil: Boolean
"""key field predicates"""
key: String
keyNEQ: String
keyIn: [String!]
keyNotIn: [String!]
keyGT: String
keyGTE: String
keyLT: String
keyLTE: String
keyContains: String
keyHasPrefix: String
keyHasSuffix: String
keyEqualFold: String
keyContainsFold: String
"""todos edge predicates"""
hasTodos: Boolean
hasTodosWith: [TodoWhereInput!]
Expand All @@ -232,6 +247,7 @@ input CreateCategoryInput {
duration: Duration
count: Uint64
strings: [String!]
key: String!
todoIDs: [ID!]
subCategoryIDs: [ID!]
}
Expand All @@ -241,6 +257,7 @@ Input was generated by ent.
"""
input CreateTodoInput {
status: TodoStatus!
oldStatus: String! @deprecated(reason: "use new 'status' field")
priority: Int
text: String!
init: Map
Expand Down Expand Up @@ -668,6 +685,7 @@ type Todo implements Node {
id: ID!
createdAt: Time!
status: TodoStatus!
oldStatus: String! @deprecated(reason: "use new 'status' field")
priorityOrder: Int! @goField(name: "Priority", forceResolver: false)
text: String!
categoryID: ID
Expand Down Expand Up @@ -768,6 +786,20 @@ input TodoWhereInput {
statusNEQ: TodoStatus
statusIn: [TodoStatus!]
statusNotIn: [TodoStatus!]
"""old_status field predicates"""
oldStatus: String
oldStatusNEQ: String
oldStatusIn: [String!]
oldStatusNotIn: [String!]
oldStatusGT: String
oldStatusGTE: String
oldStatusLT: String
oldStatusLTE: String
oldStatusContains: String
oldStatusHasPrefix: String
oldStatusHasSuffix: String
oldStatusEqualFold: String
oldStatusContainsFold: String
"""priority field predicates"""
priority: Int
priorityNEQ: Int
Expand Down Expand Up @@ -828,6 +860,7 @@ input UpdateCategoryInput {
strings: [String!]
appendStrings: [String!]
clearStrings: Boolean
key: String
addTodoIDs: [ID!]
removeTodoIDs: [ID!]
clearTodos: Boolean
Expand All @@ -850,6 +883,7 @@ Input was generated by ent.
"""
input UpdateTodoInput {
status: TodoStatus
oldStatus: String @deprecated(reason: "use new 'status' field")
priority: Int
text: String
init: Map
Expand Down