feat: add folderByPath GraphQL resolver#7707
feat: add folderByPath GraphQL resolver#7707dylan-hart wants to merge 3 commits intorequarks:mainfrom
Conversation
server/graph/resolvers/asset.js
Outdated
| for (const slug of parts) { | ||
| const folder = await WIKI.models.assetFolders.query().where({ | ||
| parentId: parentId, | ||
| slug: slug | ||
| }).first() |
There was a problem hiding this comment.
Avoid querying the DB on every path segment. There's already a method to get the hierarchy in 1 query:
const folderHierarchy = await WIKI.models.assetFolders.getHierarchy(args.folderId)
Your code should either use it or do something similar.
There was a problem hiding this comment.
Thanks! Would a single assetFolders.query() be acceptable? I'd like to use getHierarchy(), but it requires a folderId.
There was a problem hiding this comment.
You're fetching all folders into memory, which isn't better. See how the getHierarchy() method works. It makes a single query using the withRecursive() method, fetching only the necessary data.
There was a problem hiding this comment.
Refactored to use recursive SQL similar to getHierarchy(), starting from root and iterating downward instead of up from child/leaf. Thanks for your suggestion. Tested with PostgreSQL 15 and SQL Server 2022 (16).
Adds a new folderByPath(path: String!): AssetFolder query to the AssetQuery type in the GraphQL API. It allows clients to resolve a folder's ID and metadata by providing its hierarchical path (e.g. "uploads/images/2025"), which simplifies integration scenarios where asset folder IDs are not known in advance.
Specifically, our use case has been taking Microsoft Word documents and PDFs, converting them to Markdown, and using the GraphQL API to rapidly upload many pages and assets. Because we are inferring/generating page paths at runtime, we don't know the asset folder IDs unless we connect to the PostgreSQL database or observe network packets through the browser web dev tools.