As we start building out more complex GraphQL schemas, certain fields start to repeat across different types. This is a perfect use-case for the Interface Type made available to us through GraphQL’s Type System. In this video, we’ll go over how to create an Interface Type and how to add it to an existing type in a GraphQL Schema.

Add an interface for video ID:

const {
GraphQLInterfaceType, GraphQLNonNull, GraphQLID
} = require('graphql');
const { videoType } = require('../schema'); const IDInterface = new GraphQLInterfaceType({
name : 'IDNode',
fields : {
id : {
type : new GraphQLNonNull(GraphQLID)
}
},
resolveType : (object) => {
if( object.title ) {
return videoType;
} return null;
}
}); module.exports = IDInterface;

Add interface for query:

const express     = require('express');
const graphqlHttp = require('express-graphql');
const {
getVideoById, getVideos, createVideo
} = require('./data/index');
const IDInterface = require('./interfaces/index');
const server = express();
const port = process.env.PORT || ; const {
GraphQLSchema, GraphQLObjectType, GraphQLInputObjectType, GraphQLString, GraphQLList, GraphQLInt, GraphQLNonNull, GraphQLBoolean, GraphQLID
} = require('graphql'); const videoType = new GraphQLObjectType({
name : 'video',
description : 'A video on Egghead.io',
fields : {
id : {
type : new GraphQLNonNull(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'
}
},
interfaces: [IDInterface]
});
exports.videoType = videoType; const videoInputType = new GraphQLInputObjectType({
name : 'videoInput',
fields : {
title : {
type : new GraphQLNonNull(GraphQLID),
description : 'The title of the video'
},
duration : {
type : new GraphQLNonNull(GraphQLInt),
description : 'The duration of the video'
},
watched : {
type : new GraphQLNonNull(GraphQLBoolean)
}
}
}); const mutationType = new GraphQLObjectType({
name : 'Mutation',
description : 'The root Mutation type',
fields : {
createVideo : {
type : videoType,
args : {
video : {
type : new GraphQLNonNull(videoInputType),
},
},
resolve : (_, args) => {
return createVideo(args.video)
}
}
}
}); 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)
}
}
}); const schema = new GraphQLSchema({
query : queryType,
mutation : mutationType
}); server.use('/graphql', graphqlHttp({
schema,
graphiql : true, // use graphiql interface
})); server.listen(port, () => {
console.log(`Listening on http`)
})

最新文章

  1. linux 拨号+squid监控脚本
  2. ./configure会报错:pr command not found
  3. python中get、post数据
  4. [HDOJ2546] 饭卡 (01背包)
  5. 【翻译】Zakas解答Baranovskiy的JavaScript测验题
  6. phoneGap开发环境搭建(android)
  7. OpenStack Block Storage安装配置use LVM
  8. MYSQL 关闭二进制日志
  9. setsockopt角色
  10. IIS SSL取消证书合法性验证
  11. github fork, star and watch
  12. Ubuntu16.04下Office替代品Office Online
  13. pytest 15 fixture之autouse=True
  14. Locust:简介和基本用法
  15. 使用Flink实现索引数据到Elasticsearch
  16. POJ-1860.CurrencyExchange(Spfa判断负环模版题)
  17. python 输入一个字符串,打印出它所有的组合
  18. Thread类相关方法
  19. avalonJS-源码阅读(二)
  20. 机器学习之路: python 实践 提升树 XGBoost 分类器

热门文章

  1. 洛谷——P1781 宇宙总统
  2. asp.net Code学习二(使用vs 2015 update 3)
  3. javascript之Ajax起步
  4. 怎样让索引仅仅能被一个SQL使用
  5. 全球可信并且唯一免费的HTTPS(SSL)证书颁发机构:StartSSL
  6. TextView-属性大全(设置超链接颜色)
  7. js进阶 14-8 表单序列化函数serializeArray()和serialize()的区别是什么
  8. MyBatis学习总结(13)——Mybatis查询之resultMap和resultType区别
  9. (转)RMAN-06054: media recovery requesting unknown archived log for thread...
  10. 在Java中,return null 是否安全, 为什么?