博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[GraphQL] Use GraphQL's Object Type for Basic Types
阅读量:6658 次
发布时间:2019-06-25

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

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 our schema. In this video, we'll learn how to write GraphQL Object Types in GraphQL's Schema language, as well as how to create resolvers for them, and ultimately how to query them.

 

We are going to refactor this code to make it more readable and meanful:

const { graphql, buildSchema } = require('graphql');const schema = buildSchema(`    type Query {        id: ID,        title: String,        duration: Int,        watched: Boolean    }        type Schema{        query: Query    }`);const resolvers = {    id       : () => '1',    title    : () => 'bar',    duration : () => 180,    watched  : true};const query = `    query myFirstQuery {        id,        title,        duration,        watched    }`;graphql(schema, query, resolvers).then((result) => console.log(result)).catch(console.error)

 

'id', 'title', 'duration', 'watched' are video related. So we create a Video type.

const { graphql, buildSchema } = require('graphql');const schema = buildSchema(`    type Video {        id: ID,        title: String,        duration: Int,        watched: Boolean    }        type Query {        video: Video    }        type Schema{        query: Query    }`);const resolvers = {    video : () => ({        id       : '1',        title    : 'bar',        duration : 180,        watched  : true    })};const query = `    query myFirstQuery {        video {            id,            title,            duration,            watched        }    }`;graphql(schema, query, resolvers).then((result) => console.log(result)).catch(console.error)

 

转载地址:http://npqto.baihongyu.com/

你可能感兴趣的文章
你得小心BYOD这10个陷阱!
查看>>
Blue Coat 最新报告显示 移动端恶意攻击愈演愈烈
查看>>
HTML6 无 JavaScript 的单页应用引起一片哗然
查看>>
IXmaps揭示互联网流量是否被NSA监视
查看>>
告别黑白 从此黑夜拥有靓丽色彩 科达星光球全天候道路彩色监控
查看>>
《并行计算的编程模型》一3.4.2 分配和释放
查看>>
分论坛导航:企业用户参会分论坛专场
查看>>
联想王震宇:联想企业网盘3.5版本的功能亮点
查看>>
IBM推出Watson广告服务:认知计算将重塑营销市场?
查看>>
外资云计算入华“求和”
查看>>
蘑菇街王兴楠:面向下一代的移动开发体系
查看>>
Java Servlet 工作原理问答
查看>>
【硬创邦】跟hoowa学做智能路由(九):时区/服务/SSH
查看>>
几分钟快速入门Python
查看>>
无线路由器 安装使用的妙招
查看>>
鳄鱼还是木头? 专家提醒:APT攻防要当心“水坑”
查看>>
5G和生态构建能否支撑MEC快速发展?
查看>>
为什么企业需要SD-WAN?
查看>>
【宅客锋评】居e安和WeCool安防摄像头对比测评:都能用,但都够不友好
查看>>
德媒:苹果称中国政府要求提供源代码 公司没给
查看>>