错误的类型接口实现

我正在尝试按照Prisma 的指南来设置 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)

}


偶然的你
浏览 97回答 1
1回答

婷婷同学_

修改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) }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go