Skip to content
This repository was archived by the owner on Dec 19, 2022. It is now read-only.
This repository was archived by the owner on Dec 19, 2022. It is now read-only.

Chapter 4) The React useQuery() / useMutation #27

@KATT

Description

@KATT

The React API

Suggestion A) Similar to existing API

const trpc = createReactQueryHooks<AppRouter>()

const {queries, mutations, useQuery, useMutation} = trpc;

/// usage

function MyComponent() {
  // you can CMD+Click `postById` and jump straight to the backend resolver
  const query1 = useQuery([queries.postById, { id: 1 }], { /* react-query opts  */ })

  // Also same query and will be usable, but you lose jump to definition
  const query2 = useQuery(['postById', { id: 1 }], { /* react-query opts  */ })


  const mutation1 = useMutation(mutations.postUpdate); // <-- jump to definition by clicking `postUpdate`
  const mutation2 = useMutation('updatePost');

  // later used as `mutation.mutate(input)` or `mutation.mutateAsync(input)`

}

Suggestion B) Pseudo-call within hook

trpc.useQuery(client.query.postById({ id: 1 }), { /* react-query options */ })

Suggestion C) Skipping the tuple

Related decision

const trpc = createReactQueryHooks<AppRouter>()

const {queries, mutations, useQuery, useMutation} = trpc;

/// usage

function MyComponent() {
  // you can CMD+Click `postById` and jump straight to the backend resolver
  const query1 = useQuery(queries.postById, { 
    input: { id: 1 }, 
    /* [...] other react-query opts */
  }) 

  // Also same query and will be usable, but you lose jump to definition
  const query2 = useQuery('postById', {
    input: { id: 1 },
    /* [...] other react-query opts */
  }) 
}

Metadata

Metadata

Assignees

Labels

❕ RFCRequest for comments - please comment!

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions