Skip to content

Commit

Permalink
hotfix(codegen): INFRA-560 fixed useAllObjects (#5299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alllex202 authored Oct 1, 2024
1 parent 9593bff commit 20568a6
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/codegen/generate.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FetchMoreQueryOptions } from '@apollo/client/core/watchQueryOptions'
import { TypedDocumentNode } from '@graphql-typed-document-node/core'
import dayjs from 'dayjs'
import { DocumentNode } from 'graphql'
import get from 'lodash/get'
import isFunction from 'lodash/isFunction'
import { useCallback, useEffect, useRef, useState } from 'react'

Expand Down Expand Up @@ -379,7 +380,11 @@ export function generateReactHooks<
}

function useAllObjects (variables: QueryVariables, options?: QueryHookOptions<IUseObjectsQueryReturnType<GQLObject>, QueryVariables>) {
const { objs, count, error, loading, refetch: _refetch, fetchMore, stopPolling } = useObjects(variables, options)
const fetchPolicy = get(options, 'fetchPolicy')
const { objs, count, error, loading, refetch: _refetch, fetchMore, stopPolling } = useObjects(variables, {
...options,
fetchPolicy: fetchPolicy && fetchPolicy !== 'no-cache' ? fetchPolicy : 'network-only',
})
const [data, setData] = useState(objs)
const [fetchMoreError, setFetchMoreError] = useState()
const innerLoadingRef = useRef(false)
Expand Down Expand Up @@ -417,6 +422,14 @@ export function generateReactHooks<
variables: {
skip: data.length,
},
updateQuery (previousData: IUseObjectsQueryReturnType<GQLObject>, { fetchMoreResult, variables: { skip } }) {
// Slicing is necessary because the existing data is immutable, and frozen in development.
const updatedObjs = previousData.objs.slice(0)
for (let i = 0; i < fetchMoreResult.objs.length; ++i) {
updatedObjs[skip + i] = fetchMoreResult.objs[i]
}
return { ...previousData, objs: updatedObjs, meta: fetchMoreResult.meta }
},
})
// @ts-ignore
.then(({ data }) => setData(prevData => [...prevData, ...data.objs]))
Expand Down

0 comments on commit 20568a6

Please sign in to comment.