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. English trip -- Phonics 3 元音字母e

    xu言: 额...今天给我上自然拼读的maple老师 - . -和上次给我上第二集自然拼读的是同一个老师.突然考了考我上次学的内容~感觉大脑一片空白.看来review不能光说而不下苦功夫啊... 元音 ...

  2. Confluence 6 使用 LDAP 授权连接到 Confluence 内部目录

    希望连接一个内部目录但是使用 LDAP 检查登录授权: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 单击左侧面板上面的 用户目录(User Dire ...

  3. svn拒绝连接

    svn 服务开启: svnserve -d -r /home/svn

  4. hdu-1404-博弈+打表

    Digital Deletions Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. Python gui编程pyQt5安装步骤t

    Python gui编程pyQt5安装步骤         pip install PyQt5 Pip3 install PyQt5               https://riverbankco ...

  6. PHP:第二章——PHP中的流程控制语句

    if语句的集中形式 <?php /*if(条件) 语句; if(条件){语句块} if(条件){语句或语句块}else{语句或语句块} if(条件){语句或语句块}elseif(条件){语句或语 ...

  7. 跟我一起学习ASP.NET 4.5 MVC4.0(三)

    今天我们继续ASP.NET 4.5 MVC 4.0,前两天熟悉了MVC4.0在VS11和win8下的更新,以及MVC中的基础语法和几个关键字的使用.了解了这些就可以对MVC进一步认识,相信很多人都对M ...

  8. 《Python》 代码块、小数据池和编码转换

    一.代码块 Python程序是由代码块构造的.块是一个python程序的文本,他是作为一个单元执行的. 什么是代码块:一个py文件,一个函数,一个文件,一个类都是一个代码块. 作为交互方式输入的每一行 ...

  9. express学习-express搭建后台

    前言:本文是纯用node express做一个后端服务的教程,并不等同于express官网的入门教程,本文也并不涉及任何高级的Node服务端性能优化等知识. 本文是在已经看过express官方入门指南 ...

  10. L1-016 查验身份证

    一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...