Created
February 1, 2019 17:28
-
-
Save sejr/3647b95e8f49355a8ae068c50d779379 to your computer and use it in GitHub Desktop.
Firegraph comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function getPosts(): Promise<PostListItem[]> { | |
const { posts } = await firegraph.resolve(firestore, gql` | |
query { | |
posts { | |
id | |
body | |
title | |
author(fromCollection: "users") { | |
id | |
displayName | |
photoURL | |
} | |
} | |
} | |
`); | |
return posts; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function getPosts(): Promise<PostListItem[]> { | |
const posts: any[] = []; | |
// Fetch from `posts` collection. | |
let postSnapshot = await firestore.collection('posts').get(); | |
// For each post, we need to retrieve author info. | |
for (let post of postSnapshot.docs) { | |
const { id } = post; | |
const { title, body, author } = post.data(); | |
const authorSnapshot = await firestore.doc(`users/${author}`).get(); | |
const { displayName, photoURL } = authorSnapshot.data()!; | |
postAuthor = { | |
id: authorSnapshot.id, | |
displayName | |
photoURL | |
} | |
// Add the post to our list | |
posts.push({ | |
id, | |
body, | |
title, | |
author: postAuthor | |
}); | |
} | |
return posts as PostListItem[]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment