使用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/模板,也就实现了支持调用所有标签 ...
随机推荐
- 20170501xlVBA销售订单整理一行转多行
Sub NextSeven_CodeFrame() Application.ScreenUpdating = False Application.DisplayAlerts = False Appli ...
- kubectl管理多个k8s集群
#把每个k8s集群的json配置文件放到/root/.kube/目录下,改为不同名字,通过--kubeconfig实现不同集群操作 kubectl --kubeconfig=/root/.kube/m ...
- create view
create view View_count as SELECT spkfk.spid, pf_ckmx.rq, pf_ckmx.spid AS Expr1, pf_ckmx.shl, spk ...
- 使用dbms_output.put_line打印异常所在的行
dbms_output.put_line(dbms_utility.format_error_stack); dbms_output.put_line(dbms_utility.format_call ...
- 【转】asp.net 下的中文分词检索工具 - jieba.net
jieba是python下的一个检索库, 有人将这个库移植到了asp.net 平台下, 完全可以替代lucene.net以及盘古分词的搭配 之所以写这个, 其实是因为昨天面试时, 被问到网站的关键字检 ...
- QT分析之WebKit
该文章整理自 网易博客 http://blog.163.com/net_worm/blog/static/12770241920101831312381/ 转载请注明出处 WebKit是QT4新整合的 ...
- windows下python安装Numpy、Scipy、matplotlib模块
来源http://blog.csdn.net/Katrina_ALi/article/details/64922107 http://blog.csdn.net/qq_16633405/article ...
- linux pipes
In Linux, a pipe is implemented using two filedata structures which both point at the same temporary ...
- C++对象模型——默认构造函数的合成
最近在学习C++对象模型,看的书是侯捷老师的<深度探索C++对象模型>,发现自己以前对构造函数存在很多误解,作此笔记记录. 默认构造函数的误解 1.当程序猿定义了默认构造函数,编译器就会直 ...
- DevExpress v17.2新版亮点—.NET Reporting篇(二)
用户界面套包DevExpress v17.2日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了.NET Reporting v17.2 的新功能,快来下载试用新版本! 支持AS ...