How should I implement a directive to format time
- Ngôn ngữ chính
- Go
- Star
- 10.8k
- Fork
- 1.3k
- Merge trung bình
- 2 ngày 36 phút
- Pull request đã merge (30 ngày)
- 26
Mô tả
I defined a directive:
```graphql
directive @timeFormat(format: String!) on FIELD
```
and use
```graphql
query {
todo {
createdAt @timeFormat(format:"2006-01-02")
}
}
```
I'm trying to implement it
```go
func NewSchema(client *ent.Client) graphql.ExecutableSchema {
return graph.NewExecutableSchema(graph.Config{
Resolvers: &Resolver{client: client},
Directives: graph.DirectiveRoot{
TimeFormat: func(ctx context.Context, obj interface{}, next graphql.Resolver, format string) (res interface{}, err error) {
res, err = next(ctx)
if err != nil {
return nil, err
}
t, ok := res.(time.Time)
if !ok {
return nil, errors.New("timeFormat directive only works on time.Time")
}
return t.Format(format), nil
},
},
})
}
```
I found that this is invalid.
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.