Я новичок в GraphQL и Lighthouse.
Насколько я понимаю из https://lighthouse-php.com/4.0/performance/n-plus-one.html#eager -loading-Relations , для вложенного запроса 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 имеют все правильные отношения. Например, сообщение класса имеет:
public function comments() {
return $this->hasMany(Comment::class, Comment::POST_ID);
}
И комментарий класса имеет:
public function post() {
return $this->belongsTo(Post::class);
}
и т. Д.