@@ -17,10 +17,13 @@ package main
1717import (
1818 "flag"
1919 "fmt"
20+ "strings"
2021
22+ "entgo.io/contrib/entgql"
2123 entopts "entgo.io/contrib/entproto/cmd/protoc-gen-ent/options/ent"
2224 "entgo.io/contrib/schemast"
2325 "entgo.io/ent"
26+ "entgo.io/ent/schema"
2427 "entgo.io/ent/schema/edge"
2528 "entgo.io/ent/schema/field"
2629 "google.golang.org/protobuf/compiler/protogen"
@@ -108,8 +111,37 @@ func toSchema(m *protogen.Message, opts *entopts.Schema) (*schemast.UpsertSchema
108111 if opts .Name != nil {
109112 name = opts .GetName ()
110113 }
114+ var annotations []schema.Annotation
115+ gql := opts .GetGql ()
116+ if gql != nil {
117+ if gql .GetQueryField () {
118+ if gql .GetQueryFieldName () != "" {
119+ annotations = append (annotations , entgql .QueryField (gql .GetQueryFieldName ()).Annotation )
120+ } else {
121+ annotations = append (annotations , entgql .QueryField ().Annotation )
122+ }
123+ }
124+ if gql .GetType () != "" {
125+ annotations = append (annotations , entgql .Type (gql .GetType ()))
126+ }
127+ if gql .GetRelayConnection () {
128+ annotations = append (annotations , entgql .RelayConnection ())
129+ }
130+ var create , update = gql .GetMutationCreate (), gql .GetMutationUpdate ()
131+ if create || update {
132+ var options []entgql.MutationOption
133+ if create {
134+ options = append (options , entgql .MutationCreate ())
135+ }
136+ if update {
137+ options = append (options , entgql .MutationUpdate ())
138+ }
139+ annotations = append (annotations , entgql .Mutations (options ... ))
140+ }
141+ }
111142 out := & schemast.UpsertSchema {
112- Name : name ,
143+ Name : name ,
144+ Annotations : annotations ,
113145 }
114146 for _ , f := range m .Fields {
115147 if isEdge (f ) {
@@ -130,7 +162,16 @@ func toSchema(m *protogen.Message, opts *entopts.Schema) (*schemast.UpsertSchema
130162}
131163
132164func isEdge (f * protogen.Field ) bool {
133- return f .Desc .Kind () == protoreflect .MessageKind
165+ isMessageKind := f .Desc .Kind () == protoreflect .MessageKind
166+ if isMessageKind {
167+ switch f .Desc .Message ().FullName () {
168+ case "google.protobuf.Timestamp" :
169+ return false
170+ case "google.type.Date" :
171+ return false
172+ }
173+ }
174+ return isMessageKind
134175}
135176
136177func toEdge (f * protogen.Field ) (ent.Edge , error ) {
@@ -194,6 +235,15 @@ func toField(f *protogen.Field) (ent.Field, error) {
194235 values = append (values , string (pbEnum .Get (i ).Name ()))
195236 }
196237 fld = field .Enum (name ).Values (values ... )
238+ case protoreflect .MessageKind :
239+ switch f .Desc .Message ().FullName () {
240+ case "google.protobuf.Timestamp" :
241+ fld = field .Time (name )
242+ case "google.type.Date" :
243+ fld = field .Time (name )
244+ default :
245+ return nil , fmt .Errorf ("protoc-gen-ent: unsupported kind %q" , f .Desc .Kind ())
246+ }
197247 default :
198248 return nil , fmt .Errorf ("protoc-gen-ent: unsupported kind %q" , f .Desc .Kind ())
199249 }
@@ -214,6 +264,46 @@ func applyFieldOpts(fld ent.Field, opts *entopts.Field) {
214264 d .Tag = opts .GetStructTag ()
215265 d .StorageKey = opts .GetStorageKey ()
216266 d .SchemaType = opts .GetSchemaType ()
267+
268+ gql := opts .GetGql ()
269+ if gql != nil {
270+ var annotations []schema.Annotation
271+ if gql .GetSkipType () {
272+ annotations = append (annotations , entgql .Skip (entgql .SkipType ))
273+ } else {
274+ if gql .GetOrderField () {
275+ if gql .GetOrderFieldName () != "" {
276+ annotations = append (annotations , entgql .OrderField (gql .GetOrderFieldName ()))
277+ } else {
278+ annotations = append (annotations , entgql .OrderField (strings .ToUpper (fld .Descriptor ().Name )))
279+ }
280+ }
281+ if gql .GetType () != "" {
282+ annotations = append (annotations , entgql .Type (gql .GetType ()))
283+ }
284+ skipEnum , skipOrder , skipWhere , skipCreate , skipUpdate := gql .GetSkipEnumField (), gql .GetSkipOrderField (), gql .GetSkipWhereInput (), gql .GetSkipMutationCreateInput (), gql .GetSkipMutationUpdateInput ()
285+ if skipEnum || skipOrder || skipWhere || skipCreate || skipUpdate {
286+ var skipModeVals = []bool {
287+ skipEnum , skipOrder , skipWhere , skipCreate , skipUpdate ,
288+ }
289+ var skipModeList = []entgql.SkipMode {
290+ entgql .SkipEnumField ,
291+ entgql .SkipOrderField ,
292+ entgql .SkipWhereInput ,
293+ entgql .SkipMutationCreateInput ,
294+ entgql .SkipMutationUpdateInput ,
295+ }
296+ var skipMode entgql.SkipMode
297+ for i , mode := range skipModeList {
298+ if skipModeVals [i ] {
299+ skipMode |= mode
300+ }
301+ }
302+ annotations = append (annotations , entgql .Skip (skipMode ))
303+ }
304+ }
305+ d .Annotations = annotations
306+ }
217307}
218308
219309func applyEdgeOpts (edg ent.Edge , opts * entopts.Edge ) {
@@ -229,6 +319,34 @@ func applyEdgeOpts(edg ent.Edge, opts *entopts.Edge) {
229319 Columns : sk .GetColumns (),
230320 }
231321 }
322+
323+ gql := opts .GetGql ()
324+ if gql != nil {
325+ var annotations []schema.Annotation
326+ if gql .GetSkipType () {
327+ annotations = append (annotations , entgql .Skip (entgql .SkipType ))
328+ } else {
329+ skipWhere , skipCreate , skipUpdate := gql .GetSkipWhereInput (), gql .GetSkipMutationCreateInput (), gql .GetSkipMutationUpdateInput ()
330+ if skipWhere || skipCreate || skipUpdate {
331+ var skipModeVals = []bool {
332+ skipWhere , skipCreate , skipUpdate ,
333+ }
334+ var skipModeList = []entgql.SkipMode {
335+ entgql .SkipWhereInput ,
336+ entgql .SkipMutationCreateInput ,
337+ entgql .SkipMutationUpdateInput ,
338+ }
339+ var skipMode entgql.SkipMode
340+ for i , mode := range skipModeList {
341+ if skipModeVals [i ] {
342+ skipMode |= mode
343+ }
344+ }
345+ annotations = append (annotations , entgql .Skip (skipMode ))
346+ }
347+ }
348+ d .Annotations = annotations
349+ }
232350}
233351
234352type placeholder struct {
0 commit comments