GraphQL 缓存

缓存是将数据存储在称为缓存的临时存储区域中的过程 。当您返回到最近访问过的页面时,浏览器可以从缓存而不是原始服务器获取这些文件。这样可以节省您的时间和网络,减轻额外流量的负担。

与GraphQL交互的客户端应用程序负责在其末端缓存数据。一种可能的模式是将字段(如id)保留为全局唯一标识符。

InMemory Cache

InMemoryCache是GraphQL客户端应用程序中常用的规范化数据存储,不使用其他库,如Redux。

使用InMemoryCache和ApolloClient的示例代码如下:

import {ApolloClient, HttpLink, InMemoryCache} from 'apollo-boost'
const cache = new InMemoryCache();

const client = new ApolloClient({
   link: new HttpLink(),
   cache
});

InMemoryCache构造函数使用带有属性的可选配置对象来自定义缓存。

序号 参数和描述
1

addTypename

一个布尔值,用于确定是否将__typename添加到文档中(默认值:true)

2

dataIdFromObject

获取数据对象并返回在规范化存储中的数据时要使用的唯一标识符的函数

3

fragmentMatcher

默认情况下,InMemoryCache使用启发式片段匹配器

4

cacheRedirects

在发出请求之前将查询重定向到缓存中的另一个条目的函数映射。

 

我们将在ReactJS中创建一个带有两个选项卡的单页面应用程序 - 一个用于主页选项卡,另一个用于学生。学生选项卡将从GraphQL服务器API加载数据。当用户从主页选项卡导航到学生选项卡时,应用程序将查询学生数据。结果数据将由应用程序缓存。

我们还将使用getTime字段查询服务器时间,以验证页面是否已缓存。如果从缓存返回数据,页面将显示发送到服务器的第一个请求的时间。如果数据是对服务器发出新请求的结果,则它将始终显示服务器的最新时间。

1. 设置服务器

以下是设置服务器的步骤

步骤1 - 下载并安装项目所需的依赖项

创建文件夹 cache-server-app。将目录 从终端更改为 cache-server-app。按照环境设置章节中说明的步骤3到5进行操作。

第2步 - 创建架构

在项目文件夹cache-server-app中添加schema.graphql文件并添加以下代码:

type Query {
   students:[Student]
   getTime:String
}

type Student {
   id:ID!
   firstName:String
   lastName:String
   fullName:String
}

第3步 - 添加解析器

在项目文件夹中创建文件resolvers.js,并添加以下代码:

const db = require('./db')

const Query = {
      students:() => db.students.list(),
      getTime:() => {
      const today = new Date();
      var h = today.getHours();
      var m = today.getMinutes();
      var s = today.getSeconds();
      return `${h}:${m}:${s}`;
   }
}
module.exports = {Query}

第4步 - 运行应用程序

创建server.js文件。请参阅环境设置章节中的步骤8。在终端中执行命令npm start。服务器将在9000端口上启动并运行。在这里,我们将使用GraphiQL作为客户端来测试应用程序。

打开浏览器并输入URL http://localhost:9000/graphiql。在编辑器中键入以下查询:

{
   getTime
   students {
      id
      firstName
   }
}

示例响应显示学生姓名和服务器时间。

{
   "data": {
      "getTime": "22:18:42",
      "students": [
         {
            "id": "S1001",
            "firstName": "Mohtashim"
         },
         {
            "id": "S1002",
            "firstName": "Kannan"
         },
         {
            "id": "S1003",
            "firstName": "Kiran"
         }
      ]
   }
}

2. 设置ReactJS客户端

为客户打开一个新的终端。在执行客户端应用程序之前,服务器终端应保持运行。React应用程序将在端口号3000上运行,服务器应用程序将在端口号9000上运行。

第1步 - 创建React应用程序

在客户端终端中,键入以下命令:

npx create-react-app hello-world-client

这将安装典型反应应用所需的一切。该NPX实用和创造-反应,应用工具创建一个名称你好世界客户端的项目。安装完成后,在VSCode中打开项目。

使用以下命令安装路由器模块以进行响应 - npm install react-router-dom。

第2步 - 启动hello-world-client

将终端中的当前文件夹路径更改为hello-world-client。键入npm start以启动项目。这将在端口3000上运行开发服务器,并将自动打开浏览器并加载索引页面。

这在下面给出的屏幕截图中显示:

屏幕截图

第3步 - 安装Apollo客户端库

要安装Apollo客户端,请打开一个新终端并进入当前项目文件夹路径。键入以下命令

npm install apollo-boost graphql

这将下载客户端的graphql库以及Apollo Boost包。我们可以通过键入npm view apollo-boost dependencies来交叉验证。这将有许多依赖关系,如下所示

{
   'apollo-cache': '^1.1.15',
   'apollo-cache-inmemory': '^1.2.8',
   'apollo-client': '^2.4.0',
   'apollo-link': '^1.0.6',
   'apollo-link-error': '^1.0.3',
   'apollo-link-http': '^1.3.1',
   'apollo-link-state': '^0.4.0',
   'graphql-tag': '^2.4.2'
}

我们可以清楚地看到安装了apollo-client库。

第4步 - 修改index.js文件中的App组件

对于一个简单的反应应用程序,你只需要将index.js保存在src文件夹中,将index.html保存在公共文件夹中; 可以删除自动生成的所有其他文件。

目录结构如下

hello-world-client /
   -->node_modules
   -->public
      index.html
   -->src
      index.js
      students.js
   -->package.json

添加一个包含Students Component的附加文件students.js。学生详细信息通过学生组件获取。在App Component中,我们使用的是HashRouter。

以下是 反应应用中的 index.js

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {HashRouter, Route, Link} from 'react-router-dom'

//components
import Students from './students'
class App extends Component {
   render() {
      return(
         <div><h1>Home !!</h1>
         <h2>Welcome to React Application !! </h2>
         </div>
      )
   }
}

function getTime() {
   var d = new Date();
   return d.getHours()+":"+d.getMinutes()+":"+d.getSeconds()
}

const routes = <HashRouter>
   <div>
      <h4>Time from react app:{getTime()}</h4>
      <header>
         <h1>  <Link to="/">Home</Link> 
         <Link to = "/students">Students</Link>  </h1>
      </header>
      <Route exact path = "/students" component = {Students}></Route>
      <Route exact path = "/" component = {App}></Route>
   </div>
</HashRouter>

ReactDOM.render(routes, document.querySelector("#root"))

第5步 - 在Students.js中编辑组件学生

在Students Component中,我们将使用以下两种方法来加载数据

  • Fetch API(loadStudents_noCache) - 每次单击student选项卡时,都会触发一个新请求。
  • Apollo Client(loadWithApolloclient) - 这将从缓存中获取数据。

添加一个函数loadWithApolloclient,它从服务器查询学生和时间。此功能将启用缓存。这里我们使用gql函数来解析查询。

async loadWithApolloclient() {
   const query = gql`{
      getTime
      students {
         id
         firstName
      }
   }`;

   const {data} = await  client.query({query})
   return data;
}

该提取API是获取资源的简单接口。与旧的XMLHttpRequest相比,Fetch使Web请求和处理响应变得更容易。以下方法显示使用fetch api直接加载数据:

async  loadStudents_noCache() {
      const response = await fetch('http://localhost:9000/graphql', {
      method:'POST',
      headers:{'content-type':'application/json'},
      body:JSON.stringify({query:`{
         getTime
         students {
            id
            firstName
         }
      }`})
   })

   const rsponseBody = await response.json();
   return rsponseBody.data;
}

StudentsComponent的构造函数中,调用loadWithApolloClient方法。完整的 Student.js 文件如下

import React, {Component} from 'react';
import { Link} from 'react-router-dom'

//Apollo Client
import {ApolloClient, HttpLink, InMemoryCache} from 'apollo-boost'
import gql from 'graphql-tag'
const client = new ApolloClient({
   link: new HttpLink({uri:`http://localhost:9000/graphql`}),
   cache:new InMemoryCache()
})

class Students extends Component {
   constructor(props) {
      super(props);
      this.state = {
         students:[{id:1,firstName:'test'}],
         serverTime:''
      }
      this.loadWithApolloclient().then(data => {
         this.setState({
            students:data.students,
            serverTime:data.getTime
         })
      })
   }

   async  loadStudents_noCache() {
      const response = await fetch('http://localhost:9000/graphql', {
         method:'POST',
         headers:{'content-type':'application/json'},
         body:JSON.stringify({query:`{
            getTime
            students {
               id
               firstName
            }
         }`})
      })
      const rsponseBody =  await response.json();
      return rsponseBody.data;
   }

   async loadWithApolloclient() {
      console.log("inside apollo client function")
      const query = gql`{
         getTime
         students {
            id
            firstName
         }
      }`;
      const {data} = await  client.query({query})
      return data;
   }

   render() {
      return(
         <div>
            <h3>Time from GraphQL server :{this.state.serverTime}</h3>
            <p>Following Students Found </p>
            <div>
               <ul>
                  {
                     this.state.students.map(s => {
                        return(
                           <li key = {s.id}>
                              {s.firstName}
                           </li>
                        )
                     })
                  }
               </ul>
            </div>
         </div>
      )
   }
}
export default Students

第6步 - 使用npm start运行React Application

您可以通过从主页选项卡切换到学生选项卡来测试反应应用程序。一旦学生选项卡加载了来自服务器的数据。它将缓存数据。您可以通过多次从home切换到student选项卡来测试它。输出如下图所示

如果您首先通过键入URL http://localhost:3000/#/students 来加载学生页面 ,则可以看到react app和GraphQL的加载时间大致相同。之后,如果切换到主视图并返回到GraphQL服务器,则时间不会更改。这表明数据已缓存。

第7步 - 将loadWithApolloclient调用更改为loadStudents_noCache

如果在StudentComponent的构造函数中将load方法更改为loadStudents_noCache,则输出将不会缓存数据。这显示了缓存和非缓存之间的区别。

this.loadStudents_noCache().then(data => {
   this.setState({
      students:data.students,
      serverTime:data.getTime
   })
})