apollo graphql mysql_Apollo Server: GraphQL 数据分页概述

分页概述

由于Relay中的分页比较复杂, 本文不使用Relay的方式进行分页, 使用自定义GraphQL分页类型的方式进行分页.

REST的分页方式:

https://meta.discourse.org/articles?p=1

GraphQL查询分页

{

articles {

total_items # 总数

page_no # 当前页号

page_items # 每页结果数

pages # 总页数

rows: { # 分页结果

title,

author,

published_at

}

}

}

自定义分页

首先我们需要定义一个GraphQL的分页类型, 对于上述文章例子, 我们定义一个ArticlePagination类型如下:

# 文章分页对象

type ArticlePagination {

# 总共有多少条数据

total_items: Int!

# 总共有多少页

pages: Int!

# 当前页号

page_no: Int!

# 每页显示多少条数据

page_items: Int!

# 分页列表

rows: [Article]

}

然后在我们的查询字段中返回这个分页对象

type Query {

...

articles(page_no: Int!, page_items: Int!): ArticlePagination

...

}

定义解析函数

async feedbacks(_, { page_no, page_items }) {

return db.paginateArticles(page_no, page_items)

}

上述说明都是我自己参考这个项目的学习成功, 由于目前开发的项目不是开源项目, 所以就没有完整的演示代码了.

要点

使用了 ES7 的 async/await 功能, 让MySQL支持同步风格代码(对于这里是必须的, 否则解析函数会有问题)

使用了 babel-node 直接运行ES6代码, 需要按照 babel-cli 工具

使用 nodemon 来监视文件的变化, 实现热加载

关于 MySQL 的同步风格代码

查询方法定义

function query_sync(sql, params) {

return new Promise((resolve, reject) => {

pool.getConnection(function (err, connection) {

if (err) {

reject(err)

} else {

connection.query(sql, params, (err, rows) => {

if (err) {

reject(err)

} else {

resolve(rows)

}

connection.release()

})

}

})

})

}

对于 async/await 的数据库卡查询, await 后的函数总是返回一个Promise对象, 下面是调用方式

exports.paginateArticles = async function(){

let limit = 10

let offset = 0

let stmt = 'SELECT * FROM articles LIMIT ? OFFSET ?'

let result = await query_sync(stmt, [limit, offset])

return result

}

await 只能用在 async 函数中

await 函数必须返回一个 Promise 对象

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
graphql-java 是 GraphQL 的 Java 实现。这个库的目标是用于真实的生产环境。graphql-java 解析和执行查询 GraphQL 。它并不真正获取任何数据的:数据来源于执行回调或提供静态数据graphql-java 的 "hello world":import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import static graphql.Scalars.GraphQLString; import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLObjectType.newObject; public class HelloWorld {     public static void main(String[] args) {         GraphQLObjectType queryType = newObject()                         .name("helloWorldQuery")                         .field(newFieldDefinition()                                 .type(GraphQLString)                                 .name("hello")                                 .staticValue("world")                                 .build())                         .build();         GraphQLSchema schema = GraphQLSchema.newSchema()                         .query(queryType)                         .build();         Map result = new GraphQL(schema).execute("{hello}").getData();         System.out.println(result);         // Prints: {hello=world}     } } 标签:graphql
root@in_dev_docker:/apollo# bash scripts/msf_create_lossless_map.sh /apollo/hdmap/pcd_apollo/ 50 /apollo/hdmap/ /apollo/bazel-bin WARNING: Logging before InitGoogleLogging() is written to STDERR E0715 22:08:35.399576 6436 lossless_map_creator.cc:162] num_trials = 1 Pcd folders are as follows: /apollo/hdmap/pcd_apollo/ Resolution: 0.125 Dataset: /apollo/hdmap/pcd_apollo Dataset: /apollo/hdmap/pcd_apollo/ Loaded the map configuration from: /apollo/hdmap//lossless_map/config.xml. Saved the map configuration to: /apollo/hdmap//lossless_map/config.xml. Saved the map configuration to: /apollo/hdmap//lossless_map/config.xml. E0715 22:08:35.767315 6436 lossless_map_creator.cc:264] ieout_poses = 1706 Failed to find match for field 'intensity'. Failed to find match for field 'timestamp'. E0715 22:08:35.769896 6436 velodyne_utility.cc:46] Un-organized-point-cloud E0715 22:08:35.781770 6436 lossless_map_creator.cc:275] Loaded 245443D Points at Trial: 0 Frame: 0. F0715 22:08:35.781791 6436 base_map_node_index.cc:101] Check failed: false *** Check failure stack trace: *** scripts/msf_create_lossless_map.sh: line 11: 6436 Aborted (core dumped) $APOLLO_BIN_PREFIX/modules/localization/msf/local_tool/map_creation/lossless_map_creator --use_plane_inliers_only true --pcd_folders $1 --pose_files $2 --map_folder $IN_FOLDER --zone_id $ZONE_ID --coordinate_type UTM --map_resolution_type single root@in_dev_docker:/apollo# bash scripts/msf_create_lossless_map.sh /apollo/hdmap/pcd_apollo/ 50 /apollo/hdmap/
最新发布
07-16

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值