博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[GraphQL] Use Arguments in a GraphQL Query
阅读量:4317 次
发布时间:2019-06-06

本文共 2539 字,大约阅读时间需要 8 分钟。

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 || 3000;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: 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);});exports.getVideoById = getVideoById;

 

In the web interface, enter the query:

{  video (id: "b"){    id    title    duration    watched  }}

 

转载于:https://www.cnblogs.com/Answer1215/p/6239012.html

你可能感兴趣的文章
数据库三大范式
查看>>
工作总结之二:bug级别、优先级别、bug状态
查看>>
访问修饰符、封装、继承
查看>>
更换pip源到国内镜像,提升pip下载速度.
查看>>
POJ 2265 Bee Maja (找规律)
查看>>
Kendo MVVM 数据绑定(七) Invisible/Visible
查看>>
DB Intro - MySQL and MongoDB
查看>>
Practical Mathematical Handwriting
查看>>
[zz]kvm环境使用libvirt创建虚拟机
查看>>
bzoj1059 [ZJOI2007]矩阵游戏
查看>>
JDK配置步骤
查看>>
springcloud微服务实战--笔记
查看>>
View(视图)——菜单Menu
查看>>
uva 408 Uniform Generator
查看>>
SharePoint 2010 类似人人网站内信功能实施
查看>>
CF 327E(Axis Walking-状态压缩Dp-lowbit的使用)
查看>>
object对象java 利用反射 从数据库取出数据对象list 类似hibernate
查看>>
插入返回ibatis 的selectKey 实现插入数据后获得id
查看>>
vim 程序编辑器
查看>>
LIS(单调队列优化 C++ 版)(施工ing)
查看>>