99designs / 99designs/gqlgen

How should I implement a directive to format time

Đang mở
#2,648 1 bình luận 1 reaction 0 người được giao Xem trên GitHub
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

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.