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的更多相关文章

  1. 通过torodb && hasura graphql 让mongodb 快速支持graphql api

    torodb 可以方便的将mongo 数据实时同步到pg,hasura graphql 可以方便的将pg 数据暴露为graphql api,集成在一起真的很方便 环境准备 docker-compose ...

  2. 使用ASP.NET Core支持GraphQL -- 较为原始的方法

    GraphQL简介 下面是GraphQL的定义: GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时. GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述 ...

  3. 让ASP.NET Core支持GraphQL之-GraphQL的实现原理

    众所周知RESTful API是目前最流行的软件架构风格之一,它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. RESTful的优越性是毋庸置疑 ...

  4. 使用ASP.NET Core支持GraphQL( restful 配套)

    https://github.com/graphql-dotnet https://github.com/graphql GraphQL简介 官网:https://graphql.cn/code/ 下 ...

  5. GraphQL介绍&使用nestjs构建GraphQL查询服务

    GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接 ...

  6. Nginx Lua拓展模块操作Redis、Mysql

    # Nginx的拓展模块 # ngx_lua模块 # 淘宝开发的ngx_lua模块通过lua解释器集成近Nginx,可以采用lua脚本实现业务逻辑,由于lua的紧凑.快速以及内建协程,所以在保证宝兵法 ...

  7. lua对模块接口扩展的一种方法

    module lua中模块的实现,对于使用者来说就是一个库,引用此库后,可以调用库中实现的任意函数. 使用库,可以将一类功能相关的接口做封装,并提供开放接口. 参考: http://blog.codi ...

  8. Lua中模块初识

    定义了两个文件: Module.lua 和 main.lua 其中,模块的概念,使得Lua工程有了程序主入口的概念,其中main.lua就是用来充当程序主入口的. 工程截图如下: Module.lua ...

  9. dede的织梦问答模块也可以支持arclist标签

    dedecms织梦问答等模块支持arclist标签,实现随机调用其他栏目文章 就是让模块模板文件支持调用主站的模板,因为调用主站下的/templets/default/模板,也就实现了支持调用所有标签 ...

随机推荐

  1. Confluence 6 使用 LDAP 授权连接一个内部目录 - 用户 Schema 设置

    请注意:这部分仅在拷贝用户登录(Copy User on Login)功能被启用后可见. 其他用户 DN(Additional User DN) 这个值被用在进行用户查找和载入的时候来针对 base ...

  2. 二叉树—-1(No.9HN省赛小题)

    题目: 1013: Prototypes analyze 时间限制: 1 Sec  内存限制: 128 MB提交: 6  解决: 4[提交][状态][讨论版] 题目描述 ALpha Ceiling M ...

  3. LeetCode 318. Maximum Product of Word Lengths (状态压缩)

    题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...

  4. Phython笔记初识

    Phython笔记初识   Python 1898 第一版本 1991 荷兰人 Guido  协议 Gpl                     动态语音类型  

  5. 015PHP文件处理——文件处理flock 文件锁定 pathinfo realpath tmpfile tempname

    <?php /**文件处理flock 文件锁定 pathinfo realpath tmpfile tempname */ /*$arr=pathinfo('ab.txt');//获取文件路径的 ...

  6. PHP:第一章——php中的变量001 /普通赋值/引用赋值/php变量的检查与销毁

    <?php //php中的变量: //php中的变量用一个美元符$后面紧跟着变量名来表示,变量名是区分大小写的. //有效的变量只能是字母或者下划线开头,后面跟任意数量的字母.数字.或者下划线. ...

  7. 使用shake.js让你博客支持摇一摇

    大家好,又到了随机文章的时间,请使用手机打开演示站点,然后像摇妹子一样摇晃手机,你会发现非常牛逼的事情,炫酷吧.该功能已经集成在Oconnor1.8中.本文主要讲解这货的原理. 首先需要下载shake ...

  8. css控制编辑器内容自动换行

    在编辑器或者文本框中按住数字或字母不放 当字符很长时,就会撑破页面, 可以用一下方法控制字符自动换行 style="word-break:break-all;"

  9. mac_os_x更新yosemite以后github客户端更新提示ca认证错误解决办法

    最近手贱更新了mac os yosemite的系统版本,更新以后发现部分软件无法使用,例如php 扩展的redis模块,mou,eclipse等等,甚是郁闷啊.对于图形化的软件还好说去官网更新一下新版 ...

  10. 安装Linux Mint17

    韩总有台笔记本之前安装的是Win7,结果被她用成含毒,含马的机器了,最后干脆机器操作不了,愤怒的韩总把戴尔骂了个痛快并保证以后再也不用戴尔的笔记本了,然后愉快的换了一台新电脑,这台机器便放在我这里没人 ...