zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    SOFA 源码分析 — 自动故障剔除
    Pod——状态和生命周期管理及探针和资源限制
    pause的作用
    k8s-部署策略
    linux-删除一个目录下的所有文件,但保留某个或者多个指定文件
    k8s-gitlab搭建
    git中报unable to auto-detect email address 错误的解决拌办法
    k8s-secret用法
    k8s-traefik默认80端口
    nginx和apache区别
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/9946712.html
Copyright © 2011-2022 走看看