To query a GraphQL API, all you need to do is send an HTTP request that includes the query operation in the body of the request. In this lesson, we will use the browser’s fetch method to request the total days skied from our GraphQL API.

const query = `
query {
totalDays
}
`; console.log("querying the count");
fetch("https://8lq1n313m2.sse.codesandbox.io", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query })
})
.then(res => res.json())
.then(({ data }) => `totalDays: ${data.totalDays}`)
.then(console.log)
.catch(console.error);

graphql-request is a lightweight package that can be used to send queries to any GraphQL API. Sending a request with graphql-request uses less syntax than a fetch request. In this lesson, we will query the totalDays field using this helper package.

Install:

npm i --save graphql-request
import { request } from "graphql-request";

const query = `
query {
totalDays
}
`; console.log("querying the count");
request("https://8lq1n313m2.sse.codesandbox.io", query)
.then(({ totalDays }) => `totalDays: ${totalDays}`)
.then(console.log)
.catch(console.error);

Source: https://github.com/prisma/graphql-request

Personally, I feel you can do some wrapper for raw fetch function by yourself instead of install a new dependency to your project, it add more bytes to the total bundles but in return, it is shorten your own code.

最新文章

  1. JDBC连接执行MySQL存储过程报权限错误
  2. [转]何为C10K问题
  3. Linux磁盘与文件系统概念理解
  4. 软件测试Web数据分析工具HttpWatch安装
  5. 初学java,遇到的陌生词语(1)
  6. 如何彻底删除PPA软件库
  7. bootstrap 模态框关闭状态怎么获取
  8. QiQi and Symmerty
  9. linux下tar.xz 文件解压
  10. windows----composer、laravel安装
  11. 工具类 Util.Browser
  12. Eclipse中启动tomcat时内存溢出
  13. 【RL-TCPnet网络教程】第17章 RL-TCPnet之UDP通信
  14. 【原创】线段树query模板对比! 新手线段树的一个容易出错的问题!!因为我就糊涂了一整天.......
  15. Kafka技术内幕 读书笔记之(五) 协调者——消费组状态机
  16. 告别GOPATH,快速使用 go mod(Golang包管理工具)
  17. Qt Dll总结(二)——创建及使用Qt的Dll(转载)
  18. 使用AJAX实现文件上传时Illegal invocation错误
  19. SpringBoot 分页处理
  20. SQL中特殊符号的使用

热门文章

  1. 在GitHub多个帐号上添加SSH公钥
  2. classpath: spring 中的查找方式
  3. shell面试经典70例
  4. 各版本Sql Server下载地址全
  5. [转载]EasyUI中数据表格DataGrid添加排序功能
  6. 组合数问题(NOIP2016)
  7. makefile函数集锦【转】
  8. PO-BO-VO-DTO-POJO-DAO
  9. flask框架下的jinja2模板引擎(3)(模板继承与可以在模板使用的变量、方法)
  10. PHP获取IP的方法