1

我是 GraphQL 和 Lighthouse 的新手。

我从https://lighthouse-php.com/4.0/performance/n-plus-one.html#eager-loading-relationships的理解是,对于嵌套的 GraphQL 查询,“在幕后,Lighthouse 会将关系查询批处理在一起在单个数据库查询中。”

但是,我在日志中看到所有这些查询应该只有 1 个:

select count(*) as aggregate from `posts`
select * from `posts` order by `created_at` asc limit 20 offset 0
select * from `post_files` where `post_files`.`post_id` in (249, 5...
select * from `post_tags` where `post_tags`.`post_id` in (249, 5...
select * from `comments` where `comments`.`post_id` in (249, 5...
select * from `files` where `files`.`id` in (269, 615, ...
select * from `tags` where `tags`.`id` in (2, 3, 4, ...

这是我的 GraphQL 查询:

gql`
    query Posts($page: Int) {
        posts(
            first: 20
            page: $page,            
            orderBy: { field: CREATED_AT, order: ASC }
        ) {
            paginatorInfo {
                currentPage
                hasMorePages
                total
            }
            data {
                id
                content 
                created_at
                postFiles {
                    file {
                        id
                        original_name 
                        extension
                    }
                }
                postTags {
                    id
                    tag {
                        id
                        label 
                    }
                }
                comments {
                    id
                    user_id
                    message 
                    created_at
                    seen_at
                }
            }
        }
    }
`;

我的 schema.graphql 有权利@belongsTo@hasMany指令:

"A date string with format `Y-m-d`, e.g. `2011-05-23`."
scalar Date @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\Date")

"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-05-23 13:43:32`."
scalar DateTime
    @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTime")

"A datetime and timezone string in ISO 8601 format `Y-m-dTH:i:sO`, e.g. `2020-04-20T13:53:12+02:00`."
scalar DateTimeTz
    @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTimeTz")

type Comment {
    id: Int!
    user: User! @belongsTo
    user_id: Int!
    post: Post! @belongsTo
    post_id: Int!
    message: String!
    emailed_at: DateTime
    seen_at: DateTime
    created_at: DateTime!
    modified_at: DateTime!
}

type File {
    id: Int!
    original_name: String!
    extension: String!
    postFiles: [PostFile!]! @hasMany
}

type Post {
    id: Int!
    user: User! @belongsTo
    user_id: Int!
    content: String
    origin: String
    created_at: DateTime!
    modified_at: DateTime!
    postFiles: [PostFile!]! @hasMany
    comments: [Comment!]! @hasMany
    postTags: [PostTag!]! @hasMany
}

type PostFile {
    post: Post! @belongsTo
    post_id: Int!
    file: File! @belongsTo
    file_id: Int!
}

type PostTag {
    id: Int!
    post: Post! @belongsTo
    post_id: Int!
    tag: Tag! @belongsTo
    tag_id: Int!
    created_at: DateTime!
    modified_at: DateTime!
}

type Tag {
    id: Int!
    label: String!
    hash: String!
    created_at: DateTime!
    modified_at: DateTime!
    postTags: [PostTag!]! @hasMany
}

type User {
    id: Int!
    email: String!
    password: String!
    salt: String
    db_key: String
    created_at: DateTime!
    modified_at: DateTime!
    posts: [Post!]! @hasMany
    comments: [Comment!]! @hasMany
}

type Query {
    comments: [Comment!]! @all
    files: [File!]! @all    
    posts(
        orderBy: _ @orderBy(columns: ["id", "created_at"], order: ASC)
    ): [Post!]! @paginate(defaultCount: 20)
    post(id: ID! @eq): Post @find
    postFiles: [PostFile!]! @all
    postTags: [PostTag!]! @all
    tags: [Tag!]! @all
    users: [User!]! @all
}

我的 Laravel 模型具有所有正确的关系。例如,类 Post 有:

public function comments() {
    return $this->hasMany(Comment::class, Comment::POST_ID);
}

和类评论有:

 public function post() {
    return $this->belongsTo(Post::class); 
 }

等等。

4

0 回答 0