• Stars
    star
    413
  • Rank 104,136 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”₯ πŸ”— apollo-link-firebase provides you a simple way to use Firebase with graphQL.

npm version CircleCI gitter

Apollo-link-firebase

apollo-link-firebase provides you a simple way to query Firebase in graphQL with Apollo-client without building a graphQL server

Currently, we support features below:

  1. Query: All sorting and filtering methods on document are supported.
  2. Mutation: deal with set, update, remove methods with graphQL mutation.
  3. Realtime Subscription: Listen to your Firebase events using graphQL Subscription.
  4. Data Join: Retrieve your data from different paths using one graphQL.

Contents

Installation

yarn add apollo-link-firebase

Quickstart

// rtdb stands for realtime database
import {createRtdbLink} from 'apollo-link-firebase';
import * as firebase from 'firebase';

// initialize firebase
firebase.initializeApp({
  // configs
});

// create Realtime Database link
const rtdbLink = createRtdbLink({
  database: firebase.database()
});

const client = new ApolloClient({
  link: rtdbLink,
  cache: new InMemoryCache(),
});

// A simple query to retrieve data from 
// firebase.database().ref("/profile/me")
// @rtdbQuery stands for Realtime Database Query
const query = gql`
  query myProfile {
    me @rtdbQuery(ref: "/profile/me") {
      name
    }
  }
`;

// Invoke the query and log the person's name
client.query({ query }).then(response => {
  console.log(response.data.name);
});

Retrieve Object Data

Return with __typename

In Apollo client, InMemoryCache use __typename and id to save your data in store.

Using @key directive, you can speficy which field you want to return with the key of snapshot

const query = gql`
  query myProfile {
    me @rtdbQuery(ref: "/profile/me", type: "Profile") {
      id @key
      name
    }
  }
`;

Response

{
  __typename: "Profile",
  id: "me",
  name: "wwwy3y3"
}

Work with Lists of Data

For example, your data in Firebase could be like

{
  users: {
    id1: {
      name: "alovelace",
      birth: 1815
    },
    id2: {
      name: "ghopper",
      birth: 1906
    }
  }
}

Basic Query

We can query all users, and use @array directive to parse data to array

const query = gql`
  query getUsers {
    users @rtdbQuery(ref: "/users", type: "Users") @array {
      id @key
      name
    }
  }
`;

Response

[{
  __typename: "Users",
  id: "id1",
  name: "alovelace",
  birth: 1815
}, {
  __typename: "Users",
  id: "id2",
  name: "ghopper",
  birth: 1906
}]

Advance Query

In firebase client js sdk, We can get data by using sorting and filtering API

We provide corresponding API in @rtdbQuery directive arguments. In the following example, we query lists of data using orderByChild("birth") and limitToFirst(1)

const query = gql`
  query getUsers {
    users @rtdbQuery(ref: "/users", orderByChild: "birth", limitToFirst: 1, type: "Users") {
      name
    }
  }
`;

Response

[{
  __typename: "Users",
  id: "id1",
  name: "alovelace",
  birth: 1815
}]

rtdbQuery Directive Arguments

  • ref: string
  • orderByChild: string
  • orderByKey: boolean. e,g orderByKey: true
  • orderByValue: boolean
  • startAt: any
  • endAt: any
  • equalTo: any
  • limitToFirst: number
  • limitToLast: number

More Examples

Mutation

We only take payload from input key from the recommendations in this article

const mutation = gql`
  fragment Input on firebase {
    string: String
    number: Number
  }

  mutation($ref: string, $input: Input!) {
    updateArticle(input: $input) @rtdbUpdate(ref: $ref, type: "Article") {
      id @key
      string
      number
    }
  }
`;

We support four directives for mutation

  • @rtdbUpdate: Firebase update
  • @rtdbSet: Firebase set
  • @rtdbRemove: Firebase remove
  • @rtdbPush: Push new element under ref, sugar api for firebase push and set

Examples

@rtdbRemove

const mutation = gql`
  mutation($ref: string) {
    remove @rtdbRemove(ref: $ref)
  }
`;

@rtdbPush and @pushKey

const mutation = gql`
  fragment ProfileInput on firebase {
    string: String
    number: Number
  }

  mutation($ref: string, $input: ProfileInput!) {
    pushdata(input: $input) @rtdbPush(ref: $ref) {
      id @pushKey
      string
      number
    }
  }
`;

// variables
const variables = {
  ref: "/users",
  input: {
    string: "string",
    number: 1
  }
}

// response
{
  id: "-KjCIvxsKueb3Hf2LIOp",
  string: "string",
  number: 1
}

Subscription

We support four events in Firebase, more on Firebase document

You can use all the query api supported in @rtdbQuery, more advanced query examples in wiki

Usage

const subQuery = gql`
  subscription {
    newComment @rtdbSub(ref: "/comments", event: "value") {
      id @key
      content
    }
  }
`;

Directives

  • value: @rtdbSub(ref: string, event: "value")
  • child_added: @rtdbSub(ref: string, event: "child_added")
  • child_changed: @rtdbSub(ref: string, event: "child_changed")
  • child_removed: @rtdbSub(ref: string, event: "child_removed")

Example

Simple Todo React Application

Here's a simple React + apollo-link-firebase todo app

Roadmap

  • support firebase transaction
  • support firestore
  • support authenticate mutation
  • support storage mutation

Contribution

Contributions are welcome and extremely helpful πŸ™Œ

Feel free to join our gitter channel to discuss with us!

More Repositories

1

cannercms

⚑️ Content Management Framework creates custom CMS fast and easy. Support data sources such as Firebase/Firestore, GraphQL and Restful APIs.
JavaScript
2,430
star
2

WrenAI

Wren AI makes your database RAG-ready. Implement Text-to-SQL more accurately and securely.
TypeScript
1,409
star
3

canner-slate-editor

πŸ“Rich Text / WYSIWYG Editor built for Modularity and Extensibility.
JavaScript
1,127
star
4

vulcan-sql

Data API Framework for AI Agents and Data Apps
TypeScript
610
star
5

gqlify

[NOT MAINTAINED]An API integration framework using GraphQL
TypeScript
178
star
6

canner-firebase-cms

πŸ”₯ Content management for Firebase Realtime Database with SSR supported with NextJS, for blog, ecommerce, mobile apps and even chatbot! This project is based on Canner CMS
JavaScript
119
star
7

wren-engine

Wren Engine is the backbone of the Wren AI project - The semantic engine for LLMs, bringing business context to AI agents.
Java
97
star
8

graphql-rbac

GraphQL Role-based access control (RBAC) middleware
TypeScript
36
star
9

canner-firestore-cms

The example of building the CMS with Canner and Firestore
JavaScript
25
star
10

graphql-i18n

An i18n middleware for GraphQL
TypeScript
22
star
11

cms30

πŸ™Œ #CMS30 | Free 30 CMS templates
JavaScript
22
star
12

vulcan-sql-examples

Curated VulcanSQL show cases
Jupyter Notebook
18
star
13

react-loading-image

A react image loader.
JavaScript
13
star
14

canner-demo

😻 Collection of demos to demonstrate how Canner can accomplish in different use cases.
CSS
13
star
15

antd-geosuggest

Antd based geo suggestion for Google maps places API
JavaScript
11
star
16

typescript-koa-boilerplate

typescript-koa-boilerplate
HCL
11
star
17

image-upload

Based on antd image uploader
JavaScript
9
star
18

emoji-mart-picker

emoji popup picker
JavaScript
7
star
19

create-table-picker

Table creater based on react-hovertable
JavaScript
7
star
20

grid-draggable

grid draggable system
JavaScript
4
star
21

antd-canner-components

🀑Modular Ant design based CMS components for Canner, over 40+ components
JavaScript
4
star
22

canner-udf-example

Canner UDF example
Java
3
star
23

generator-canner-react

A yeoman generator for canner react projects
JavaScript
2
star
24

npm-check-install

Check whether a npm module is installed, otherwise installed it.
JavaScript
2
star
25

image-gallery

A gallery for photos.
JavaScript
2
star
26

docs.canner.io

Canner official documentation
JavaScript
1
star
27

react-window-detect-dimensions

Wrapper component that detects window resize and pass down to children
JavaScript
1
star
28

get-selection-range

Get current cursor's selection range
JavaScript
1
star
29

gatsby-plugin-canner-schema

Gatsby plugin for using Canner schema
JavaScript
1
star
30

docs.gqlify.com

Document of gqlify
JavaScript
1
star
31

image-service-config

Help you setup configurations in ant design uploader, support imgur, Firebase client, Firebase admin
JavaScript
1
star
32

canner-quick-start

The simplest example of building a CMS with Canner
JavaScript
1
star
33

grid-breakpoint

Automatically add breakpoints to your grids
JavaScript
1
star
34

antd-cms-component-docs

🐜 Documentation for antd-cms-component
JavaScript
1
star
35

victory-canner-components

A chart library for Canner, based on Victory Chart
JavaScript
1
star