When working with collections of things in GraphQL, we'll always reach out for the GraphQLListType. In this video, we'll learn how to use GraphQLList from the graphql package in combination with a GraphQLObject Type to create a field that returns a collection in our Schema.

We can use GraphQLList to fetch list objects:

const queryType = new GraphQLObjectType({
name: 'QueryType',
description: 'The root query type',
fields :{
videos: {
type: new GraphQLList(videoType),
resolve: getVideos
},
video: {
type: videoType,
args: {
id: {
type : new GraphQLNonNull(GraphQLID),
description: 'The id of the video'
}
},
resolve: (_, args) => getVideoById(args.id)
}
}
});

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);
}); const getVideos = () => new Promise((resolve) => resolve(videos)); exports.getVideoById = getVideoById;
exports.getVideos = getVideos;

[GraphQL] Use GraphQLList with GraphQLObject Types的更多相关文章

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

  2. GraphQL Java Demo代码

    mvn 引用GraphQL <dependency> <groupId>com.graphql-java</groupId> <artifactId>g ...

  3. 转 GraphQL Schema Stitching explained: Schema Delegation

    转自官方文档 In the last article, we discussed the ins and outs of remote (executable) schemas. These remo ...

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

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

  5. [GraphQL] Use GraphQL's Object Type for Basic Types

    We can create the most basic components of our GraphQL Schema using GraphQL's Object Types. These ty ...

  6. [GraphQL] Query GraphQL Interface Types in GraphQL Playground

    Interfaces are similar to Unions in that they provide a mechanism for dealing with different types o ...

  7. [GraphQL] Mutations and Input Types

    Sometimes, you want to resues object type when doing mutation, you can use 'input' type to help: inp ...

  8. graphql 新API 开发方式

    我们知道 GraphQL 使用 Schema 来描述数据,并通过制定和实现 GraphQL 规范 定义了支持 Schema 查询的 DSQL (Domain Specific Query Langua ...

  9. GraphQL:一种不同于REST的接口风格

    从去年开始,JS算是完全踏入ES6时代.在React相关项目中接触到了一些ES6的语法.这次接着GraphQL这种新型的接口风格,从后端的角度接触ES6. 这篇文章从ES6的特征讲起,打好语法基础:然 ...

随机推荐

  1. WdatePicker日期控件的使用

    将压缩包中的文件连带文件夹添加到项目中去,注意要完整的添加到项目中去,不要更改了其目录结构 然后在aspx页面中直接使用即可: 首先引入: <script src="/Controls ...

  2. gulp几个常见问题及解决方案

    1. 找不到local gulp 报错代码: $ gulp [23:29:31] Local gulp not found in [23:29:31] Try running: npm install ...

  3. django 简单会议室预约(3)

    URL配置: 今天配置一下URL,打开urls.py配置如下: from django.conf.urls import patterns, include, url from djapp impor ...

  4. vue2.0实现银行卡类型种类的选择

    功能效果:vue2.0实现银行卡类型种类的选择 图片.png 参考代码如下: <template> <div class="app"> <header ...

  5. 【Educational Codeforces Round 35 B】Two Cakes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从小到大枚举x. 看看a/x+b/x是不是大于等于n 是的话. 看看是不是两种蛋糕都能凑一堆. 也即x的最大枚举量是min(a,b) ...

  6. 【Uva 11080】Place the Guards

    [Link]: [Description] 一些城市,之间有道路相连,现在要安放警卫,警卫能看守到当前点周围的边,一条边只能有一个警卫看守,问是否有方案,如果有最少放几个警卫. [Solution] ...

  7. Codeforces Round #277.5 解题报告

    又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...

  8. LINUX设备驱动程序笔记(三)字符设备驱动程序

          <一>.主设备号和次设备号        对字符设备的訪问时通过文件系统内的设备名称进行的.那些设备名称简单称之为文件系统树的节点,它们通常位于/dev文件夹. 字符设备驱动程 ...

  9. graphicview和widgets没本质区别。它只是更轻量级,更灵活,性能更高的widgets

    graphicview和widgets没本质区别.它只是更轻量级,更灵活,性能更高的widgets.核心就是把widgets变成了更轻量级的graphicitem,把QWidget的各种事件转换成了g ...

  10. Android Studio将Eclipse的项目作为module的依赖库

    情形: 我们现在有一个eclipse的项目,我们想把这个项目作为android studio的module的一个依赖库. 以前我们在eclipse的时候常常是在一个工作区里面把一个A工程 as a l ...