Skip to content
This repository has been archived by the owner on Aug 14, 2019. It is now read-only.

Simple Query Example

william chang(張仲威) edited this page Mar 26, 2018 · 1 revision

Simple Query Example

Data in Firebase

{
  users: {
    id1: {
      name: "alovelace",
      birth: 1815
    },
    id2: {
      name: "ghopper",
      birth: 1906
    }
  }
}

Query Examples

Query all in array

const query = gql`
  query getUsers {
    users @rtdbQuery(ref: "/users", type: "Users") @array {
      id @key
      name
      birth
    }
  }
`;

Response

[{
  __typename: "Users",
  id: "id1",
  name: "alovelace",
  birth: 1815
}, {
  __typename: "Users",
  id: "id2",
  name: "ghopper",
  birth: 1906
}]

Query with orderByChild & limitToFirst

const query = gql`
  query getUsers {
    users @rtdbQuery(ref: "/users", orderByChild: "birth", limitToFirst: 1, type: "Users") @array {
      id @key
      name
      birth
    }
  }
`;

Response

[{
  __typename: "Users",
  id: "id1",
  name: "alovelace",
  birth: 1815
}]

Query with orderByKey

const query = gql`
  query getUsers {
    users @rtdbQuery(ref: "/users", orderByKey: true, type: "Users") @array {
      id @key
      name
      birth
    }
  }
`;

Response

[{
  __typename: "Users",
  id: "id1",
  name: "alovelace",
  birth: 1815
}, {
  __typename: "Users",
  id: "id2",
  name: "ghopper",
  birth: 1906
}]