From 1fe6db9e880ba52e7527cb4c539121d35f4c3ea2 Mon Sep 17 00:00:00 2001 From: Ryan Delaney Date: Wed, 16 Jun 2021 13:21:23 -0700 Subject: [PATCH] Fix bug where subsequent query loads didn't use configured query function --- src/wired/component.tsx | 48 +++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/wired/component.tsx b/src/wired/component.tsx index 24e2653..00804e9 100644 --- a/src/wired/component.tsx +++ b/src/wired/component.tsx @@ -1,5 +1,5 @@ import type { NextPageContext, Redirect } from 'next'; -import Router, { useRouter } from 'next/router'; +import Router, { useRouter, NextRouter } from 'next/router'; import React, { ComponentType, ReactNode, Suspense, useEffect } from 'react'; import { loadQuery, PreloadedQuery, useQueryLoader } from 'react-relay/hooks'; import { @@ -17,10 +17,20 @@ import type { AnyPreloadedQuery } from './types'; // eslint-disable-next-line @typescript-eslint/no-explicit-any (RelayFeatureFlags as any).ENABLE_REQUIRED_DIRECTIVES = true; -export interface WiredOptions { +export type WiredProps< + P extends {} = {}, + Q extends OperationType = OperationType +> = P & { + CSN: boolean; + preloadedQuery: PreloadedQuery; +}; + +export interface WiredOptions { /** Fallback rendered when the page suspends. */ fallback?: ReactNode; - variablesFromContext?: (ctx: NextPageContext) => Variables; + variablesFromContext?: ( + ctx: NextPageContext | NextRouter + ) => Props['preloadedQuery']['variables']; /** Called when creating a Relay environment on the client. Should be idempotent. */ createClientEnvironment: () => Environment; /** Props passed to the component when rendering on the client. */ @@ -36,22 +46,16 @@ export interface WiredOptions { ) => Promise; } -export type WiredProps< - P extends {} = {}, - Q extends OperationType = OperationType -> = P & { - CSN: boolean; - preloadedQuery: PreloadedQuery; -}; - -function defaultVariablesFromContext(ctx: NextPageContext): Variables { +function defaultVariablesFromContext( + ctx: NextPageContext | NextRouter +): Variables { return ctx.query; } export function Wire( Component: ComponentType, query: GraphQLTaggedNode, - opts: WiredOptions + opts: WiredOptions ) { function WiredComponent(props: Props) { const router = useRouter(); @@ -61,8 +65,10 @@ export function Wire( ); useEffect(() => { - loadQuery(router.query); - }, [loadQuery, router.query]); + loadQuery( + (opts.variablesFromContext ?? defaultVariablesFromContext)(router) + ); + }, [loadQuery, router]); if (props.CSN) { return ( @@ -82,9 +88,9 @@ export function Wire( return WiredComponent; } -function wiredInitialProps( +function wiredInitialProps( query: GraphQLTaggedNode, - opts: WiredOptions + opts: WiredOptions ) { return async (ctx: NextPageContext) => { if (typeof window === 'undefined') { @@ -95,10 +101,10 @@ function wiredInitialProps( }; } -async function getServerInitialProps( +async function getServerInitialProps( ctx: NextPageContext, query: GraphQLTaggedNode, - opts: WiredOptions + opts: WiredOptions ) { const { variablesFromContext = defaultVariablesFromContext } = opts; const serverSideProps = opts.serverSideProps @@ -133,10 +139,10 @@ async function getServerInitialProps( }; } -function getClientInitialProps( +function getClientInitialProps( ctx: NextPageContext, query: GraphQLTaggedNode, - opts: WiredOptions + opts: WiredOptions ) { const { variablesFromContext = defaultVariablesFromContext } = opts; const clientSideProps = opts.clientSideProps