tailcallhq / tailcall

High Performance GraphQL Runtime
https://tailcall.run
Apache License 2.0
1.29k stars 254 forks source link

Handle entity resolvers in the n+1 checker #2892

Open meskill opened 1 month ago

meskill commented 1 month ago

Current implementation is checking only the fields while calculating n+1 issue. But after implementing https://github.com/tailcallhq/tailcall/issues/2639 resolvers could now be defined on types as well.

When considering entity resolver the entity resolver itself should be batched to prevent n+1 issue and that should be applied to any nested fields.

Handle resolvers on types while executing n+1 check

Example Configuration

schema
  @server(port: 8000)
  @upstream(baseURL: "http://jsonplaceholder.typicode.com", httpCache: 42, batch: {delay: 100}) {
  query: Query
}

type Query {
  posts: [Post] @http(path: "/posts")
  users: [User] @http(path: "/users")
  user(id: Int!): User @http(path: "/users/{{.args.id}}")
}

type User @http(path: "/users", query: [{key: "id", value: "{{.value.user.id}}"}], batchKey: ["id"]) {
  id: Int!
  name: String!
  username: String!
  email: String!
  phone: String
  website: String
}

type Post {
  id: Int!
  userId: Int!
  title: String!
  body: String!
  user: User @call(steps: [{query: "user", args: {id: "{{.value.userId}}"}}])
}

Actual

tailcall check -n ./config.graphql
 INFO File read: ./config.graphql ... ok
 INFO Config ./config.graphql ... ok
 INFO N + 1 detected: 1
query { posts { user } }

Expected

tailcall check -n ./config.graphql
 INFO File read: ./config.graphql ... ok
 INFO Config ./config.graphql ... ok
 INFO N + 1 detected: 2
query { posts { user } }
query { _entities { __typename: "User" } }
tusharmath commented 1 month ago

/bounty 150

algora-pbc[bot] commented 1 month ago

💎 $150 bounty • Tailcall Inc.

Steps to solve:

  1. Start working: Comment /attempt #2892 with your implementation plan
  2. Submit work: Create a pull request including /claim #2892 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

🙏 Thank you for contributing to tailcallhq/tailcall! 🧐 Checkout our guidelines before you get started. 💵 More about our bounty program.

Attempt Started (GMT+0) Solution
🟢 @meskill #2978
algora-pbc[bot] commented 1 month ago

💡 @meskill submitted a pull request that claims the bounty. You can visit your bounty board to reward.