lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Support optimized select for top-level query

Open Lyrisbee opened this issue 3 years ago • 5 comments

  • [x] Added or updated tests
  • [ ] Documented user facing changes
  • [ ] Updated CHANGELOG.md

I made some updates from https://github.com/nuwave/lighthouse/pull/1626.

Tests passed directives

  1. @all
  2. @find
  3. @paginate

The query builder and scout builder are not optimized.

type Query {
  postsSearch(search: String @search): [Post!]! @paginate // scout builder
}

Tests passed relation types

  1. hasOne
  2. hasMany
  3. morphOne
  4. morphMany
  5. belongsTo
  6. belongsToMany

Only the root query will be optimized. The nested queries are not optimized.

type User {
  name: String!
  posts: [Post!]! @hasMany
}

type Post {
  id: ID!
  title: String!
}

type Query {
  users: [User!]! @all
}
{
  users {
    name
    posts {
      id
      title
    }
  }
}
# query logs
# optimized
select `id`, `name` from `users`
# the nested query is not optimized
select * from `posts` where `posts`.`user_id` in (1, 2) and `posts`.`deleted_at` is null

Changes Only query the selection fields.

Breaking changes

  • Custom attribute needs to use @select
public function getCompanyNameAttribute(): string
{
    return $this->company->name;
}
type User {
    id: ID!
    companyName: String! @select(columns: ["company_id"])
}
  • The result may change when using optimized select. For predictable results, you should use @orderBy https://dba.stackexchange.com/a/6053

Note This PR was only tested in our own project, and there may be some edge cases that are not considered.

Lyrisbee avatar Nov 21 '22 08:11 Lyrisbee

@spawnia

Is there anything that needs to change for this pull request to be merged?

bepsvpt avatar Jan 27 '23 08:01 bepsvpt

Thank you for your continued efforts on this. Unfortunately this pull request needs significant rework for the reasons I outlined in my review.

spawnia avatar Jan 27 '23 09:01 spawnia

You can add the following entry to CHANGELOG.md:

### Added

- Allow optimizing `SELECT` https://github.com/nuwave/lighthouse/pull/2235 by @Lyrisbee, @bepsvpt

spawnia avatar Feb 22 '23 08:02 spawnia

Thanks @spawnia, could we add a @storipress tag as well — we'll get back to this one sometime next month as we're working on some other features right now. We expect this to be done EO March

HelloAlexPan avatar Feb 22 '23 09:02 HelloAlexPan

Thanks @spawnia, could we add a @storipress tag as well — we'll get back to this one sometime next month as we're working on some other features right now. We expect this to be done EO March

Sure, that sounds fine to me.

spawnia avatar Feb 22 '23 10:02 spawnia