Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL query to access first item in an array?

Tags:

graphql

Had a good search, but can't seem to find anything for this. Is there a way in GraphQL to only access the first item in an array?

Something like:

query {
  allDBItems {
    edges {
      node {
        exampleArray([0])
      }
    }
  }
}
like image 815
Joss Classey Avatar asked Apr 24 '19 09:04

Joss Classey


People also ask

Can GraphQL query return array?

GraphQL provides a query language to define the shape of data you'd like returned from an HTTP API on a server and a library to help make it happen. It's easy to return a single item or multiple items.

What is __ Typename in GraphQL?

The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).


1 Answers

No, GraphQL does not provide any syntax for manipulating the request, outside of conditionally including/excluding fields using the @skip and @include directives.

The GraphQL service you are querying may support a way to limit the result set for a field to a specific length by providing an argument on the field like limit, first or last. However, it's up to the server to include these fields as part of the service's schema and to provide the logic to implement them. Check your API's documentation to see if these fields are supported.

Any manipulations of the response have to be done client-side. There is an experimental graphql-lodash library that lets you do this right inside your queries -- but at the end of the day, the data is still being transformed client-side.

like image 170
Daniel Rearden Avatar answered Oct 09 '22 22:10

Daniel Rearden