Skip to content

Commit

Permalink
Fix fragment initial load in React Framework (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis authored Sep 25, 2023
1 parent 386fc4c commit 09eddb4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
19 changes: 19 additions & 0 deletions e2e/react/src/components/SponsorInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { graphql, useFragment, type SponsorInfo } from '$houdini'

type Props = {
sponsor: SponsorInfo
}

export default function SponsorSummary(props: Props) {
const data = useFragment(
props.sponsor,
graphql(`
fragment SponsorInfo on Sponsor {
name
avatarUrl
}
`)
)

return <div>{data?.name}</div>
}
1 change: 1 addition & 0 deletions e2e/react/src/routes/+page.gql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
query SponsorList {
sponsors {
name
...SponsorInfo
}
}
18 changes: 17 additions & 1 deletion e2e/react/src/routes/+page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { cache } from '$houdini/runtime'
import SponsorInfo from '~/components/SponsorInfo'

import type { PageProps } from './$types'

if (globalThis.window) {
// @ts-ignore
window.cache = cache
}

export default function ({ SponsorList }: PageProps) {
return <div>{JSON.stringify(SponsorList)}</div>
return (
<div>
{SponsorList.sponsors.map((sponsor) => (
<div key={sponsor.name}>
<SponsorInfo sponsor={sponsor} />
</div>
))}
</div>
)
}
9 changes: 4 additions & 5 deletions packages/houdini-react/src/runtime/hooks/useFragment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import cache from '$houdini/runtime/cache'
import { deepEquals } from '$houdini/runtime/lib/deepEquals'
import { fragmentKey } from '$houdini/runtime/lib/types'
import type { GraphQLObject, GraphQLVariables, FragmentArtifact } from '$houdini/runtime/lib/types'
import * as React from 'react'

import { useRouterContext } from '../routing'
import { useDeepCompareMemoize } from './useDeepCompareEffect'
import { useDocumentSubscription } from './useDocumentSubscription'

Expand All @@ -15,6 +15,8 @@ export function useFragment<
reference: _Data | { [fragmentKey]: _ReferenceType } | null,
document: { artifact: FragmentArtifact }
) {
const { cache } = useRouterContext()

// get the fragment reference info
const { parent, variables, loading } = fragmentReference<_Data, _Input, _ReferenceType>(
reference,
Expand Down Expand Up @@ -46,10 +48,7 @@ export function useFragment<
stuff: {
parentID: parent,
},
// setup = true?
// we don't need to do the first read because we
// have an initial value...
// does Boolean(initialValue) === { setup: true }
setup: true,
},
})

Expand Down
2 changes: 1 addition & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dependsOn": ["^compile"]
},
"typedefs": {
"dependsOn": ["^typedefs"]
"dependsOn": ["^typedefs", "^compile"]
},
"build": {
"dependsOn": ["typedefs", "compile"]
Expand Down

0 comments on commit 09eddb4

Please sign in to comment.