Skip to content

Commit

Permalink
feat: Adding in vars to the api
Browse files Browse the repository at this point in the history
  • Loading branch information
agmoss committed Oct 19, 2022
1 parent 5a90a62 commit 373dea0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion __specs__/useQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('useQueryRd', () => {

beforeEach(() => {
result = {
data: null,
data: undefined,
loading: false,
error: undefined,
_rd: {
Expand Down
20 changes: 9 additions & 11 deletions src/useQueryRd.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import {
DocumentNode,
QueryResult,
useQuery
} from '@apollo/client'
import { DocumentNode, OperationVariables, QueryResult, useQuery } from '@apollo/client'
import { RemoteData } from './rd'

export type QueryResultWithRemoteData<T> = QueryResult & { _rd: RemoteData<T> }
export type QueryResultWithRemoteData<T, V = OperationVariables> = QueryResult<T, V> & { _rd: RemoteData<T> }

/**
* @description Maps a `useQuery` QueryResult to the appropriate RemoteData discriminant
* @param d DocumentNode
* @param query DocumentNode
* @param vars Query variables
* @returns Everything from `QueryResult` with an accompanying `_rd` property wth the RemoteData object
*/
export const useQueryRd = <T>(
d: DocumentNode
): QueryResultWithRemoteData<T> => {
const res = useQuery<T>(d)
export const useQueryRd = <T, V = OperationVariables>(
query: DocumentNode,
vars?: V
): QueryResultWithRemoteData<T, V> => {
const res = useQuery<T, V>(query, { variables: vars ? { ...vars } : undefined })

if (!res.called) {
return {
Expand Down

0 comments on commit 373dea0

Please sign in to comment.