V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ivydom
V2EX  ›  程序员

推荐一个微信小程序的 GraphQL 客户端-wxapp-graphql

  •  
  •   ivydom · 2018-06-14 12:31:05 +08:00 · 2321 次点击
    这是一个创建于 2160 天前的主题,其中的信息可能已经有所发展或是发生改变。

    wxapp-graphql

    github: https://github.com/Authing/wxapp-graphql

    微信小程序 GraphQL 客户端,点击图片观看演示 demo

    Watch the video

    使用方法

    clone 项目

    git clone https://github.com/Authing/wxapp-graphql
    

    将文件中的 graphql 文件夹复制到你的项目中并开始使用

    
    // 引入文件
    var gql = require('path/to/graphql/wxgql.js');
    var GraphQL = gql.GraphQL;
    
    Page({
      test: function() {
    
        // 初始化对象
        let gql = GraphQL({
          url: 'https://users.authing.cn/graphql' // url 必填 
        }, true); //第二个参数的 true 代表是否使用对象方法,如 gql.query 或 gql.mutate,默认是函数方法,如 gql({body: {query: '', variables: {}}}),建议写 true,为 true 时可以使用 promise
    
        gql.query({
            query: `query getAccessTokenByAppSecret($secret: String!, $clientId: String!){
        getAccessTokenByAppSecret(secret: $secret, clientId: $clientId)
    }`,
            variables: {
              secret: '427e24d3b7e289ae9469ab6724dc7ff0',
              clientId: '5a9fa26cf8635a000185528c'
            }
        }).then(function(res) {
          //成功
        }).catch(function(error) {
          //失败
        });
        
      }
    });
    
    

    另外一种使用方法(可选)

    var gql = require('path/to/graphql/wxgql.js');
    var GraphQL = gql.GraphQL;
    
    Page({
      test: function() {
        let gql = GraphQL({
          url: 'https://users.authing.cn/graphql' // url 必填 
        });
        
        gql({
          // 示例 GraphQL 查询, body 必填
          body: {
            query: `query getAccessTokenByAppSecret($secret: String!, $clientId: String!){
        getAccessTokenByAppSecret(secret: $secret, clientId: $clientId)
    }`,
            variables: {
              secret: '427e24d3b7e289ae9469ab6724dc7ff0',
              clientId: '5a9fa26cf8635a000185528c'
            }
          },
    
          // 成功
          success: function (res) {
            console.log(res);
          },
    
          // 失败
          fail: function (res) {
            console.log(res);
          },
    
          // 执行完成
          complete: function (res) {
            console.log(res);
          }
        });
    });
    

    TODO

    1. 兼容 NPM
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3028 人在线   最高记录 6547   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:20 · PVG 08:20 · LAX 17:20 · JFK 20:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.