使用lua graphql 模块让openresty 支持graphql api

2021-07-01 16:05

阅读:646

标签:lua   headers   项目   require   order   ret   display   arguments   width   

 

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

标签:lua   headers   项目   require   order   ret   display   arguments   width   

原文地址:https://www.cnblogs.com/rongfengliang/p/9946712.html


评论


亲,登录后才可以留言!