Reduce duplication when creating frontend queries and actions (#633)

* Make waspc/todoApp return tasks in consistent order

(cherry picked from commit 57606c74e4)

* Reduce duplication in frontend actions and queries

* Fix formatting

* Update e2e tests to include queries/core
This commit is contained in:
Filip Sodić 2022-06-17 14:35:01 +02:00 committed by GitHub
parent 5e5b264448
commit fdd8e47b25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 198 additions and 33 deletions

View File

@ -1,15 +1,8 @@
{{={= =}=}}
import { callOperation } from '../operations'
import * as resources from '../operations/resources'
async function {= actionFnName =}(args) {
const actionResult = await callOperation('{= actionRoute =}', args)
await resources.invalidateQueriesUsing(entitiesUsed)
return actionResult
}
export const entitiesUsed = {=& entitiesArray =}
export default {= actionFnName =}
import { createAction } from './core'
export default createAction(
'{= actionRoute =}',
{=& entitiesArray =},
)

View File

@ -0,0 +1,10 @@
import { callOperation } from '../operations'
import { invalidateQueriesUsing } from '../operations/resources'
export function createAction(actionRoute, entitiesUsed) {
return async function action(args) {
const actionResult = await callOperation(actionRoute, args)
await invalidateQueriesUsing(entitiesUsed)
return actionResult
}
}

View File

@ -1,16 +1,7 @@
{{={= =}=}}
import { callOperation } from '../operations'
import * as resources from '../operations/resources'
function {= queryFnName =}(args) {
return callOperation('{= queryRoute =}', args)
}
const queryCacheKey = '{= queryRoute =}'
{= queryFnName =}.queryCacheKey = queryCacheKey
const entitiesUsed = {=& entitiesArray =}
resources.addResourcesUsedByQuery(queryCacheKey, entitiesUsed)
export default {= queryFnName =}
import { createQuery } from './core'
export default createQuery(
'{= queryRoute =}',
{=& entitiesArray =},
)

View File

@ -0,0 +1,13 @@
import { callOperation } from '../operations'
import { addResourcesUsedByQuery } from '../operations/resources'
export function createQuery(queryRoute, entitiesUsed) {
function query(args) {
return callOperation(queryRoute, args)
}
query.queryCacheKey = queryRoute
addResourcesUsedByQuery(query.queryCacheKey, entitiesUsed)
return query
}

View File

@ -33,6 +33,7 @@ waspBuild/.wasp/build/web-app/package.json
waspBuild/.wasp/build/web-app/public/favicon.ico
waspBuild/.wasp/build/web-app/public/index.html
waspBuild/.wasp/build/web-app/public/manifest.json
waspBuild/.wasp/build/web-app/src/actions/core.js
waspBuild/.wasp/build/web-app/src/api.js
waspBuild/.wasp/build/web-app/src/config.js
waspBuild/.wasp/build/web-app/src/ext-src/Main.css
@ -43,6 +44,7 @@ waspBuild/.wasp/build/web-app/src/index.js
waspBuild/.wasp/build/web-app/src/logo.png
waspBuild/.wasp/build/web-app/src/operations/index.js
waspBuild/.wasp/build/web-app/src/operations/resources.js
waspBuild/.wasp/build/web-app/src/queries/core.js
waspBuild/.wasp/build/web-app/src/queries/index.js
waspBuild/.wasp/build/web-app/src/queryClient.js
waspBuild/.wasp/build/web-app/src/router.js

View File

@ -244,6 +244,13 @@
],
"696886c4dd2bb66df380e2a9ebf07d54fe39b25af968aeea090ed6fb528d402e"
],
[
[
"file",
"web-app/src/actions/core.js"
],
"0891b7607b5503ada42386f73476a46f774a224d8b606f79ef37e451b229d399"
],
[
[
"file",
@ -314,6 +321,13 @@
],
"9bccf509e1a3e04e24444eacd69e0f093e590a9b85325bedb6f1d52ac85ead3c"
],
[
[
"file",
"web-app/src/queries/core.js"
],
"031c6d10898cb43f7f7e581422542585383b262d8a4f4d8738c695cca4b3fca1"
],
[
[
"file",

View File

@ -0,0 +1,10 @@
import { callOperation } from '../operations'
import { invalidateQueriesUsing } from '../operations/resources'
export function createAction(actionRoute, entitiesUsed) {
return async function action(args) {
const actionResult = await callOperation(actionRoute, args)
await invalidateQueriesUsing(entitiesUsed)
return actionResult
}
}

View File

@ -0,0 +1,13 @@
import { callOperation } from '../operations'
import { addResourcesUsedByQuery } from '../operations/resources'
export function createQuery(queryRoute, entitiesUsed) {
function query(args) {
return callOperation(queryRoute, args)
}
query.queryCacheKey = queryRoute
addResourcesUsedByQuery(query.queryCacheKey, entitiesUsed)
return query
}

View File

@ -33,6 +33,7 @@ waspCompile/.wasp/out/web-app/package.json
waspCompile/.wasp/out/web-app/public/favicon.ico
waspCompile/.wasp/out/web-app/public/index.html
waspCompile/.wasp/out/web-app/public/manifest.json
waspCompile/.wasp/out/web-app/src/actions/core.js
waspCompile/.wasp/out/web-app/src/api.js
waspCompile/.wasp/out/web-app/src/config.js
waspCompile/.wasp/out/web-app/src/ext-src/Main.css
@ -43,6 +44,7 @@ waspCompile/.wasp/out/web-app/src/index.js
waspCompile/.wasp/out/web-app/src/logo.png
waspCompile/.wasp/out/web-app/src/operations/index.js
waspCompile/.wasp/out/web-app/src/operations/resources.js
waspCompile/.wasp/out/web-app/src/queries/core.js
waspCompile/.wasp/out/web-app/src/queries/index.js
waspCompile/.wasp/out/web-app/src/queryClient.js
waspCompile/.wasp/out/web-app/src/router.js

View File

@ -244,6 +244,13 @@
],
"434b67f4ee148d6a5c8d88879b616d0af36d71abf193e84ef247e5ab959a13bd"
],
[
[
"file",
"web-app/src/actions/core.js"
],
"0891b7607b5503ada42386f73476a46f774a224d8b606f79ef37e451b229d399"
],
[
[
"file",
@ -314,6 +321,13 @@
],
"9bccf509e1a3e04e24444eacd69e0f093e590a9b85325bedb6f1d52ac85ead3c"
],
[
[
"file",
"web-app/src/queries/core.js"
],
"031c6d10898cb43f7f7e581422542585383b262d8a4f4d8738c695cca4b3fca1"
],
[
[
"file",

View File

@ -0,0 +1,10 @@
import { callOperation } from '../operations'
import { invalidateQueriesUsing } from '../operations/resources'
export function createAction(actionRoute, entitiesUsed) {
return async function action(args) {
const actionResult = await callOperation(actionRoute, args)
await invalidateQueriesUsing(entitiesUsed)
return actionResult
}
}

View File

@ -0,0 +1,13 @@
import { callOperation } from '../operations'
import { addResourcesUsedByQuery } from '../operations/resources'
export function createQuery(queryRoute, entitiesUsed) {
function query(args) {
return callOperation(queryRoute, args)
}
query.queryCacheKey = queryRoute
addResourcesUsedByQuery(query.queryCacheKey, entitiesUsed)
return query
}

View File

@ -35,6 +35,7 @@ waspJob/.wasp/out/web-app/package.json
waspJob/.wasp/out/web-app/public/favicon.ico
waspJob/.wasp/out/web-app/public/index.html
waspJob/.wasp/out/web-app/public/manifest.json
waspJob/.wasp/out/web-app/src/actions/core.js
waspJob/.wasp/out/web-app/src/api.js
waspJob/.wasp/out/web-app/src/config.js
waspJob/.wasp/out/web-app/src/ext-src/Main.css
@ -46,6 +47,7 @@ waspJob/.wasp/out/web-app/src/index.js
waspJob/.wasp/out/web-app/src/logo.png
waspJob/.wasp/out/web-app/src/operations/index.js
waspJob/.wasp/out/web-app/src/operations/resources.js
waspJob/.wasp/out/web-app/src/queries/core.js
waspJob/.wasp/out/web-app/src/queries/index.js
waspJob/.wasp/out/web-app/src/queryClient.js
waspJob/.wasp/out/web-app/src/router.js

View File

@ -258,6 +258,13 @@
],
"f223cbdd7db93d51c3ecf8282a06801449b29bfb6821313c383c1b18ad8c1479"
],
[
[
"file",
"web-app/src/actions/core.js"
],
"0891b7607b5503ada42386f73476a46f774a224d8b606f79ef37e451b229d399"
],
[
[
"file",
@ -335,6 +342,13 @@
],
"9bccf509e1a3e04e24444eacd69e0f093e590a9b85325bedb6f1d52ac85ead3c"
],
[
[
"file",
"web-app/src/queries/core.js"
],
"031c6d10898cb43f7f7e581422542585383b262d8a4f4d8738c695cca4b3fca1"
],
[
[
"file",

View File

@ -0,0 +1,10 @@
import { callOperation } from '../operations'
import { invalidateQueriesUsing } from '../operations/resources'
export function createAction(actionRoute, entitiesUsed) {
return async function action(args) {
const actionResult = await callOperation(actionRoute, args)
await invalidateQueriesUsing(entitiesUsed)
return actionResult
}
}

View File

@ -0,0 +1,13 @@
import { callOperation } from '../operations'
import { addResourcesUsedByQuery } from '../operations/resources'
export function createQuery(queryRoute, entitiesUsed) {
function query(args) {
return callOperation(queryRoute, args)
}
query.queryCacheKey = queryRoute
addResourcesUsedByQuery(query.queryCacheKey, entitiesUsed)
return query
}

View File

@ -38,6 +38,7 @@ waspMigrate/.wasp/out/web-app/package.json
waspMigrate/.wasp/out/web-app/public/favicon.ico
waspMigrate/.wasp/out/web-app/public/index.html
waspMigrate/.wasp/out/web-app/public/manifest.json
waspMigrate/.wasp/out/web-app/src/actions/core.js
waspMigrate/.wasp/out/web-app/src/api.js
waspMigrate/.wasp/out/web-app/src/config.js
waspMigrate/.wasp/out/web-app/src/ext-src/Main.css
@ -48,6 +49,7 @@ waspMigrate/.wasp/out/web-app/src/index.js
waspMigrate/.wasp/out/web-app/src/logo.png
waspMigrate/.wasp/out/web-app/src/operations/index.js
waspMigrate/.wasp/out/web-app/src/operations/resources.js
waspMigrate/.wasp/out/web-app/src/queries/core.js
waspMigrate/.wasp/out/web-app/src/queries/index.js
waspMigrate/.wasp/out/web-app/src/queryClient.js
waspMigrate/.wasp/out/web-app/src/router.js

View File

@ -244,6 +244,13 @@
],
"c91b7ea515a4889c4abfd355500c8260210602093a94c0beee302450272c3737"
],
[
[
"file",
"web-app/src/actions/core.js"
],
"0891b7607b5503ada42386f73476a46f774a224d8b606f79ef37e451b229d399"
],
[
[
"file",
@ -314,6 +321,13 @@
],
"9bccf509e1a3e04e24444eacd69e0f093e590a9b85325bedb6f1d52ac85ead3c"
],
[
[
"file",
"web-app/src/queries/core.js"
],
"031c6d10898cb43f7f7e581422542585383b262d8a4f4d8738c695cca4b3fca1"
],
[
[
"file",

View File

@ -0,0 +1,10 @@
import { callOperation } from '../operations'
import { invalidateQueriesUsing } from '../operations/resources'
export function createAction(actionRoute, entitiesUsed) {
return async function action(args) {
const actionResult = await callOperation(actionRoute, args)
await invalidateQueriesUsing(entitiesUsed)
return actionResult
}
}

View File

@ -0,0 +1,13 @@
import { callOperation } from '../operations'
import { addResourcesUsedByQuery } from '../operations/resources'
export function createQuery(queryRoute, entitiesUsed) {
function query(args) {
return callOperation(queryRoute, args)
}
query.queryCacheKey = queryRoute
addResourcesUsedByQuery(query.queryCacheKey, entitiesUsed)
return query
}

View File

@ -33,13 +33,17 @@ genOperations spec =
<++> Resources.genResources spec
genQueries :: AppSpec -> Generator [FileDraft]
genQueries spec = do
queriesFds <- mapM (genQuery spec) (AS.getQueries spec)
return $ queriesFds ++ [C.mkTmplFd $ C.asTmplFile [relfile|src/queries/index.js|]]
genQueries spec =
mapM (genQuery spec) (AS.getQueries spec)
<++> return
[ C.mkSrcTmplFd [relfile|queries/index.js|],
C.mkSrcTmplFd [relfile|queries/core.js|]
]
genActions :: AppSpec -> Generator [FileDraft]
genActions spec =
mapM (genAction spec) (AS.getActions spec)
<++> return [C.mkSrcTmplFd [relfile|actions/core.js|]]
genQuery :: AppSpec -> (String, AS.Query.Query) -> Generator FileDraft
genQuery _ (queryName, query) = return $ C.mkTmplFdWithDstAndData tmplFile dstFile (Just tmplData)
@ -49,8 +53,7 @@ genQuery _ (queryName, query) = return $ C.mkTmplFdWithDstAndData tmplFile dstFi
dstFile = C.asWebAppFile $ [reldir|src/queries/|] </> fromJust (getOperationDstFileName operation)
tmplData =
object
[ "queryFnName" .= (queryName :: String),
"queryRoute"
[ "queryRoute"
.= ( ServerGenerator.operationsRouteInRootRouter
++ "/"
++ ServerOperationsRoutesG.operationRouteInOperationsRouter operation
@ -67,8 +70,7 @@ genAction _ (actionName, action) = return $ C.mkTmplFdWithDstAndData tmplFile ds
dstFile = C.asWebAppFile $ [reldir|src/actions/|] </> fromJust (getOperationDstFileName operation)
tmplData =
object
[ "actionFnName" .= (actionName :: String),
"actionRoute"
[ "actionRoute"
.= ( ServerGenerator.operationsRouteInRootRouter
++ "/"
++ ServerOperationsRoutesG.operationRouteInOperationsRouter operation