Skip to content

Commit

Permalink
GraphQL useMutation options (redwoodjs#3041)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjreimer authored Jul 27, 2021
1 parent 03140b2 commit d9d3852
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
11 changes: 10 additions & 1 deletion packages/web/src/apollo/typeOverride.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
// Override useQuery and useMutation types
import type { QueryResult, MutationTuple } from '@apollo/client'
import type {
QueryHookOptions,
QueryResult,
MutationHookOptions,
MutationTuple,
} from '@apollo/client'

// @MARK: Override relevant types from Apollo here
declare global {
interface QueryOperationResult extends QueryResult {}
interface MutationOperationResult<TData, TVariables>
extends MutationTuple<TData, TVariables> {}

interface GraphQLQueryHookOptions extends QueryHookOptions {}
interface GraphQLMutationHookOptions extends MutationHookOptions {}
}

export {}
15 changes: 4 additions & 11 deletions packages/web/src/components/GraphQLHooksProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import type { DocumentNode } from 'graphql'

export interface GraphQLHookOptions {
variables?: Record<string, any>
refetchQueries?: { query: DocumentNode; variables?: Record<string, any> }[]
onCompleted?: (data: any) => void
[key: string]: any
}

type DefaultUseQueryType = (
query: DocumentNode,
options?: GraphQLHookOptions
options?: GraphQLQueryHookOptions
) => QueryOperationResult

type DefaultUseMutationType = (
mutation: DocumentNode,
options?: GraphQLHookOptions
options?: GraphQLMutationHookOptions
) => MutationOperationResult
export interface GraphQLHooks<
TuseQuery = DefaultUseQueryType,
Expand Down Expand Up @@ -73,14 +66,14 @@ export const GraphQLHooksProvider = <

export function useQuery<TData = any>(
query: DocumentNode,
options?: GraphQLHookOptions
options?: GraphQLQueryHookOptions
): QueryOperationResult<TData> {
return React.useContext(GraphQLHooksContext).useQuery(query, options)
}

export function useMutation<TData = any>(
mutation: DocumentNode,
options?: GraphQLHookOptions
options?: GraphQLMutationHookOptions
): MutationOperationResult<TData> {
return React.useContext(GraphQLHooksContext).useMutation(mutation, options)
}
12 changes: 12 additions & 0 deletions packages/web/src/global.web-auto-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ declare global {
// not defining it here, because it gets overriden by Apollo provider anyway
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface MutationOperationResult<TData = any, TVariables = any> {}

// Overridable useQuery and useMutation hooks
interface GraphQLQueryHookOptions {
variables?: Record<string, any>
[key: string]: any
}

export interface GraphQLMutationHookOptions {
variables?: Record<string, any>
onCompleted?: (data: any) => void
[key: string]: any
}
}
8 changes: 4 additions & 4 deletions tasks/framework-tools/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2429,10 +2429,10 @@
resolved "https://registry.yarnpkg.com/@open-draft/until/-/until-1.0.3.tgz#db9cc719191a62e7d9200f6e7bab21c5b848adca"
integrity sha512-Aq58f5HiWdyDlFffbbSjAlv596h/cOnt2DO1w3DOC7OJ5EHs0hd/nycJfiu9RJbT6Yk6F1knnRRXNSpxoIVZ9Q==

"@redwoodjs/[email protected].1":
version "0.35.1"
resolved "https://registry.yarnpkg.com/@redwoodjs/eslint-config/-/eslint-config-0.35.1.tgz#db6155303775b09734045f4c8dbc1a22bd9fa0ac"
integrity sha512-WIHQESsyAsNb9syugVCJU1kO5meXA8wSuDHEIoZFvbZ1ylegusdksAdeX3hwhgx6DlNbdJZRlkqLy8ilMd74Mw==
"@redwoodjs/[email protected].2":
version "0.35.2"
resolved "https://registry.yarnpkg.com/@redwoodjs/eslint-config/-/eslint-config-0.35.2.tgz#3e6a6fb7aa3444a8ce723403f260c82efc4fcbec"
integrity sha512-Q3IxuGCFl1wnEV5zbeqBh3pSCrbPBd2rK3iQmqPtBD9ni4OnfaYwyYOo+eLIYufUmULSn/y8QNT5Flwylapt8g==
dependencies:
"@typescript-eslint/eslint-plugin" "4.28.2"
"@typescript-eslint/parser" "4.28.2"
Expand Down

0 comments on commit d9d3852

Please sign in to comment.