Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable typed queries for threads #1568

Merged
merged 35 commits into from
Apr 24, 2024
Merged

Enable typed queries for threads #1568

merged 35 commits into from
Apr 24, 2024

Conversation

FlowFlorent
Copy link
Contributor

@FlowFlorent FlowFlorent commented Apr 12, 2024

Description

@liveblocks/react

  • useThreads now handles typed queries

Example:

  useThreads({
    query: {
      metadata: {
        organization: {
          startsWith: "liveblocks:",
        },
      },
    },
  })

@liveblocks/node

  • getThreads now handles typed queries
  client.getThreads<{
    status: "open";
    priority: 3;
    organization: "liveblocks:engineering";
  }>({
    roomId: "room1",
    query: {
      metadata: {
        status: "open",
        priority: 3,
        organization: {
          startsWith: "liveblocks:",
        },
      },
    },
  })

@FlowFlorent FlowFlorent changed the base branch from main to 1.12 April 12, 2024 21:51
@FlowFlorent FlowFlorent changed the base branch from 1.12 to threads-filter-improvments April 12, 2024 21:52
@FlowFlorent FlowFlorent changed the base branch from threads-filter-improvments to feat/threads-query April 12, 2024 21:52
Copy link

vercel bot commented Apr 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
examples-nextjs-comments-canvas ❌ Failed (Inspect) Apr 23, 2024 8:44pm
2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
examples-nextjs-comments-audio ⬜️ Ignored (Inspect) Visit Preview Apr 23, 2024 8:44pm
examples-nextjs-comments-notifications ⬜️ Ignored (Inspect) Visit Preview Apr 23, 2024 8:44pm

@FlowFlorent FlowFlorent marked this pull request as ready for review April 15, 2024 21:09
Copy link

vercel bot commented Apr 15, 2024

Deployment failed with the following error:

Too many requests - try again in 3 minutes (more than 60, code: "api-deployments-flood").

Copy link
Contributor

@nvie nvie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few comments. Overall, I think the safest way to write a function that takes a more complex object and produces a valid query string is to use a recursive "prettifier", which takes an AST (in this case the "typed query", which sort of is a simplified AST) and produces a string value, i.e. the reverse operation of a parse.

You can see an example of it here:
https://github.com/liveblocks/liveblocks-cloudflare/blob/main/packages/liveblocks-query-parser/src/prettify/index.ts

With a function like that, it's impossible to generate syntactically invalid outputs, and it will take care of all the proper escaping. (Notice how this function calls itself recursively on sub parts of the AST, until it reaches the "nodes", which it basically just uses JSON.stringify() for.)

@nvie
Copy link
Contributor

nvie commented Apr 16, 2024

@FlowFlorent Let me know once you're done with this PR. I'm happy to play devils advocate and try to come up with unit tests that won't pass.

@FlowFlorent
Copy link
Contributor Author

@nvie Thanks for all your comments, I've adjusted the PR.

I am still a bit uncertain about the escaping part though 🤔

Copy link

vercel bot commented Apr 17, 2024

Deployment failed with the following error:

Too many requests - try again in 4 minutes (more than 60, code: "api-deployments-flood").

Base automatically changed from feat/threads-query to 1.12 April 17, 2024 21:15
@FlowFlorent FlowFlorent requested a review from CTNicholas as a code owner April 17, 2024 21:15
@FlowFlorent FlowFlorent requested a review from nvie April 18, 2024 14:51
Copy link
Contributor

@nvie nvie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, @FlowFlorent! 🙌

@@ -45,6 +45,7 @@
"devDependencies": {
"@liveblocks/eslint-config": "*",
"@liveblocks/jest-config": "*",
"@liveblocks/query-parser": "^0.0.3",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"@liveblocks/query-parser": "^0.0.3",
"@liveblocks/query-parser": "^0.0.4",

@FlowFlorent FlowFlorent merged commit 0d104f5 into 1.12 Apr 24, 2024
58 of 60 checks passed
@FlowFlorent FlowFlorent deleted the typed-query branch April 24, 2024 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants