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

graphql-batch

A query batching executor for the graphql gem
授权协议 MIT License
开发语言 Java
所属分类 大数据、 数据查询
软件类型 开源软件
地区 不详
投 递 者 颜君浩
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

GraphQL::Batch

Provides an executor for the graphql gem which allows queries to be batched.

Installation

Add this line to your application's Gemfile:

gem 'graphql-batch'

And then execute:

$ bundle

Or install it yourself as:

$ gem install graphql-batch

Usage

Basic Usage

Schema Configuration

Require the library

require 'graphql/batch'

Define a custom loader, which is initialized with arguments that are used for grouping and a perform method for performing the batch load.

class RecordLoader < GraphQL::Batch::Loader
  def initialize(model)
    @model = model
  end

  def perform(ids)
    @model.where(id: ids).each { |record| fulfill(record.id, record) }
    ids.each { |id| fulfill(id, nil) unless fulfilled?(id) }
  end
end

Use GraphQL::Batch as a plugin in your schema after specifying the mutationso that GraphQL::Batch can extend the mutation fields to clear the cache afterthey are resolved (for graphql >= 1.5.0).

class MySchema < GraphQL::Schema
  query MyQueryType
  mutation MyMutationType

  use GraphQL::Batch
end

For pre 1.5.0 versions:

MySchema = GraphQL::Schema.define do
  query MyQueryType

  GraphQL::Batch.use(self)
end

Field Usage

The loader class can be used from the resolver for a graphql field by calling .for with the grouping arguments to get a loader instance, then call .load on that instance with the key to load.

field :product, Types::Product, null: true do
  argument :id, ID, required: true
end

def product(id:)
  RecordLoader.for(Product).load(id)
end

The loader also supports batch loading an array of records instead of just a single record, via load_many. For example:

field :products, [Types::Product, null: true], null: false do
  argument :ids, [ID], required: true
end

def products(ids:)
  RecordLoader.for(Product).load_many(ids)
end

Although this library doesn't have a dependency on active record,the examples directory has record and association loadersfor active record which handles edge cases like type casting idsand overriding GraphQL::Batch::Loader#cache_key to load associationson records with the same id.

Promises

GraphQL::Batch::Loader#load returns a Promise using the promise.rb gem to provide a promise based API, so you can transform the query results using .then

def product_title(id:)
  RecordLoader.for(Product).load(id).then do |product|
    product.title
  end
end

You may also need to do another query that depends on the first one to get the result, in which case the query block can return another query.

def product_image(id:)
  RecordLoader.for(Product).load(id).then do |product|
    RecordLoader.for(Image).load(product.image_id)
  end
end

If the second query doesn't depend on the first one, then you can use Promise.all, which allows each query in the group to be batched with other queries.

def all_collections
  Promise.all([
    CountLoader.for(Shop, :smart_collections).load(context.shop_id),
    CountLoader.for(Shop, :custom_collections).load(context.shop_id),
  ]).then do |results|
    results.reduce(&:+)
  end
end

.then can optionally take two lambda arguments, the first of which is equivalent to passing a block to .then, and the second one handles exceptions. This can be used to provide a fallback

def product(id:)
  # Try the cache first ...
  CacheLoader.for(Product).load(id).then(nil, lambda do |exc|
    # But if there's a connection error, go to the underlying database
    raise exc unless exc.is_a?(Redis::BaseConnectionError)
    logger.warn err.message
    RecordLoader.for(Product).load(id)
  end)
end

Unit Testing

Your loaders can be tested outside of a GraphQL query by doing thebatch loads in a block passed to GraphQL::Batch.batch. That methodwill set up thread-local state to store the loaders, batch load anypromise returned from the block then clear the thread-local stateto avoid leaking state between tests.

def test_single_query
    product = products(:snowboard)
    title = GraphQL::Batch.batch do
      RecordLoader.for(Product).load(product.id).then(&:title)
    end
    assert_equal product.title, title
  end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

See our contributing guidelines for more information.

License

The gem is available as open source under the terms of the MIT License.

  • GraphQL 入门: 简介 GraphQL 入门: Apollo Client - 简介 GraphQL 入门: Apollo Client - 安装和配置选项 GraphQL 入门: Apollo Client - 连接到数据 GraphQL 入门: Apollo Client - 网络层 GraphQL 入门: Apollo Client - 开发调试工具 GraphQL 入门: Apoll

  • 怎么说, 用python简直是一种享受. 以为python提供了神奇的魔术方法, 基于这些方法写出的框架非常"智能". 代码冗余少, 实现优雅 ! 本篇文章将简述如何用python提供mongodb+GraphQL的基本服务. mongoengine MongoEngine is a Document-Object Mapper (think ORM, but for document datab

  • graphql 前端编写 by Rasheed Bustamam 通过拉希德·布斯塔曼(Rasheed Bustamam) GraphQL:前端查询变得容易 (GraphQL: front-end queries made easy) If you’ve been reading about the latest trends in front-end development, you may h

  • graphql查询方案优化 起因 graphql API对外提供完整的接口文档,按需取数据的功能,对于接口联调和前端开发同学看来绝对是神器,但对于初看graphql的后台同学看来,这个取数据怎么取?对于一个两层结构的列表数据,我对列表中的每一项都取一次二级数据?这后台抗得住吗? 举个例子:我们有这样一个应用场景,我们要取一个用户列表,列表中每一项展示有3个字段id、name、friends,fri

  • 一、简介 GraphQL 是一个Facebook于2012开发出来且2015开源的应用层的查询语言,你需要在后台定义一个基于GraphQL的图形模式(schema),然后你的客户端就可以查询他们想要的数据,而不需要后台重新定义一个接口返回你需要的数据. ​ 因为不需要更改你后台,所以这种方式比 REST API 方式更好,让我们可以在不同的客户端上灵活改变数据显示. 二、安装 # DOS命令下,管

  • 如果喜欢我们的文章别忘了点击关注阿里南京技术专刊呦~ 本文转载自 阿里南京技术专刊-知乎,欢迎大牛小牛投递阿里南京前端/后端开发等职位,详见 阿里南京诚邀前端小伙伴加入~。 在最近的项目中,我们选择了 GraphQL 作为 API 查询语言替代了传统的 Restful 传参的方法进行前后端数据传递。服务端选用了 egg.js + Apollo graphql-tools,前端使用了 React.j

  • 项目背景 后端开发经常会面临的一个问题是数据存储,和数据管理。不论你是用的mysql,oracle,cassandra,postgres,mongo等等,要查询这些数据存储介质必须为每一个数据源开发或者集成相对应的中间件。而随着我们的项目增多,或者存储介质的迁移变动时,我们不得不面临代码重构,甚至重做的问题,这其间会产生大量的时间成本和人力成本。GraphQL正是在这种背景下诞生的,它定义了一套s

 相关资料
  • 快速开始 GraphQL 是一种用于 API 的查询语言。这是 GraphQL 和 REST 之间一个很好的比较 (译者注: GraphQL 替代 REST 是必然趋势)。在这组文章中, 我们不会解释什幺是 GraphQL, 而是演示如何使用 @nestjs/GraphQL 模块。 GraphQLModule 只不过是 Apollo 服务器的包装器。我们没有造轮子, 而是提供一个现成的模块, 这让

  • GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。 向你的 API 发出一个 GraphQL 请求就能准确获得你想要的数据,不多不少。 GraphQL 查询总是返回可预测

  • Graphql editor 是一款 Graphql 的可视化编辑器和 IDE,帮助用户更容易理解 GraphQL 模式,通过使用可视化块系统创建模式。GraphQL Editor 将把它们转化为代码。通过 GraphQL Editor,用户可以在不写任何代码的情况下创建可视化的图表,或者以一种很好的方式呈现其模式。 GraphQL View Code Editor View Hierarchy View

  • GraphQL CLI Help us to improve new GraphQL CLI. Check out the new structure and commands below!Feel free to contact us in Discord channel. We would love to hear your feedback. Features Helpful command

  • Fullstack GraphQL Simple Demo Application API built with Node + Express + GraphQL + Sequelize (supports MySQL, Postgres, Sqlite and MSSQL). WebApp built with React + Redux. Written in ES6 using Babel

  • Hasura GraphQL Engine Hasura is an open source product that accelerates API development by 10x by giving you GraphQL or REST APIs with built in authorization on your data, instantly. Read more at hasu

  • 这是利用Koa + GraphQL + Apollo-Server实现的,学生信息增、删、改、查的栗子 本栗子将搭配Koa实现一个GraphQL查询,逐步从简单Kao服务、到Mongodb的数据插入查询、再到GraphQL的使用,让大家快速看到: 搭建Koa搭建一个后台项目 后台路由简单处理方式 利用Mongoose简单操作Mongodb的增、删、改、查 掌握Apollo-Server简单操作数据

  • GraphQL Ruby 是 GraphQL 的一个 Ruby 实现。 Website API Documentation Newsletter 安装: # Gemfilegem 'graphql' $ bundle install 旨在: 实现 GraphQL 规范并支持一个 Relay 前端 在可能的情况下,提供与参考实现相似的习惯性的、简单的 Ruby API 支持 Ruby on Rails 和 Relay