-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
63 lines (52 loc) · 1.51 KB
/
schema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import firebase from 'firebase'
import { filter, map } from 'lodash'
import { makeExecutableSchema } from 'graphql-tools'
firebase.initializeApp({
databaseURL: 'https://grafire-b1b6e.firebaseio.com',
})
const mapSnapshotToEntity = snapshot => ({ id: snapshot.key, ...snapshot.val() })
const mapSnapshotToEntities = snapshot => map(snapshot.val(), (value, id) => ({ id, ...value }))
const ref = path => firebase.database().ref(path)
const getValue = path => ref(path).once('value')
const getEntity = path => getValue(path).then(mapSnapshotToEntity)
const getEntities = path => getValue(path).then(mapSnapshotToEntities)
// http://dev.apollodata.com/tools/graphql-tools/resolvers.html
const resolvers = {
Author: {
posts(author) {
return getEntities('posts').then(posts => filter(posts, { authorId: author.id }))
},
},
Post: {
author(post) {
return getEntities('authors').then(posts => filter(authors, { id: authorId }))
},
},
Query: {
posts() {
return getEntities('posts')
},
authors() {
// ... so a resolver can return a promise that resolves to an array, or an array of promises, and both are handled correctly.
return getEntities('authors')
}
},
}
const schema = `
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post]
}
type Post {
id: Int!
title: String
author: Author
}
type Query {
posts: [Post]
authors: [Author]
}
`
export default makeExecutableSchema({ typeDefs: schema, resolvers })