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的更多相关文章

  1. 使用lua graphql 模块让openresty 支持graphql api

      graphql 是一个很不错的api 查询标准语言,已经有一个lua 的版本支持graphql 项目使用docker&&docker-compose 运行 环境准备 模块安装 lu ...

  2. GraphQL介绍&使用nestjs构建GraphQL查询服务

    GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接 ...

  3. Graphql介绍(Introduction to GraphQL)

    Introduction to GraphQL  GraphQL介绍 Learn about GraphQL, how it works, and how to use it in this seri ...

  4. 通过torodb && hasura graphql 让mongodb 快速支持graphql api

    torodb 可以方便的将mongo 数据实时同步到pg,hasura graphql 可以方便的将pg 数据暴露为graphql api,集成在一起真的很方便 环境准备 docker-compose ...

  5. [GraphQL] Apollo React Query Component

    In this lesson I refactor a React component that utilizes the graphql higher-order component to the ...

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

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

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

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

  8. 使用Hot Chocolate和.NET 6构建GraphQL应用(3) —— 实现Query基础功能

    系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 需求 在本文中,我们通过一个简单的例子来看一下如何实现一个最简单的GraphQL的接口. 实现 引入Hot Cho ...

  9. 使用Hot Chocolate和.NET 6构建GraphQL应用(4) —— 实现Query映射功能

    系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 需求 在上一篇文章使用Hot Chocolate和.NET 6构建GraphQL应用(3) -- 实现Query基 ...

随机推荐

  1. Google的Bigtable学习笔记(不保证正确性)

    跪求各路大侠指正:1.首先是一个列式存储的简单数据模型的数据库,它比键值对模型/文档模型NoSQL数据库复杂点(也就更强一点).2.它的分布式存储性能依靠于GFS也就对单机房网络有硬性指标.3.它同时 ...

  2. HTTP02--Http请求头及缓存知识

    一.常用工具 Linux下:curl命令,I参数可以查看http head信息. Firefox:Firebug.HttpFox 二.常见Http Head信息 三.浏览器缓存机制 使用ctrl+F5 ...

  3. Orchard SQLite v1.7.2

    1. Upgrade NHibernate to v3.3.3 2. Add lib csharp-sqlite to support SQLite 3. Replace oracle client ...

  4. 从Mono 4.0观C# 6.0部分新特性

    Struct的默认构造函数 struct Complex { public Int32 Re { get; set; } public Int32 Im { get; set; } public Co ...

  5. [ACM_数学] Taxi Fare [新旧出租车费差 水 分段函数]

    Description Last September, Hangzhou raised the taxi fares. The original flag-down fare in Hangzhou ...

  6. spring常用jar包总结(转载)

    spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...

  7. javascript变量名提升

    预解析的过程 代码的执行过程 程序在执行过程,会先将代码读取到内存中检查,会将所有的声明在此时进行标记.所谓的标记就是让js解释器直到有这个名字,后面在使用名字的时候,不会出现未定义的错误,这个标记就 ...

  8. Beta分布和Dirichlet分布

    在<Gamma函数是如何被发现的?>里证明了\begin{align*} B(m, n) = \int_0^1 x^{m-1} (1-x)^{n-1} \text{d} x = \frac ...

  9. atitit.ajax 最佳实践跟框架选型 o99

    atitit.ajax 最佳实践跟框架选型 1. 选型框架dwr/dwr3 跟jq 1 2. DWR方便的地方分为两个地方. 1 3. dwr 优点: 1 4. 缺点: 2 5. 根jq的区别 2 1 ...

  10. atitit.获取北京时间CST 功能api总结 O7

    atitit.获取北京时间CST 功能api总结 O7 1. 获取cst时间(北京时间)两布:1.抓取url timtstamp >>format 到cst 1 2. 设置本机时间  se ...