Fetching Records

Fetch records from the API

Fetching data is a fundamental operation when working with APIs. In Noloco's dynamic GraphQL API, this is accomplished using GraphQL Queries. This guide will introduce you to the pattern and arguments used to fetch records with precision and efficiency.

Basic Query Structure

Queries for fetching records are named based on the table you're looking to retrieve data from. The naming convention is <tableName>Collection, where <tableName> represents the name of your table. For instance:

  • To fetch projects, use projectsCollection.

  • To fetch users, use usersCollection.

The edges, node, and pageInfo Pattern

This structure allows you to navigate through your data with ease:

  • edges: This represents the connection between nodes. Within edges, you can query the node to access the fields of that record.

  • pageInfo: Contains metadata about the current page of results, useful for pagination. Fields commonly available include hasNextPage, hasPreviousPage, and startCursor.

Example Query

Let's assume we have the following Client Portal Noloco App, with a Project table

You can fetch all of the projects and their name with the following GraphQL Query:

query {
  projectsCollection {
    edges {
      node {
        id
        uuid
        name
      }
    }
    pageInfo {
      hasNextPage
    }
  }
}

3. Query Arguments

You can refine your data retrieval using the following arguments:

  • first: Limits the number of records returned. For example, first: 5 will return only the first five records.

  • before: Cursor for pagination. Used in combination with pageInfo to retrieve records before a certain point.

  • after: Cursor for pagination. Used in combination with pageInfo to retrieve records after a certain point.

  • orderBy: An object that specifies which field to sort by and the direction of sorting. Example: orderBy: { field: "name", direction: "ASC" }.

  • where: Allows you to filter records based on field values. The structure of the filter object varies depending on the field type. For instance, for a text field, you might use: where: { fieldName: { equals: "desiredValue" } }.

Example Query with Arguments

Here's how you might fetch the first 10 projects, ordered by name:

query {
  projectsCollection(first: 10, orderBy: {field: "name", direction: ASC}) {
    edges {
      node {
        id
        name
      }
    }
  }
}

Conclusion

Noloco's integration with GraphQL provides a powerful and flexible way to fetch and manipulate data. With a solid understanding of the patterns and arguments available, you can tailor your queries to retrieve precisely the data you need. As you become more accustomed to these structures, you'll find it becomes second nature to craft efficient and effective queries.

Last updated