From ce146fa4e0c19f0c6a55e3348c8ea45e60f0f17e Mon Sep 17 00:00:00 2001 From: Logic <376693576@qq.com> Date: Wed, 5 Dec 2018 17:05:30 +0800 Subject: [PATCH] =?UTF-8?q?graphql-test=20=E5=AE=A2=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/models/test.js | 41 +++++++++++++++++++++++++++++------------ src/util/fetch-api.js | 16 ++++++++++++++++ 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index a9ecddc0..644fa666 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "dependencies": { "antd": "3.8.2", + "apollo-boost": "^0.1.22", "axios": "^0.18.0", "classnames": "^2.2.6", "history": "^4.7.2", diff --git a/src/models/test.js b/src/models/test.js index 65c229fd..c9bd4eaa 100644 --- a/src/models/test.js +++ b/src/models/test.js @@ -3,14 +3,14 @@ * 项目中不同的大模块可以创建不同的js作为model * 此model中包含了用于src/container/test模块的数据和方法 * **/ -import { message } from "antd"; -import Server from "../util/fetch-api"; // 自己封装的异步请求方法 +import { message } from 'antd'; +import Server from '../util/fetch-api'; // 自己封装的异步请求方法 const model = { /** store数据 **/ state: { count: 0, // 测试数字 - fetchvalue: [] // 异步请求的测试数据 + fetchvalue: [], // 异步请求的测试数据 }, /** reducers **/ reducers: { @@ -19,7 +19,7 @@ const model = { }, updateFetchApi(state, payload) { return { ...state, fetchvalue: payload }; - } + }, }, /** actions **/ actions: { @@ -30,28 +30,45 @@ const model = { // 测试 - ajax请求 async serverAjax(params = {}) { try { - const res = await Server.newPost("url.ajax", params); - if (res.status === "success") { + const res = await Server.newPost('url.ajax', params); + if (res.status === 'success') { this.updateFetchApi(res.data); // 异步请求成功后,可以把数据存入store,即走redux流程 } return res; // 也可以直接返回给view层,在页面中直接处理 } catch (e) { - message.error("网络错误", 1); + message.error('网络错误', 1); } }, // 测试 - fetch请求 async serverFetch(params = {}) { try { - const res = await Server.newPost("url.ajax", params); - if (res.status === "success") { + const res = await Server.newPost('url.ajax', params); + if (res.status === 'success') { this.updateFetchApi(res.data); } return res; } catch (e) { - message.error("网络错误", 1); + message.error('网络错误', 1); + } + }, + // 测试 - graphql请求 + async serverGraphql(params = {}) { + const ql = ` + { + rates(currency: "USD") { + currency + } + } + `; + try { + const res = await Server.newGraphql(ql); + console.log('获取到了什么:', res); + return res; + } catch (e) { + message.error('网络错误', 1); } - } - } + }, + }, }; export default model; diff --git a/src/util/fetch-api.js b/src/util/fetch-api.js index c8df33c7..b08e08ef 100644 --- a/src/util/fetch-api.js +++ b/src/util/fetch-api.js @@ -5,6 +5,12 @@ import reqwest from "reqwest"; // 封装了ajax请求的库 import axios from "axios"; // 封装了fetch请求的库 +import ApolloClient from "apollo-boost"; +import gql from "graphql-tag"; + +const client = new ApolloClient({ + uri: "https://isluo.com/graphql" +}); export default class ApiService { /** ajax请求(用的reqwest.js) **/ @@ -32,4 +38,14 @@ export default class ApiService { data: JSON.stringify(bodyObj) }); } + + static newGraphql(ql) { + client + .query({ + query: gql` + ${ql} + ` + }) + .then(result => console.log(result)); + } }