当前位置: 首页 > 软件库 > 大数据 > 数据查询 >

apollo-link-firebase

授权协议 MIT License
开发语言 Java
所属分类 大数据、 数据查询
软件类型 开源软件
地区 不详
投 递 者 费学
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

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!

  • fcm和firebase In a previous post I confessed defeat in attempting to get an AWS Lambda GraphQL server to connect to a Firebase server. I didn’t give up right away, though, and a short time later found

  • 1. 获取源代码 从git上clone [apollo项目] 2. 配置数据库 启动一个mySql数据库 导入 script/sql/ 文件夹下面的apolloconfigdb.sql 和 apolloportaldb.sql 3.打包编译 找到路径 scripts/build.bat(.sh)。windows(linux) 修改数据库连接信息 修改完后运行 4. 准备镜像文件 找到apollo-

  • 前提     apollo被评选为2018年优秀的开源框架,才让我们关注到apollo,优秀的配置中心框架的存在,apollo的具体作用小编在之前的文章【apollo】——分布式配置中心中介绍,本文主要介绍springboot整合apollo,以及在项目中的使用。 组成     在使用过程中apollo主要分为服务端和客户端,具体包含内容如下,本文主要介绍client端的使用,服务端的会再之后的内

 相关资料
  • apollo-link ⚠️ THIS PROJECT HAS BEEN DEPRECATED ⚠️ The Links in this repo have been migrated to the apollo-client project (as of >= @apollo/client@3.0.0). Please refer to the Apollo Client migration g

  • Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。 Swoft 基于 Apollo 提供的 API,在之上进行封装,使之能在 Swoft 中快速使用。 安装 swoft/whoops 作为一个额外的扩展组件,需要手动安装: Composer 安装 com

  • Apollo以ActiveMQ原型为基础,是一个更快、更可靠、更易于维护的消息代理工具。Apache称Apollo为最快、最强健的 STOMP(Streaming Text Orientated Message Protocol,流文本定向消息协议)服务器。 Apollo的特性如下: 支持Stomp 1.0和Stomp 1.1协议 主题和队列 队列浏览器 主题持久订阅 镜像队列 可靠的消息传递 消

  • 描述 Link 是基础的链接组件,在 Web 容器中通过 a 标签实现,它带有默认样式 textDecoration: 'none'。 使用 Link 组件并不能新开一个 Webview ,它只是在当前的 Webview 中做页面的跳转。 安装 $ npm install rax-link --save 属性 属性 类型 默认值 必填 描述 支持 onPress Function - ✘ 节点被点

  • link 方法 把字符串显示为超链接。 语法: stringObject.link( url ); 参数说明: url - 必需,要链接的 URL。 示例: var Str = "graybobo", s = Str.link( 'https://github.com/Graybobo' ); console.log( s ); 结果: >>> <a href

  • 描述 (Description) 此方法创建一个请求另一个URL的HTML超文本链接。 语法 (Syntax) link()方法的语法如下 - string.link( hrefname ) 属性细节 hrefname - 指定A标记的HREF的任何字符串; 它应该是一个有效的URL。 返回值 (Return Value) 返回带有标记的字符串。 例子 (Example) 请尝试以下示例。 <h