简体   繁体   English

如何从我的redux动作向GraphQL-server发出请求?

[英]How to make requests to GraphQL-server from my redux action?

I have react+redux application and I want to replace rest-requests in my actions to grapqhl-requests. 我有react + redux应用程序,我想将我的操作中的rest-request替换为grapqhl-requests。 What is the most simple method to do it? 最简单的方法是什么?

I read about apollo, but I just to want add graphql in my existing requests. 我了解了有关阿波罗的信息,但我只是想在现有请求中添加graphql。

You must change the way you will get the data because Apollo is a layer between you and the Api requested where you can join many Apis into one single query call. 您必须更改获取数据的方式,因为Apollo是您与请求的Api之间的一层,您可以在其中将许多Apis合并到一个查询调用中。 The example above is very simple and just to explain the way will place your querys and how it connects to Apollo Server. 上面的示例非常简单,仅说明了如何放置查询以及如何将查询连接到Apollo Server。 You can set middlewares to log requests on add some headers too. 您也可以设置中间件以在添加一些标头时记录请求。

You will install [ https://github.com/apollographql/react-apollo] and [ https://github.com/apollographql/graphql-tag] 您将安装[ https://github.com/apollographql/react-apollo]和[ https://github.com/apollographql/graphql-tag]

After that create a connection file in your project that imports 之后,在导入的项目中创建一个连接文件

graphClient.js graphClient.js

import { ApolloClient, createNetworkInterface } from 'react-apollo';
const networkInterface = createNetworkInterface({
  uri: 'yourserver address'
});
const client = new ApolloClient({
  networkInterface,
});
export default client;

yourRequestFile.js yourRequestFile.js

import gql from 'graphql-tag';
import client from './graphClient';

export const getYourData = () => ({
  query: gql`
  query {
    products() {
      info {
        value {
          qty
          value
        }
      }
    }
  }`,
});
const returnYourData = async () => client.query(getYourdata());
export default returnYourData;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM