How does @indices directive work?

In the previous posts, we have seen @skip and @include directives for conditional exclusion and inclusion respectively.

Now we will check the function of @indices directive.

  • If you query the data using a field other than hypi.id, the query execution will get slower as the amount of data grows. So, use an index to execute a query in a performant way.
  • If you query with get or find function using hypi.id of an object, then you need not use indices directive. However, if you are using an arcql filter with any field other than hypi.id, you should create an index on the field.
  • You can create one or more indices on one or more fields.

Declare below Data type in the schema.

type DirectiveIndex @indices (sets: [  
  #hypi:idx:name: index_field1
  ["field1"],
  #hypi:idx:name: index_field1_field2
  ["field1","field2"]  
]){
    field1: String
    field2: Int
}
  • In the DirectiveIndex data type, [“field1”] and ["field1","field2"] are the indices. Use sets: argument to declare the indices.
  • #hypi:idx:name: is a special comment to give the indices suitable names. ( index_field1, index_field1_field2 in this example)
  • There is a limit of 256 characters for an index’s name and it is completely optional to provide it. If you have lots of fields, do provide an index name for clarity.
  • You may query the data using the find function. The query will be the same and the performance will also be the same as data grows.
 {
  find(type: DirectiveIndex, arcql: "field1='abc' AND field2 = 1") {
    edges {
      node {
        ... on DirectiveIndex {
          field1
          field2
        }
      }
      cursor
    }
  }
}