Skip to content

Commit a699dc0

Browse files
author
Sam Jackson
committed
Merge branch 'master' of github.com:sejr/firegraph
* 'master' of github.com:sejr/firegraph: doc: minor note regarding references docs: better examples in readme
2 parents 258aba4 + d689a93 commit a699dc0

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

README.md

+45-32
Original file line numberDiff line numberDiff line change
@@ -35,52 +35,65 @@ yarn add graphql graphql-tag firegraph
3535

3636
## Usage
3737

38-
Right now, `firegraph` has somewhat limited functionality but it can still be very useful when you want a quick way to retrieve values for various collections with arbitrary levels of nesting.
38+
**You do not need to host a GraphQL server to use Firegraph.** Your project does require the above dependencies (`firegraph`, `graphql`, and `graphql-tag`, however. You can either write queries inside your JavaScript files with `gql`, or if you use webpack, you can use `graphql-tag/loader` to import GraphQL query files (`*.graphql`) directly.
39+
40+
### Retrieving a Collection
3941

4042
``` typescript
41-
import gql from 'graphql-tag';
42-
import firegraph from 'firegraph';
43-
import { firestore } from '../path/to/my/firebase';
43+
const { posts } = await firegraph.resolve(firestore, gql`
44+
query {
45+
posts {
46+
id
47+
title
48+
body
49+
}
50+
}
51+
`)
52+
```
53+
### Retrieving Nested Collections
4454

45-
const userQuery = gql`
55+
``` typescript
56+
const { posts: postsWithComments } = await firegraph.resolve(firestore, gql`
4657
query {
47-
users {
58+
posts {
4859
id
49-
hometown
50-
fullName
51-
birthdate
52-
favoriteColor
53-
posts {
60+
title
61+
body
62+
comments {
5463
id
55-
message
64+
body
5665
}
5766
}
5867
}
59-
`;
60-
61-
firegraph.resolve(firestore, userQuery).then(result => {
62-
for (let user of result.users) {
63-
console.log(user);
64-
}
65-
});
68+
`)
6669
```
6770

68-
The console log in that for loop at the end would produce something like:
71+
### Retrieving Collections with References
72+
73+
Right now, we are assuming that `post.author` is a string that matches the ID of some document in the `users` collection. In the future we will leverage Firestore's `DocumentReference` value type to handle both use cases.
6974

7075
``` typescript
71-
{
72-
id: 'sZOgUC33ijsGSzX17ybT',
73-
hometown: GeoPoint { _lat: 40.141832766, _long: -84.242165698 },
74-
fullName: { family: 'Doe', given: 'John', middle: null },
75-
birthdate: Timestamp { seconds: 747763200, nanoseconds: 0 },
76-
favoriteColor: 'blue',
77-
posts: [
78-
{
79-
id: 'i4CWhNXr8qPqaG3KLrZk',
80-
message: 'Hello world!'
76+
const { posts: postsWithAuthorAndComments } = await firegraph.resolve(firestore, gql`
77+
query {
78+
posts {
79+
id
80+
title
81+
body
82+
author(fromCollection: "users") {
83+
id
84+
displayName
85+
}
86+
comments {
87+
id
88+
body
89+
author(fromCollection: "users") {
90+
id
91+
displayName
92+
}
93+
}
8194
}
82-
]
83-
}
95+
}
96+
`)
8497
```
8598

8699
# Roadmap

0 commit comments

Comments
 (0)