[GraphQL] Use Arguments in a GraphQL Query
in use Query arguments GraphQL
2023-09-14 08:59:19 时间
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 } }
相关文章
- ORA-00316: log string of thread string, type string in header is not log file ORACLE 报错 故障修复 远程处理
- ORA-01591: lock held by in-doubt distributed transaction string ORACLE 报错 故障修复 远程处理
- ORA-02020: too many database links in use ORACLE 报错 故障修复 远程处理
- ORA-30948: Illegal in-place XML schema evolution operation: string ORACLE 报错 故障修复 远程处理
- ORA-41698: invalid use of “string” attribute in aggregate predicate ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-010391; Symbol: ER_NPIPE_PIPE_ALREADY_IN_USE; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-12042: cannot alter job_queue_processes in single process mode ORACLE 报错 故障修复 远程处理
- ORA-12542: TNS:address already in use ORACLE 报错 故障修复 远程处理
- PostgreSQL 55006: object_in_use 报错 故障修复 远程处理
- 从Redis fork主进程 Can’t save in background: fork: Cannot allocate memory && vm.overcommit_memory详解大数据
- Oracle 等待事件 Enqueues:IN,InstanceNumber 官方解释,作用,如何使用及优化方法
- Oracle中使用EXISTS代替IN(oracle替换in)
- 语法应用Oracle中IN语法的多种方式(oracle中in)
- MySQL 中的 IN 语句分析(mysql语句in)
- Understanding the accept function in Linux programming.(linuxaccept)
- Exploring the Latest Trends and Insights in Oracle ERP: Join the Forum Now!(oracleerp论坛)
- MySQL中使用USE关键字控制数据库(mysql中use关键字)
- MySQL中的IN子句如何利用索引实现高效查询(mysql中in使用索引)
- Oracle的IN走索引技术(in走索引 oracle)
- Oracle中的IN操作符使用指南(in在oracle)
- 在Oracle数据库中优化IN子句(oracle中in优化)
- 利用Oracle中的If In语句减少数据处理时间(oracle中if in)
- 从Oracle到NO IN数据库迁移指南(oracle no in)
- Oracle在区间搜寻的优越性能(oracle in 区间)
- iAI大显身手Oracle在AI领域的崛起(oracle in a)
- 详解MySQL存储过程参数有三种类型(in、out、inout)
- php中in_array函数用法探究