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.

[GraphQL] Query a GraphQL API with graphql-request的更多相关文章

  1. 爬取LeetCode题目——如何发送GraphQL Query获取数据

    前言   GraphQL 是一种用于 API 的查询语言,是由 Facebook 开源的一种用于提供数据查询服务的抽象框架.在服务端 API 开发中,很多时候定义一个接口返回的数据相对固定,因此要获得 ...

  2. 人人都是 API 设计师:我对 RESTful API、GraphQL、RPC API 的思考

    原文地址:梁桂钊的博客 博客地址:http://blog.720ui.com 欢迎关注公众号:「服务端思维」.一群同频者,一起成长,一起精进,打破认知的局限性. 有一段时间没怎么写文章了,今天提笔写一 ...

  3. SpringBoot开发秘籍 - 集成Graphql Query

    概述 REST作为一种现代网络应用非常流行的软件架构风格受到广大WEB开发者的喜爱,在目前软件架构设计模式中随处可见REST的身影,但是随着REST的流行与发展,它的一个最大的缺点开始暴露出来: 在很 ...

  4. Express4.x API (二):Request (译)

    写在前面 最近学习express想要系统的过一遍API,www.expressjs.com是express英文官网(进入www.epxressjs.com.cn发现也是只有前几句话是中文呀~~),所以 ...

  5. harbor rest api 转graphql api

    原理 实际上就是使用graphql 中的binding,首先基于swagger api 进行schema 生成,后边就是 使用binding 进行graphql 请求api 转换为rest api 请 ...

  6. [GraphQL] Use Arguments in a GraphQL Query

    In GraphQL, every field and nested object is able to take in arguments of varying types in order to ...

  7. streamsets rest api 转换 graphql

    原理很简单,就是使用swagger api 生成schema 然后代理请求处理api 调用 参考项目 https://github.com/rongfengliang/streamsets-graph ...

  8. swagger api 转graphql npm 包试用

    graphql 比较方便的进行api 的查询,操作,swagger 是一个方便的open api 描述标准,当前我们有比较多的 restapi 但是转换为graphql 是有成本的,还好swagger ...

  9. haproxy 2.0 dataplaneapi rest api 转为graphql docker 镜像

    为了方便直接使用haproxy dataplaneapi graphql 格式的查询,制作了一个简单的docker 镜像 基于dotenv 进行配置管理,可以直接通过环境变量传入参数,处理不同hapr ...

随机推荐

  1. a标签打电话

    <a href="tel:0147-88469258"></a> <a href="mailto:bd@pangxiekeji.com&qu ...

  2. spring boot 2.0之后默认的连接池 HIkariCP介绍

    HIkariCP链接池比之传统的Tomcat JDBC datasource .c3p0 datasource 等传统链接池优势太大,在获取链接释放链接,执行效率上面高出很多,这个产品的口号是“快速. ...

  3. iOS-android-windowsphone等移动终端平台开发流程图

    到了公司后,半个月时间就是在熟悉下面这张图里的流程, 项目流程图:     下面是我对这张图的一些理解:        

  4. Spring3.2+Struts2.3+Mybatis3.2整合使用(注解使用)

    0.包结构:

  5. 放棋游戏(NOIP模拟赛)(DP)

    没有原题... 囧.. [问题描述] 游戏规则是这样,有n(1<=n<=100)行格子,第一行由n个格子,第二行有n-1个格子,第三行由n-2个格子,……以此类推,第n行有1个格子.要求再 ...

  6. android 画竖虚线

    参考:http://blog.csdn.net/zhao2017/article/details/73866460 1.在Android中写横虚线比较简单,写竖虚线的话稍微麻烦点: 需要将写的虚线旋转 ...

  7. Selenium2+python自动化9-CSS定位语法【转载】

    前言 大部分人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁.这一篇css ...

  8. VS2008中的配置文件app.config简单小结

    应用程序的配置文件用于读取和保存简单的本地数据,vs中新增配置文件可以直接在项目的”属性“-”设置“里添加,添加后在项目的Properties文件夹会多出一组两个文件:Settings.setting ...

  9. [BZOJ1054][HAOI2008]移动玩具 bfs+hash

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2432  Solved: 1355[Submit][Stat ...

  10. [UML] Use Case 是什么

    虽然每个人使用系统的场景有所差异,但是若用户的目标是相同的,则其场景会极为类似.那么这些类似的场景的集合就是类,这种类就称为Use Case.其实例就是场景