[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.

原文地址:https://www.cnblogs.com/Answer1215/p/10234912.html

时间: 2024-08-30 03:22:56

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

以LeetCode为例——如何发送GraphQL Query获取数据

前言 ??GraphQL 是一种用于 API 的查询语言,是由 Facebook 开源的一种用于提供数据查询服务的抽象框架.在服务端 API 开发中,很多时候定义一个接口返回的数据相对固定,因此要获得更多信息或者只想得到某部分信息时,基于 RESTful API 的接口就显得不那么灵活.而 GraphQL 对 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具. ??目前,L

以LeetCode为例重庆幸运农场——如何发送GraphQL Query获取数据

前言GraphQL 是一种用于 API 的查询语言重庆幸运农场QQ2952777280[话仙源码论坛]hxforum.com[木瓜源码论坛]papayabbs.com ,是由 Facebook 开源的一种用于提供数据查询服务的抽象框架.在服务端 API 开发中,很多时候定义一个接口返回的数据相对固定,因此要获得更多信息或者只想得到某部分信息时,基于 RESTful API 的接口就显得不那么灵活.而 GraphQL 对 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要

harbor rest api 转graphql api

原理 实际上就是使用graphql 中的binding,首先基于swagger api 进行schema 生成,后边就是 使用binding 进行graphql 请求api 转换为rest api 请求,目前测试过两个开源的方案: prisma 的graphql-openapi-binding 以及swagger-graphql 类库 步骤 swagger 模型生成graphql schema 使用cli 工具 swagger-to-graphql npm install -g swagger-

swagger api 转graphql npm 包试用

graphql 比较方便的进行api 的查询,操作,swagger 是一个方便的open api 描述标准,当前我们有比较多的 restapi 但是转换为graphql 是有成本的,还好swagger-to-graphql 这个npm 包帮助我们简化了操作 基本项目 具体项目参考 https://github.com/rongfengliang/swagger-to-graphql-docker 项目结构 ├── Dockerfile ├── README.md ├── api │ └── s.

streamsets rest api 转换 graphql

原理很简单,就是使用swagger api 生成schema 然后代理请求处理api 调用 参考项目 https://github.com/rongfengliang/streamsets-graphql-api streamsets restapi 使用的npm 包 package.json: { "name": "restapi", "version": "1.0.0", "main": "i

[GraphQL] Query Lists of Multiple Types using a Union in GraphQL

Unions are used when we want a GraphQL field or list to handle multiple types of data. With a Union Type, we can define a field that could resolve completely different types of data. In this lesson, we will write a query that obtains a list of differ

[GraphQL] Query GraphQL Interface Types in GraphQL Playground

Interfaces are similar to Unions in that they provide a mechanism for dealing with different types of data. However, an interface is more suited for data types that include many of the same fields. In this lesson, we will query different types of pet

记一次通过c#运用GraphQL调用Github api

GraphQL是什么 .net下如何运用GraphQL 运用GraphQL调用Github api 结语 一.Graphql是什么 最近在折腾使用Github api做个微信小程序练练手,本篇文章就是在这个过程中记录. 直接先看下GraphQL的语法风格,感受一下: query { repository(owner:"octocat", name:"Hello-World") { id } } 这是最最最简单的一个运用示例,效果上等价于http://graphqla

API设计风格(RRC、REST、GraphQL、服务端驱动)

Web API设计其实是一个挺重要的设计话题,许多公司都会有公司层面的Web API设计规范,几乎所有的项目在详细设计阶段都会进行API设计,项目开发后都会有一份API文档供测试和联调.本文尝试根据自己的理解总结一下目前常见的四种API设计风格以及设计考虑点. RPC 这是最常见的方式,RPC说的是本地调用远程的方法,面向的是过程. RPC形式的API组织形态是类和方法,或者说领域和行为. 因此API的命名往往是一个动词,比如GetUserInfo,CreateUser. 因为URI会非常多而且