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

Advance Query

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

Advanced Query

Data in firebase

{
  users: {
    dritchie: {
      name: "dritchie",
      birth: 1941
    },
    wkernighan: {
      name: "wkernighan",
      birth: 1942
    }
  },
  books: {
    0131101633: {
      name: "The C Programming Language",
      authors: {
        dritchie: true,
        wkernighan: true
      },
      reviews: {
        id1: {content: "good book!"},
        id2: {content: "still good book!"}
      }
    },
    0134190440: {
      name: "The Go Programming Language",
      authors: {
        wkernighan: true
      },
      reviews: {
        id1: {content: "love go"},
        id2: {content: "love go too!"}
      }
    }
  },
  comments: {
    id1: {
      author: "dritchie",
      content: "comment!"
    },
    id2: {
      author: "wkernighan",
      content: "comment!"
    }
  }
}

Query Examples

Query nested array

const query = gql`
  query getBooks {
    books @rtdbQuery(ref: "/books", type: "Books") @array {
      id @key
      name
      reviews @array {
        id @key
        content
      }
    }
  }
`;

Response

[{
  id: "0131101633",
  name: "The C Programming Language",
  reviews: [{
    id: "id1",
    content: "good book!"
  }, {
    id: "id2",
    content: "still good book!"
  }]
}, {
  id: "0134190440",
  name: "The Go Programming Language",
  reviews: [{
    id: "id1",
    content: "love go"
  }, {
    id: "id2",
    content: "love go too!"
  }]
}]

Query nested array-of-scalar-value

const query = gql`
  query getBooks {
    books @rtdbQuery(ref: "/books", type: "Books") @array {
      id @key
      name
      authors @array {
        id @key
      }
    }
  }
`;

Response

[{
  id: "0131101633",
  name: "The C Programming Language",
  authors: [{
    id: "dritchie"
  }]
}, {
  id: "0134190440",
  name: "The Go Programming Language",
  authors: [{
    id: "dritchie"
  }, {
    id: "wkernighan"
  }]
}]

Query with export

The export directive re-exposes a field for use in a later (nested) query. An example use-case would be getting a list of users, and hitting a different path to fetch more data using the exported field in the query args.

In the following query, we export id first, then assign it to equalTo argument using $export$id in nested query.

const query = gql`
  query getUserWithComments {
    users @rtdbQuery(ref: "/users", type: "Users") @array {
      id @key @export
      name
      birth
      comments @rtdbQuery(ref: "/comments", orderByChild: "author", equalTo: "$export$id", type: "Comment") @array
      {
        id @key
        content
      } 
    }
  }
`;

Response

[{
  id: "dritchie",
  name: "dritchie",
  birth: 1941,
  comments: [{
    id: "id1",
    content: "comment!"
  }]
}, {
  id: "wkernighan",
  name: "wkernighan",
  birth: 1942,
  comments: [{
    id: "id2",
    content: "comment!"
  }]
}]

Query with export and customizor function

Sometimes, you need more than $export$id. Say you need to concatenate strings, so you can point to /users/${id}. With custimizor function, you can return whatever you want with exported variables passed in.

For example, we want to fetch authors' data in each article.

const query = gql`
  query getBooksWithAuthors($authorRef: any) {
    books @rtdbQuery(ref: "/books", type: "Books") @array {
      id @key
      name
      authors @array {
        id @key @export
        data @rtdbQuery(ref: $authorRef, type: "Users") {
          name
          birth
        }
      }
    }
  }
`;

// variables
{
  authorRef: ({exportVal}) => `/authors/${exportVal.id}`
}

Response

[{
  id: "0131101633",
  name: "The C Programming Language",
  authors: [{
    id: "dritchie",
    data: {
      name: "dritchie",
      birth: 1941
    }
  }]
}, {
  id: "0134190440",
  name: "The Go Programming Language",
  authors: [{
    id: "dritchie",
    data: {
      name: "dritchie",
      birth: 1941
    }
  }, {
    id: "wkernighan",
    data: {
      name: "wkernighan",
      birth: 1942
    }
  }]
}]