Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Created December 21, 2016 14:06
Show Gist options
  • Select an option

  • Save simenbrekken/0356ea0edaef4724f0ca25a162e4eb4e to your computer and use it in GitHub Desktop.

Select an option

Save simenbrekken/0356ea0edaef4724f0ca25a162e4eb4e to your computer and use it in GitHub Desktop.

Revisions

  1. simenbrekken created this gist Dec 21, 2016.
    62 changes: 62 additions & 0 deletions schema.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    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() {
    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 })