使用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

posted on   荣锋亮  阅读(829)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2017-11-12 gradle 配置java 项目maven 依赖
2017-11-12 gradle 插件
2017-11-12 gradle java 简单项目使用
2017-11-12 gradle 命令行
2017-11-12 gradle wrapper 简单使用
2017-11-12 gradle 安装试用
2017-11-12 linux 使用asciinema 进行命令行屏幕录制共享

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示