[GraphQL] Query a GraphQL API with graphql-request
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的更多相关文章
- 爬取LeetCode题目——如何发送GraphQL Query获取数据
前言 GraphQL 是一种用于 API 的查询语言,是由 Facebook 开源的一种用于提供数据查询服务的抽象框架.在服务端 API 开发中,很多时候定义一个接口返回的数据相对固定,因此要获得 ...
- 人人都是 API 设计师:我对 RESTful API、GraphQL、RPC API 的思考
原文地址:梁桂钊的博客 博客地址:http://blog.720ui.com 欢迎关注公众号:「服务端思维」.一群同频者,一起成长,一起精进,打破认知的局限性. 有一段时间没怎么写文章了,今天提笔写一 ...
- SpringBoot开发秘籍 - 集成Graphql Query
概述 REST作为一种现代网络应用非常流行的软件架构风格受到广大WEB开发者的喜爱,在目前软件架构设计模式中随处可见REST的身影,但是随着REST的流行与发展,它的一个最大的缺点开始暴露出来: 在很 ...
- Express4.x API (二):Request (译)
写在前面 最近学习express想要系统的过一遍API,www.expressjs.com是express英文官网(进入www.epxressjs.com.cn发现也是只有前几句话是中文呀~~),所以 ...
- harbor rest api 转graphql api
原理 实际上就是使用graphql 中的binding,首先基于swagger api 进行schema 生成,后边就是 使用binding 进行graphql 请求api 转换为rest api 请 ...
- [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 ...
- streamsets rest api 转换 graphql
原理很简单,就是使用swagger api 生成schema 然后代理请求处理api 调用 参考项目 https://github.com/rongfengliang/streamsets-graph ...
- swagger api 转graphql npm 包试用
graphql 比较方便的进行api 的查询,操作,swagger 是一个方便的open api 描述标准,当前我们有比较多的 restapi 但是转换为graphql 是有成本的,还好swagger ...
- haproxy 2.0 dataplaneapi rest api 转为graphql docker 镜像
为了方便直接使用haproxy dataplaneapi graphql 格式的查询,制作了一个简单的docker 镜像 基于dotenv 进行配置管理,可以直接通过环境变量传入参数,处理不同hapr ...
随机推荐
- Python之Excel编程
excel编程:excel中是unicode编码方式 需要使用xrld,xlwt和openpyxl这三个模块,需先通过pip install下载 xlrd 读取模块:xls,xlsx ...
- git上导出的maven项目转成正常的Dynamic web project
项目地址:http://git.oschina.net/jiafuwei0407/springmvc,springmvc maven项目 包含日志的测试 restful api 如何在eclipse ...
- 实现实体类和Xml相互转化
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- 搜索水题四连发_C++
特别声明:以下题目有部分为原创题,涉及版权问题,不得转载,违者追究 法律责任! 话说这是一套神题,只有你想不到,没有你做不到 题目更正后比 Pascal 跑得还快哈~ 一道特别裸,但是特别坑的搜索题 ...
- 戴文的Linux内核专题:03 驱动程序【转】
转自:http://www.lai18.com/content/432194.html 驱动程序是使内核能够沟通和操作硬件或协议(规则和标准)的小程序.没有驱动程序,内核不知道如何与硬件沟通或者处理协 ...
- [BZOJ1032][JSOI2007]祖码Zuma 区间dp
1032: [JSOI2007]祖码Zuma Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1105 Solved: 576 [Submit][S ...
- Codeforces Beta Round #91 (Div. 2 Only) A. Lucky Division【暴力/判断是不是幸运数字4,7的倍数】
A. Lucky Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Python的工具包[2] -> matplotlib图像绘制 -> matplotlib 库及使用总结
matplotlib图像绘制 / matplotlib image description 目录 关于matplotlib matplotlib库 补充内容 Figure和AxesSubplot的生 ...
- 洛谷——P2026 求一次函数解析式
P2026 求一次函数解析式 题目背景 做数学寒假作业的怨念…… 题目描述 给定两个整点的坐标,求它们所在直线的函数解析式(一次函数). 输入输出格式 输入格式: 输入共两行. 第一行有两个整数x1, ...
- Web/JAVA 简单题目汇总
[Java标识符,变量.常量] 一.Java合法标识符命名规则 (1)区分字母大小写,标识符长度不限 (2) 英文,Unicode码双字节文字字符(日文,韩文,中文),数字,下划线,$(美元符号)均 ...