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

Simplify Queries by passing client / Query #187

Open
sgup opened this issue Aug 10, 2024 · 3 comments
Open

Simplify Queries by passing client / Query #187

sgup opened this issue Aug 10, 2024 · 3 comments

Comments

@sgup
Copy link
Contributor

sgup commented Aug 10, 2024

In a query like this:

useQuery(
  triplit,
  triplit
    .query('directors')
    .include('allFilms', (rel) => rel('allFilms').include('actors').build())
);

In the ClientQueryBuilder class, why not expose the current Q Query and a reference to the client, so it could be simplified further to:

useQuery(
  triplit
    .query('directors')
    .include('allFilms', (rel) => rel('allFilms').include('actors'))
    // no build() or client ref required as it can be accessed from class
);

& since useQuery is a react-specific hook, and thus made specifically to provide an easier/better interface over triplit, it would make sense to go further react-y and use the context/provider pattern:

import { TriplitProvider } from '@triplit/react'

function App() {
  <TriplitProvider client={triplitClient}>
    <div>
      <h1>My Triplit App</h1>
    </div>
  </TriplitProvider>
}

& in the components:

useQuery(
  "directors",
  (q) => q.include("allFilms", (rel) => rel("allFilms").include("actors"))
  // triplit client is provided by the TriplitProvider
);

Final Diff:

useQuery(
  triplit,
  triplit
    .query('directors')
    .include('allFilms', (rel) => rel('allFilms').include('actors').build())
);
  
// vs 
  
useQuery(
  "directors",
  (q) => q.include("allFilms", (rel) => rel("allFilms").include("actors"))
);
@matlin
Copy link
Contributor

matlin commented Aug 10, 2024

I like your idea! Especially the query builder callback ((q) => q.include(allFilms"....) Have you tried implementing this in "userland"? Seemingly only thing that would need to change is the @triplit/react package.

@MentalGear
Copy link
Contributor

// no build() or client ref required as it can be accessed from class

Yeah, I'd also like to see this simplified syntax across all frameworks (svelte, etc) that use useQuery!

@MentalGear
Copy link
Contributor

@sgup Maybe you could add a PR to add the abridged syntax version to the core ?

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

No branches or pull requests

3 participants