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

2018-06-14 12:31:05 +08:00
 ivydom

wxapp-graphql

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

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

使用方法

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
2323 次点击
所在节点    程序员
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/463047

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX