- Get Started
 - Framework
 
- Get Started
 - Framework
 
6.4.3. Query
In this chapter, you’ll learn about the Query utility and how to use it to fetch data from modules.
What is Query?#
Query fetches data across modules. It’s a set of methods registered in the Local Protocol container under the query key.
In your resources, such as API routes or workflows, you can resolve Query to fetch data across custom modules and Local Protocol’s commerce modules.
Query Example#
For example, create the route src/api/query/route.ts with the following content:
7} from "@localprotojs/framework/utils"8 9export const GET = async (10 req: LocalProtoRequest,11 res: LocalProtoResponse12) => {13 const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)14 15 const { data: myCustoms } = await query.graph({16 entity: "my_custom",17 fields: ["id", "name"],18 })19 20 res.json({ my_customs: myCustoms })21}
In the above example, you resolve Query from the Local Protocol container using the ContainerRegistrationKeys.QUERY (query) key.
Then, you run a query using its graph method. This method accepts as a parameter an object with the following required properties:
entity: The data model's name, as specified in the first parameter of themodel.definemethod used for the data model's definition.fields: An array of the data model’s properties to retrieve in the result.
The method returns an object that has a data property, which holds an array of the retrieved data. For example:
Querying the Graph#
When you use the query.graph method, you're running a query through an internal graph that the Local Protocol application creates.
This graph collects data models of all modules in your application, including commerce and custom modules, and identifies relations and links between them.
Retrieve Linked Records#
Retrieve the records of a linked data model by passing in fields the data model's name suffixed with .*.
For example:
Retrieve List Link Records#
If the linked data model has isList enabled in the link definition, pass in fields the data model's plural name suffixed with .*.
For example:
Apply Filters#
The query.graph function accepts a filters property. You can use this property to filter retrieved records.
In the example above, you filter the my_custom records by multiple IDs.
Sort Records#
The graph method's object parameter accepts a pagination property to configure the pagination of retrieved records.
To sort returned records, pass an order property to pagination.
The order property is an object whose keys are property names, and values are either:
ASCto sort records by that property in ascending order.DESCto sort records by that property in descending order.
Apply Pagination#
To paginate the returned records, pass the following properties to pagination:
skip: (required to apply pagination) The number of records to skip before fetching the results.take: The number of records to fetch.
When you provide the pagination fields, the query.graph method's returned object has a metadata property. Its value is an object having the following properties:
skipnumbertakenumbercountnumber