Created
May 21, 2022 21:32
-
-
Save giautm/dc048109cdbf7a1411a7e209d7ebded4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package softdelete | |
import ( | |
"context" | |
"entgo.io/contrib/entgql" | |
"entgo.io/ent" | |
"entgo.io/ent/entql" | |
"entgo.io/ent/schema/field" | |
"entgo.io/ent/schema/mixin" | |
) | |
// Mixin for all schemas in the graph. | |
type Mixin struct { | |
mixin.Schema | |
} | |
// Fields of the SoftDeleteMixin mixin. | |
func (Mixin) Fields() []ent.Field { | |
return []ent.Field{ | |
field.Time("delete_time"). | |
Optional(). | |
Annotations( | |
entgql.Skip(), | |
), | |
} | |
} | |
// Policy defines the privacy policy of the BaseMixin. | |
func (Mixin) Policy() ent.Policy { | |
return privacy.Policy{ | |
Query: privacy.QueryPolicy{ | |
NotDeleteRule(), | |
}, | |
} | |
} | |
func NotDeleteRule() privacy.QueryMutationRule { | |
type SoftDeleteFilter interface { | |
WhereDeleteTime(p entql.TimeP) | |
} | |
return privacy.FilterFunc(func(ctx context.Context, f privacy.Filter) error { | |
tf, ok := f.(SoftDeleteFilter) | |
if !ok { | |
return privacy.Denyf("unexpected filter type %T", f) | |
} | |
tf.WhereDeleteTime(entql.TimeNil()) | |
return privacy.Skip | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment