[GraphQL] Add an Interface to a GraphQL Schema
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`)
})
[GraphQL] Add an Interface to a GraphQL Schema的更多相关文章
- [GraphQL] Query GraphQL Interface Types in GraphQL Playground
Interfaces are similar to Unions in that they provide a mechanism for dealing with different types o ...
- [GraphQL] Write a GraphQL Schema in JavaScript
Writing out a GraphQL Schema in the common GraphQL Language can work for simple GraphQL Schemas, but ...
- 转 GraphQL Schema Stitching explained: Schema Delegation
转自官方文档 In the last article, we discussed the ins and outs of remote (executable) schemas. These remo ...
- Hasura GraphQL schema 生成是如何工作的
不像大部分的graphql 引擎,使用标准的graphql 规范的处理模型,Hasura graphql 不存在resolver 的概念(实际上是有的,只是转换为了sql语法) 以下是Hasura g ...
- graphql-inspector graphql schema比较&&文档校验&&查找破坏性变动工具
graphql-inspector 是一个方便的graphql 周边工具,可以加速graphql 应该的开发,同时可以帮助我们排查问题 包含以下特性: 进行schema 的比较 文档校验(通过sche ...
- ASP.NET Core中使用GraphQL - 第六章 使用EF Core作为持久化仓储
ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...
- stardog graphql 简单操作
预备环境: 下载stardog 软件包 graphql 查询地址 创建一个简单数据库 ./stardog-admin db create -nstarwars graphql 查询方式 http 地址 ...
- Why GraphQL is Taking Over APIs
A few years ago, I managed a team at DocuSign that was tasked with re-writing the main DocuSign web ...
- Vue + GraphQL初试
基本用法 GraphQL概述 GraphQL基本语法特性 GraphQL类型系统 GraphQL类型系统内置基础类型 GraphQL类型系统内置修饰符 GraphQL工作原理 GraphQL执行过程 ...
随机推荐
- MSSQL相关用法
一.分页查询 方式一(row_number): SELECT TOP pageSize * FROM (SELECT row_number() OVER (ORDER BY orderColumn) ...
- Linux 服务器初始配置步骤
1.IP配置 vim /etc/sysconfig/network-scripts/ifcfg- BOOTPROTO=dhcp 改成 static ONBOOT=no 改成 yes 添加 IPADDR ...
- [1,2,3].forEach(alert);这样的写法有什么利和弊吗?
以下这个问题遇到了之后.问了太阳神,以下是太阳神的解答: [1,2,3].forEach(alert);这样的写法有什么利和弊吗? 首先forEach使用方法非常easy降低代码量, 可是也有非常多地 ...
- 91.生成ini文件并写入和读取ini文件
写入 WritePrivateProfileStringA("hello money", infx[i].name, money, "1.ini"); 按照字符 ...
- JSP语法基础(一)
一.JSP页面中的凝视 (1)HTML凝视 <!-- comment [ <%=expression %> ] --> 能在client显示的一种凝视,标记内的全部JSP脚本元 ...
- Visual C# 2008 调试技巧
1,非中断模式下的调试. 利用系统“输出”窗口.(视图-输出)来打印调试信息.有Debug和Release两种版本,通过运行按钮右边的选项可以选择程序的运行方式.而对应的现实调试信息的方法也不同. ...
- CSS笔记 - fgm练习 2-8 - 简易日历
<style> *{margin: 0; padding: 0} .outer{ width: 240px; margin: 10px auto; background: #f0f0f0; ...
- JNI各种环境下编译方法及初期出错分析
转自 https://www.cnblogs.com/xyang0917/p/4172490.html 第五步.将C/C++代码编译成本地动态库文件 动态库文件名命名规则:lib+动态库文件名+后缀( ...
- Asp.NETCore让FromServices回来
起因 这两天,我忽然有点怀念 Asp.NET MVC 5 之前的时代,原因是我看到项目里面有这么一段代码(其实不止一段,几乎每个 Controller 都是) [Route("home&qu ...
- tomcat总体架构
Tomcat 总体结构图 从上图中可以看出Tomcat的心脏是两个组件:Connector 和 Container,关于这两个组件将在后面详细介绍.Connector 组件是可以被替换,这样可以提供给 ...