You can configure certain RPC aspects within your next.config.js
file
inside the blitz property.
To change how every file path is resolved, you can set the resolverPath
option inside next.config.js
.
module.exports = withBlitz({
blitz: {
resolverPath: "queries|mutations",
},
})
// Or with a custom function
module.exports = withBlitz({
blitz: {
resolverPath: (filePath) => {
return filePath.replace("app/", "") // Removes `app/` from the path
},
},
})
There are three options to determine how the RPC path is resolved during build.
queries|mutations
(default)queries
or mutations
folder as the rootroot
Example output:
File: src/products/queries/getProduct.ts
"queries|mutations" URL: /api/rpc/getProduct
"root" URL: src/products/queries/getProduct
File: src/products/mutations/createProduct.ts
"queries|mutations" URL: /api/rpc/createProduct
"root" URL: src/products/mutations/createProduct
File: src/products/mutations/v2/createProduct.ts
"queries|mutations" URL: /api/rpc/v2/createProduct
"root" URL: src/products/mutations/v2/createProduct
Inside the blitz
property add the param includeRPCFolders
as an array
of relative path folders from your Blitz root folder. For example:
module.exports = withBlitz({
blitz: {
includeRPCFolders: ["../../<YOUR DIRECTORY PATH>"],
},
})
Your resolvers still need to be placed in queries
or mutations
folders.
In your [[...blitz]].ts
api file you can see the following settings in
the rpcHandler
logging?: {
/**
* allowList Represents the list of routes for which logging should be enabled
* If whiteList is defined then only those routes will be logged
*/
allowList?: string[]
/**
* blockList Represents the list of routes for which logging should be disabled
* If blockList is defined then all routes except those will be logged
*/
blockList?: string[]
/**
* verbose Represents the flag to enable/disable logging
* If verbose is true then Blitz RPC will log the input and output of each resolver
*/
verbose?: boolean
/**
* disablelevel Represents the flag to enable/disable logging for a particular level
*/
disablelevel?: "debug" | "info"
}
Blitz RPC defaults to:
verbose
to be true if it not configured. input
and resolver completion time
will be logged with the
info
level. result
and next.js serialization time
will be
logged with the debug
level.rpcAppHandler
:This function acts as the handler for all the queries and mutations in the next.js app directory.
options
(optional): An object with the following properties:onInvokeError
(optional): A function that will be called when an
error occurs during the invocation of a resolver.formatError
(optional): A function that will be called to format the
error before sending it to the client.logging
(optional): An object with the following properties:allowList
(optional): An array of resolvers that should be logged.blockList
(optional): An array of resolvers that should not be
logged.verbose
(optional): A boolean that determines whether verbose
logging should be enabled.disablelevel
(optional): A string that determines which level of
logging should be disabled.An object with the handlers that handle the GET, POST, and HEAD requests.
// app/api/rpc/[[...blitz]]/route.ts
export default api(
rpcAppHandler({
onError: console.log,
formatError: (error) => {
error.message = `FormatError handler: ${error.message}`
return error
},
logging: {
verbose: true,
blockList: ["getCurrentUser", ...]
},
})
)
rpcHandler
:This function acts as the handler for all the queries and mutations in the next.js pages directory.
options
(optional): An object with the following properties:onInvokeError
(optional): A function that will be called when an
error occurs during the invocation of a resolver.formatError
(optional): A function that will be called to format the
error before sending it to the client.logging
(optional): An object with the following properties:allowList
(optional): An array of resolvers that should be logged.blockList
(optional): An array of resolvers that should not be
logged.verbose
(optional): A boolean that determines whether verbose
logging should be enabled.disablelevel
(optional): A string that determines which level of
logging should be disabled.A function that returns the handler for the RPC API.
// pages router
// src/pages/api/rpc/[[...blitz]].ts
export default api(
rpcHandler({
onError: console.log,
formatError: (error) => {
error.message = `FormatError handler: ${error.message}`
return error
},
logging: {
verbose: true,
blockList: ["getCurrentUser", ...], //just write the resolver name [which is the resolver file name]
},
})
)
This is will enable verbose Blitz RPC logging for all resolvers except the
resolvers getCurrentUser
and others mentioned in the blockList