[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 do common operations like fetching an object by it's ID, filtering, sorting, and more. In this video, we'll update a field to take in an id argument and then learn how to use that argument in our resolve method to fetch a video by its id.
const express = require('express');
const graphqlHttp = require('express-graphql');
const { getVideoById } = require('./data/index');
const server = express();
const port = process.env.PORT || ;
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
GraphQLID
} = require('graphql');
const videoType = new GraphQLObjectType({
name: 'video',
description: 'A video on Egghead.io',
fields: {
id: {
type: GraphQLID,
description: 'The id of the video'
},
title: {
type: GraphQLString,
description: 'The title of the video'
},
duration: {
type: GraphQLInt,
description: 'The duration of the video'
},
watched: {
type: GraphQLBoolean,
description: 'Whether or no the viewer watched the video'
}
}
})
const queryType = new GraphQLObjectType({
name: 'QueryType',
description: 'The root query type',
fields :{
video: {
type: videoType,
args: {
id: {
type: GraphQLID,
description: 'The id of the video'
}
},
resolve: (_, args) => getVideoById(args.id)
}
}
});
const schema = new GraphQLSchema({
query: queryType
});
server.use('/graphql', graphqlHttp({
schema,
graphiql : true, // use graphiql interface
}));
server.listen(port, () => {
console.log(`Listening on http`)
})
data:
const videoA = {
id: 'a',
title: 'Create a GraphQL Schema',
duration: ,
watched: true,
};
const videoB = {
id: 'b',
title: 'Ember.js CLI',
duration: ,
watched: false,
};
const videos = [videoA, videoB];
const getVideoById = (id) => new Promise((resolve) => {
const [video] = videos.filter((video) => {
return video.id === id;
});
resolve(video);
});
exports.getVideoById = getVideoById;
In the web interface, enter the query:
{
video (id: "b"){
id
title
duration
watched
}
}
[GraphQL] Use Arguments in a GraphQL Query的更多相关文章
- 使用lua graphql 模块让openresty 支持graphql api
graphql 是一个很不错的api 查询标准语言,已经有一个lua 的版本支持graphql 项目使用docker&&docker-compose 运行 环境准备 模块安装 lu ...
- GraphQL介绍&使用nestjs构建GraphQL查询服务
GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接 ...
- Graphql介绍(Introduction to GraphQL)
Introduction to GraphQL GraphQL介绍 Learn about GraphQL, how it works, and how to use it in this seri ...
- 通过torodb && hasura graphql 让mongodb 快速支持graphql api
torodb 可以方便的将mongo 数据实时同步到pg,hasura graphql 可以方便的将pg 数据暴露为graphql api,集成在一起真的很方便 环境准备 docker-compose ...
- [GraphQL] Apollo React Query Component
In this lesson I refactor a React component that utilizes the graphql higher-order component to the ...
- 爬取LeetCode题目——如何发送GraphQL Query获取数据
前言 GraphQL 是一种用于 API 的查询语言,是由 Facebook 开源的一种用于提供数据查询服务的抽象框架.在服务端 API 开发中,很多时候定义一个接口返回的数据相对固定,因此要获得 ...
- SpringBoot开发秘籍 - 集成Graphql Query
概述 REST作为一种现代网络应用非常流行的软件架构风格受到广大WEB开发者的喜爱,在目前软件架构设计模式中随处可见REST的身影,但是随着REST的流行与发展,它的一个最大的缺点开始暴露出来: 在很 ...
- 使用Hot Chocolate和.NET 6构建GraphQL应用(3) —— 实现Query基础功能
系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 需求 在本文中,我们通过一个简单的例子来看一下如何实现一个最简单的GraphQL的接口. 实现 引入Hot Cho ...
- 使用Hot Chocolate和.NET 6构建GraphQL应用(4) —— 实现Query映射功能
系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 需求 在上一篇文章使用Hot Chocolate和.NET 6构建GraphQL应用(3) -- 实现Query基 ...
随机推荐
- Python成长笔记 - 基础篇 (五)
1.装饰器: 装饰器:器代表函数的意思,装饰器就是一个函数,作用是用来装饰其他的函数 原则: 1.不能修改被装饰函数的源代码 2.不能修改被装饰函数的调用方式 实现装饰器所需要的知识: 1.函数即变量 ...
- 如何卸除SDL TRADOS中的自开发插件
去年学着用SDL的例子编译了一个名为SimpleText的插件,每次打开TRADOS 2014时都要提示三次加载插件,很是烦人.但我想卸掉时,却无从下手,不知道怎么办.这个问题纠缠了我很久,今晨心性比 ...
- innodb_ft_max_token_size取值范围
根据问档中所说,innodb_ft_max_token_size取值范围为10-252,而实际上但我们在配置文件设置innodb_ft_max_token_size=252时,error log会出现 ...
- C#函数式编程之部分应用
何谓函数式编程 相信大家在实际的开发中,很多情况下完成一个功能都需要借助多个类,那么我们这里的基本单元就是类.而函数式编程则更加细化,致使我们解决一个功能的基本单元是函数,而不是类,每个功能都是由多个 ...
- 跟我一起学STL(2)——vector容器详解
一.引言 在上一个专题中,我们介绍了STL中的六大组件,其中容器组件是大多数人经常使用的,因为STL容器是把运用最广的数据结构实现出来,所以我们写应用程序时运用的比较多.然而容器又可以序列式容器和关联 ...
- 2.利用NABCD模型进行竞争性需求分析
1) N (Need 需求) 在宿舍里,舍友下载了一个比较好玩的游戏,一块好看的电影或者共享一个大体积的文件,而你又不想去重新下载,于是乎:‘’哎,win8怎么共享?‘’,‘’我的网上邻居怎么看不到你 ...
- [JS4] 最简单JS框架
<html> <head> <title></title> <SCRIPT TYPE="text/JavaScript"> ...
- clearing & settlement
http://blog.donews.com/tianshun/archive/2013/07/ http://wenku.baidu.com/view/e5a736e3e53a580217fcfe1 ...
- Html 字体大小单位 px em pt
网页上定义字体大小有常见三种单位,px.em.pt px px是pixel缩写,是基于像素的单位.在浏览网页过程中,屏幕上的文字.图片等会随屏幕的分辨率变化而变化,一个100px宽度大小的图片,在80 ...
- Backbone入门——开发第一个Backbone页面
1. 功能描述在新建的html页面中,通过导入的backbone文件搭建一个简单的mvc结构.当用户进入该页时,id号为“divTip”的<div>元素中将显示“hello,backbon ...