使用lua graphql 模块让openresty 支持graphql api
graphql 是一个很不错的api 查询标准语言,已经有一个lua 的版本支持graphql
项目使用docker&&docker-compose 运行
环境准备
- 模块安装
luarocks install graphql
- docker镜像准备
模块使用luarocks 安装,默认alpine 镜像是没有安装这个包,我们使用alpine-fat的
FROM openresty/openresty:alpine-fat
RUN /usr/local/openresty/luajit/bin/luarocks install graphql
项目代码
- 项目结构
├── Dockerfile
├── README.md
├── app
├── docker-compose.yaml
└── nginx.conf
- nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
gzip on;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
server {
listen 80;
server_name localhost;
charset utf-8;
root html;
default_type text/html;
location / {
content_by_lua_block {
require("html/app")()
}
}
# graphql 支持
location /g {
more_set_headers 'Content-Type application/json';
content_by_lua_block {
require("g/init")()
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- graphql 代码
app/g/init.lua
local parse = require 'graphql.parse'
local schema = require 'graphql.schema'
local types = require 'graphql.types'
local validate = require 'graphql.validate'
local execute = require 'graphql.execute'
local json = require "cjson"
-- Parse a query
local ast = parse [[
query getUser($id: ID) {
person(id: $id) {
firstName
lastName
}
}
]]
-- Create a type
local Person = types.object {
name = 'Person',
fields = {
id = types.id.nonNull,
firstName = types.string.nonNull,
middleName = types.string,
lastName = types.string.nonNull,
age = types.int.nonNull
}
}
-- Create a schema
local schema = schema.create {
query = types.object {
name = 'Query',
fields = {
person = {
kind = Person,
arguments = {
id = types.id
},
resolve = function(rootValue, arguments)
if arguments.id ~= 1 then return nil end
return {
id = 1,
firstName = 'Bob',
lastName = 'Ross',
age = 52
}
end
}
}
}
}
-- Validate a parsed query against a schema
validate(schema, ast)
-- Execution
local rootValue = {}
local variables = { id = 1 }
local operationName = 'getUser'
local function g()
local result=execute(schema, ast, rootValue, variables, operationName)
ngx.say(json.encode(result))
end
return g
运行
- build 镜像
docker-compose build
- 运行
docker-compose up -d
- 效果

参考资料
https://luarocks.org/modules/hisham/graphql
https://github.com/bjornbytes/graphql-lua
https://github.com/rongfengliang/openresty-lua-demo
使用lua graphql 模块让openresty 支持graphql api的更多相关文章
- 通过torodb && hasura graphql 让mongodb 快速支持graphql api
torodb 可以方便的将mongo 数据实时同步到pg,hasura graphql 可以方便的将pg 数据暴露为graphql api,集成在一起真的很方便 环境准备 docker-compose ...
- 使用ASP.NET Core支持GraphQL -- 较为原始的方法
GraphQL简介 下面是GraphQL的定义: GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时. GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述 ...
- 让ASP.NET Core支持GraphQL之-GraphQL的实现原理
众所周知RESTful API是目前最流行的软件架构风格之一,它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. RESTful的优越性是毋庸置疑 ...
- 使用ASP.NET Core支持GraphQL( restful 配套)
https://github.com/graphql-dotnet https://github.com/graphql GraphQL简介 官网:https://graphql.cn/code/ 下 ...
- GraphQL介绍&使用nestjs构建GraphQL查询服务
GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接 ...
- Nginx Lua拓展模块操作Redis、Mysql
# Nginx的拓展模块 # ngx_lua模块 # 淘宝开发的ngx_lua模块通过lua解释器集成近Nginx,可以采用lua脚本实现业务逻辑,由于lua的紧凑.快速以及内建协程,所以在保证宝兵法 ...
- lua对模块接口扩展的一种方法
module lua中模块的实现,对于使用者来说就是一个库,引用此库后,可以调用库中实现的任意函数. 使用库,可以将一类功能相关的接口做封装,并提供开放接口. 参考: http://blog.codi ...
- Lua中模块初识
定义了两个文件: Module.lua 和 main.lua 其中,模块的概念,使得Lua工程有了程序主入口的概念,其中main.lua就是用来充当程序主入口的. 工程截图如下: Module.lua ...
- dede的织梦问答模块也可以支持arclist标签
dedecms织梦问答等模块支持arclist标签,实现随机调用其他栏目文章 就是让模块模板文件支持调用主站的模板,因为调用主站下的/templets/default/模板,也就实现了支持调用所有标签 ...
随机推荐
- P1758 [NOI2009]管道取珠
考虑这个式子的意义. 不妨看做进行了两轮操作,这个式子显然等价于两次操作后得到的序列相同的方案数. 这个东西显然是可以dp的. 随便优化一下就成了O(n^3)
- Is your JDeveloper Slow? - It shouldn't be!(转)
我的Jdeveloper随便点一个AM,code的显示速度和手指的反应速度跟不上,真的是着急,忍了好久,找到以下教程. 经过考虑,仅仅只是在jdev.conf(jdevbin/jdev/bin/jde ...
- hulu
一. 一开始因为没收到含有共享文档链接的邮件,所以简单自我介绍,聊了几句项目.问了: 1. 玩嗨如果数据库结构变化要怎么办 2. 哈佛项目是否为官方渠道 一直没收到邮件,面试官读网址给我,进到共享文档 ...
- Oracle数据库的“健康指示器”——事件(events)
Oracle数据库“赢在”在架构,这话还是有道理的,除了锁(lock)这个最大的架构上的“赢点”之外,Oracle事件(event)也是另外一个很重要的架构上的“赢点”,因为当今的Oracle数据库, ...
- dubbo监控中心搭建
从网上下载了一个dubbo监控中心,地址忘了,文件名是dubbo-monitor-simple-2.5.3-assembly.tar.gz. 修改监控中心配置文件如下: dubbo.container ...
- ElasticSearch-hadoop saveToEs源码分析
ElasticSearch-hadoop saveToEs源码分析: 类的调用路径关系为: EsSpark -> EsRDDWriter -> RestService -> Rest ...
- .NET 性能优化方法总结==转
.NET 性能优化方法总结 目录 目录 1. C#语言方面... 4 1.1 垃圾回收... 4 1.1.1 避免不必要的对象创建... 4 1.1.2 不要使用空析构函数 ★... 4 1.1.3 ...
- 提高C++程序运行效率的10个简单方法
转载: 一.尽量减少值传递,多用引用来传递参数.至于其中的原因,相信大家也很清楚,如果参数是int等语言自定义的类型可能能性能的影响还不是很大,但是如果参数是一个类的对象,那么其效率问题就不言而喻了. ...
- php中的curl的一些参数总结
curl可以根据是否是http或则是https选择加密发送的内容: 使用curl发送请求的基本流程 1,初始化连接句柄: 2,设置curl选项: 3,执行并获取结果: 4,释放curl连接句柄: 例子 ...
- 推荐八款.Net优秀的开源CMS 内容管理系统
1,老牌内容管理系统SiteServer CMS 推荐指数:5 SiteServer CMS 是.NET平台的CMS系统,也是一款拥有十年历史与广泛知名度的CMS系统,2017年5月初迈出了自成立以 ...