gpt4 book ai didi

go - 错误的类型接口(interface)实现

转载 作者:行者123 更新时间:2023-12-01 22:17:06 24 4
gpt4 key购买 nike

我正在尝试关注 Prisma's设置 GraphQL Go 服务器的指南。

解析器.go

package main

import (
"context"
"prismago/hello-world/generated/prisma-client"
)

// Resolver struct
type Resolver struct {
Prisma *prisma.Client
}

// Mutation MutationResolver
func (r *Resolver) Mutation() MutationResolver {
return &mutationResolver{r}
}

// Post PostResolver
func (r *Resolver) Post() PostResolver {
return &postResolver{r}
}

// Query QueryResolver
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}

// User UserResolver
func (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)
}
func (r *queryResolver) Post(ctx context.Context, postID string) (*prisma.Post, error) {
return r.Prisma.Post(prisma.PostWhereUniqueInput{ID: &postID}).Exec(ctx)
}
func (r *queryResolver) PostsByUser(ctx context.Context, userID string) ([]prisma.Post, error) {
return r.Prisma.Posts(&prisma.PostsParams{
Where: &prisma.PostWhereInput{
Author: &prisma.UserWhereInput{
ID: &userID,
}},
}).Exec(ctx)
}
func (r *queryResolver) Users(ctx context.Context) ([]prisma.User, error) {
return r.Prisma.Users(&prisma.UsersParams{
Where: &prisma.UserWhereInput{},
}).Exec(ctx)
}

type userResolver struct{ *Resolver }

func (r *userResolver) Posts(ctx context.Context, obj *prisma.User) ([]prisma.Post, error) {
return r.Prisma.User(prisma.UserWhereUniqueInput{ID: &obj.ID}).Posts(nil).Exec(ctx)
}

运行 go run ./server生成以下错误:
cannot use &userResolver literal (type *userResolver) as type UserResolver in return argument:
*userResolver does not implement UserResolver (wrong type for Posts method)
have Posts(context.Context, *"prismago/hello-world/generated/prisma-client".User) ([]"prismago/hello-world/generated/prisma-client".Post, error)
want Posts(context.Context, *"prismago/hello-world/generated/prisma-client".User) ([]*"prismago/hello-world/generated/prisma-client".Post, error)


我试图修复类型错误:
func (r *userResolver) Posts(ctx context.Context, obj *prisma.User) ([]*prisma.Post, error) {
return r.Prisma.User(prisma.UserWhereUniqueInput{ID: &obj.ID}).Posts(nil).Exec(ctx)
}


错误:
cannot use []"prismago/hello-world/generated/prisma-client".Post as type []*"prismago/hello-world/generated/prisma-client".Post in return argument

我究竟做错了什么?

相关问题: https://github.com/99designs/gqlgen/issues/714

最佳答案

修改generated.go文件接口(interface)接受 []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)
}

关于go - 错误的类型接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58908466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com