This repository has been archived by the owner on Aug 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Advance Query
william chang(張仲威) edited this page Mar 26, 2018
·
1 revision
{
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!"
}
}
}
const query = gql`
query getBooks {
books @rtdbQuery(ref: "/books", type: "Books") @array {
id @key
name
reviews @array {
id @key
content
}
}
}
`;
[{
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!"
}]
}]
const query = gql`
query getBooks {
books @rtdbQuery(ref: "/books", type: "Books") @array {
id @key
name
authors @array {
id @key
}
}
}
`;
[{
id: "0131101633",
name: "The C Programming Language",
authors: [{
id: "dritchie"
}]
}, {
id: "0134190440",
name: "The Go Programming Language",
authors: [{
id: "dritchie"
}, {
id: "wkernighan"
}]
}]
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
}
}
}
`;
[{
id: "dritchie",
name: "dritchie",
birth: 1941,
comments: [{
id: "id1",
content: "comment!"
}]
}, {
id: "wkernighan",
name: "wkernighan",
birth: 1942,
comments: [{
id: "id2",
content: "comment!"
}]
}]
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}`
}
[{
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
}
}]
}]