[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基 ...
随机推荐
- Vmware Vsphere WebService SDK开发(第一讲)-基本知识学习
刚开始这方面开发的时候,不知道如何下手,能够查到的资料特别少,而且看到很多网友和我一样也在找这方面的资料.接下来的一段时间我就结合自己所参与的项目,完成关于Vmware Vsphere WebServ ...
- EmberJs之3W
写在前面 最近比较忙,换了新工作还要学习很多全新的技术栈,并给自己找了很多借口来不去坚持写博客.常常具有讽刺意味的是,更多剩下的时间并没有利用而更多的是白白浪费,也许这就是青春吧,挥霍吧,这不是我想要 ...
- REST API 安全设计指南
0x01 REST API 简介 REST的全称是REpresentational State Transfer,表示表述性无状态传输,无需session,所以每次请求都得带上身份认证信息.rest是 ...
- 初识ASP.NET 5中的Sake与KoreBuild
从github上签出基于ASP.NET 5的MVC 6的源代码进行编译,发现有2个编译命令: build.cmd是针对Windows的编译命令,build.sh是针对Mac/Linux的编译命令,这本 ...
- 2014年市场需求排名前10的编程语言 - 生命的延续是 BI
开篇介绍 2014年就快收尾了,Team 内部每人都会准备一些 Tech Talk 的内容,技术方面的,咨询方面的都可以.我就准备了一些有关 BI 排名,BI 报表排名,包括各种技术编程语言等相关排名 ...
- MVC中用Jpaginate分页 So easy!(兼容ie家族)
看过几款分页插件,觉得Jpaginate比较简约,样式也比较容易的定制,而且体验也比较好,支持鼠标滑动效果.先上效果图: 整个过程很简单,只需要3步 一.引入相关样式和脚本: 1.MVC4中,用了Bu ...
- Spirng quartz 整合
以下是资料来源: quartz maven confighttp://quartz-scheduler.org/downloads Spring 定时器(Timer,Quartz)http://sup ...
- .net对象序列化解析
一.二进制格式器(Binary Formatter) vs XML格式器(XML Formatter): 下面我先向大家介绍两种不同的格式器,分别用它们如何实现序列化机制和反序列化机制,请看下面的代码 ...
- paip.提升性能---string split
paip.提升性能---string split 大概一万次就能看到慢的兰.. /////split 慢的原因.因为使用了正则表达式的,这样,就慢的了.. 作者Attilax 艾龙, EMAIL: ...
- 一些新的web性能优化技术
1.IconFont:图标字体,这是近年来新流行的一种以字体代替图片的技术.它可以适应任何分辨率而不会出现图片模糊问题,与图片相比它具有更小的容量,更高的灵活性(像字体一样可以设置图标大小.颜色.透明 ...