我正在尝试按照Prisma 的指南来设置 GraphQL Go 服务器。解析器.gopackage mainimport ( "context" "prismago/hello-world/generated/prisma-client")// Resolver structtype Resolver struct { Prisma *prisma.Client}// Mutation MutationResolverfunc (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r}}// Post PostResolverfunc (r *Resolver) Post() PostResolver { return &postResolver{r}}// Query QueryResolverfunc (r *Resolver) Query() QueryResolver { return &queryResolver{r}}// User UserResolverfunc (r *Resolver) User() UserResolver { return &userResolver{r}}type mutationResolver struct{ *Resolver }func (r *mutationResolver) CreateUser(ctx context.Context, name string) (*prisma.User, error) { return r.Prisma.CreateUser(prisma.UserCreateInput{ Name: name, }).Exec(ctx)}func (r *mutationResolver) CreateDraft(ctx context.Context, title string, userID string) (*prisma.Post, error) { return r.Prisma.CreatePost(prisma.PostCreateInput{ Title: title, Author: &prisma.UserCreateOneWithoutPostsInput{ Connect: &prisma.UserWhereUniqueInput{ID: &userID}, }, }).Exec(ctx)}func (r *mutationResolver) Publish(ctx context.Context, postID string) (*prisma.Post, error) { published := true return r.Prisma.UpdatePost(prisma.PostUpdateParams{ Where: prisma.PostWhereUniqueInput{ID: &postID}, Data: prisma.PostUpdateInput{Published: &published}, }).Exec(ctx)}type postResolver struct{ *Resolver }func (r *postResolver) Author(ctx context.Context, obj *prisma.Post) (*prisma.User, error) { return r.Prisma.Post(prisma.PostWhereUniqueInput{ID: &obj.ID}).Author().Exec(ctx)}type queryResolver struct{ *Resolver }func (r *queryResolver) PublishedPosts(ctx context.Context) ([]prisma.Post, error) { published := true return r.Prisma.Posts(&prisma.PostsParams{ Where: &prisma.PostWhereInput{Published: &published}, }).Exec(ctx)}
1 回答
婷婷同学_
TA贡献1844条经验 获得超8个赞
修改generated.go
文件接口以接受返回的[]prisma.Post
而不是[]*prisma.Post
.
改变:
type UserResolver interface { Posts(ctx context.Context, obj *prisma.User) ([]*prisma.Post, error) }
到:
type UserResolver interface { Posts(ctx context.Context, obj *prisma.User) ([]prisma.Post, error) }
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报
0/150
提交
取消