-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: add folderByPath GraphQL resolver #7707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.