Skip to content

Commit

Permalink
feat!: Use query option pattern and support useSuspense (#263)
Browse files Browse the repository at this point in the history
* breaking change: Change query operation to object; Update generate query components to use query function; Add suspense query to the generation; Update context

* fixes

* fix camel case difference from 'case' and 'lodash'

* revert name change

* revert context name

* fix tests

* small fixes

* Update context template with the correct types

---------

Co-authored-by: jan.silhan <jan.silhan@pwc.com>
Co-authored-by: Fabien Bernard <fabien0102@hotmail.com>
  • Loading branch information
3 people authored Jan 30, 2025
1 parent 88e09cc commit b97044f
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 295 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions plugins/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

Collection of typescript generators & utils

## Options

### generateSuspenseQueries

Generate `useSuspenseQuery` wrapper along side `useQuery`.

## Generators

### generateSchemaType
Expand Down
207 changes: 101 additions & 106 deletions plugins/typescript/src/core/createOperationQueryFnNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,130 +152,125 @@ export const createOperationQueryFnNodes = ({
undefined
),
],
f.createTypeReferenceNode(f.createIdentifier("Promise"), [dataType])
f.createTypeReferenceNode(f.createIdentifier("Promise"), [
dataType,
])
)
),
]),

f.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
f.createObjectLiteralExpression(
[
f.createPropertyAssignment(
f.createIdentifier("queryKey"),
f.createCallExpression(
f.createIdentifier("queryKeyFn"),
undefined,
[
f.createObjectLiteralExpression(
[
f.createPropertyAssignment(
f.createIdentifier("path"),
f.createStringLiteral(camelizedPathParams(url))
),
f.createPropertyAssignment(
f.createIdentifier("operationId"),
f.createStringLiteral(operationId)
),
f.createShorthandPropertyAssignment(
f.createIdentifier("variables"),
undefined
),
],
true
),
]
)
f.createIdentifier("queryKeyFn"),
undefined,
[
f.createObjectLiteralExpression(
[
f.createPropertyAssignment(
f.createIdentifier("path"),
f.createStringLiteral(camelizedPathParams(url))
),
f.createPropertyAssignment(
f.createIdentifier("operationId"),
f.createStringLiteral(operationId)
),
f.createShorthandPropertyAssignment(
f.createIdentifier("variables"),
undefined
),
],
true
),
]
)
),
f.createPropertyAssignment(
f.createIdentifier("queryFn"),
f.createArrowFunction(
[f.createModifier(ts.SyntaxKind.AsyncKeyword)],
undefined,
verb === "get"
? [
f.createParameterDeclaration(
undefined,
undefined,
f.createObjectBindingPattern([
f.createBindingElement(
undefined,
undefined,
f.createIdentifier("signal"),
undefined
),
]),
undefined,
f.createTypeLiteralNode([
f.createPropertySignature(
undefined,
f.createIdentifier("signal"),
f.createToken(ts.SyntaxKind.QuestionToken),
f.createTypeReferenceNode(
f.createIdentifier("AbortSignal"),
undefined
)
),
]),
undefined
),
]
: [
f.createParameterDeclaration(
undefined,
undefined,
f.createObjectBindingPattern([
f.createBindingElement(
undefined,
undefined,
f.createIdentifier("variables"),
undefined
),
f.createBindingElement(
undefined,
undefined,
f.createIdentifier("signal"),
undefined,
undefined,
verb === "get"
? [
f.createParameterDeclaration(
undefined,
undefined,
f.createObjectBindingPattern([
f.createBindingElement(
undefined,
undefined,
f.createIdentifier("signal"),
undefined
),
]),
undefined,
f.createTypeLiteralNode([
f.createPropertySignature(
undefined,
f.createIdentifier("signal"),
f.createToken(ts.SyntaxKind.QuestionToken),
f.createTypeReferenceNode(
f.createIdentifier("AbortSignal"),
undefined
),
]),
undefined,
f.createTypeLiteralNode([
f.createPropertySignature(
undefined,
f.createIdentifier("variables"),
undefined,
variablesType
),
f.createPropertySignature(
undefined,
f.createIdentifier("signal"),
f.createToken(ts.SyntaxKind.QuestionToken),
f.createTypeReferenceNode(
f.createIdentifier("AbortSignal"),
undefined
)
),
]),
undefined
),
],
undefined,
f.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
f.createCallExpression(
f.createIdentifier(operationFetcherFnName),
undefined,
[
f.createObjectLiteralExpression(
[
f.createSpreadAssignment(
f.createIdentifier("variables")
)
),
],
false
]),
undefined
),
f.createIdentifier("signal"),
]
)
: [
f.createParameterDeclaration(
undefined,
undefined,
f.createObjectBindingPattern([
f.createBindingElement(
undefined,
undefined,
f.createIdentifier("variables"),
undefined
),
f.createBindingElement(
undefined,
undefined,
f.createIdentifier("signal"),
undefined
),
]),
undefined,
f.createTypeLiteralNode([
f.createPropertySignature(
undefined,
f.createIdentifier("variables"),
undefined,
variablesType
),
f.createPropertySignature(
undefined,
f.createIdentifier("signal"),
f.createToken(ts.SyntaxKind.QuestionToken),
f.createTypeReferenceNode(
f.createIdentifier("AbortSignal"),
undefined
)
),
]),
undefined
),
],
undefined,
f.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
f.createCallExpression(
f.createIdentifier(operationFetcherFnName),
undefined,
[
f.createIdentifier("variables"),
f.createIdentifier("signal"),
]
)
)
),
],
true
Expand Down
Loading

0 comments on commit b97044f

Please sign in to comment.