[GraphQL] Use GraphQLList with GraphQLObject Types

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: 120,
    watched: true,
};
const videoB = {
    id: ‘b‘,
    title: ‘Ember.js CLI‘,
    duration: 240,
    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;
时间: 2024-12-29 11:36:38

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

[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] 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 types allow us to group related fields together under a specific type, such as a Video or a User, and then allows us to fetch these types when we query

[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

[GraphQL] Create an Input Object Type for Complex Mutations

When we have certain mutations that require more complex input parameters, we can leverage the Input Object Type in GraphQL. In this video, we’ll learn how to create an Input Object Type and how to add it to a GraphQL Mutation Type. const express = r

GraphQL入门指南

Introduction GraphQL is Facebook's new query language for fetching application data in a uniform way. GraphQL并不是一个面向图数据库的查询语言,而是一个数据抽象层,包括数据格式.数据关联.查询方式定义与实现等等一揽子的东西.GraphQL也并不是一个具体的后端编程框架,如果将REST看做适合于简单逻辑的查询标准,那么GraphQL可以做一个独立的抽象层,通过对于多个REST风格的简单的接口

[GraphQL] Write a GraphQL Schema in JavaScript

Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but as our application grows, or when we start using more complex types like interfaces or unions, we find that we can’t use a GraphQL Language file in t

Graphql介绍(Introduction to GraphQL)

Introduction to GraphQL GraphQL介绍 Learn about GraphQL, how it works, and how to use it in this series of articles. Looking for documentation on how to build a GraphQL service? There are libraries to help you implement GraphQL in many different langua

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.

ASP.NET Core中使用GraphQL - 第六章 使用EF Core作为持久化仓储

ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP.NET Core中使用GraphQL - 第三章 依赖注入 ASP.NET Core中使用GraphQL - 第四章 GrahpiQL ASP.NET Core中使用GraphQL - 第五章 字段, 参数, 变量 本篇中我将演示如何配置持久化仓储,这里原文中是使用的Postgres, 这里我改用