graphql-yoga interface && union 使用
接口就是一个约定,方便数据的约定,union 可以实现数据类型的共享,减少代码量
基本项目
参考 https://github.com/rongfengliang/graphql-yoga-dockerdemo
- 项目结构
├── Jenkinsfile
├── README.md
├── app.js
├── docker-compose.yml
├── dockerfile
├── package.json
├── schema.graphql
└── yarn.lock
interface && union 使用
- interface app.js
const { GraphQLServer } = require('graphql-yoga')
const typeDefs = `schema.graphql`
const resolvers = {
Applogin:{
__resolveType(obj) {
if (obj.type==1) {
return 'MobileApplogin'
} else {
return 'DesktopApplogin'
}
}
},
SearchResult:{
__resolveType(obj) {
if (obj.type) {
return 'App'
} else {
return 'LocalUser'
}
}
},
Query: {
hello: function(_,name){
return {
name:`${name.name}`,
age:33
}
},
localtype:function(parent,ob){
return {
name:`${ob.name}`,
age: ob.name.length
}
},
login:function(parent,ob){
if (ob.name=="dalong"){
return {
name:"dalong",
account:"app mobile info"
}
}
else {
return {
name:"dalong",
type:44,
account:"app mobile info"
}
}
},
appsearch:function(parent,ob){
return {
name:"demoaopp",
age:333,
type:55
}
},
appinfo:function(parent,ob){
console.log(ob,parent)
return {
name:ob.name,
type:3
}
},
listOfStrings:function(parent){
return [
"dalong",
"demoapp",
"rong"
]
}
},
Mutation:{
addUser:function(parent,ob){
console.log(ob)
return JSON.stringify(ob)
}
}
}
const options = {
port: 8000,
endpoint: '/graphql',
subscriptions: '/subscriptions',
playground: '/playground',
}
const server = new GraphQLServer({ typeDefs, resolvers })
server.start(() => console.log('Server is running on localhost:4000'))
- schema
type LocalUser {
name: String
age: Int
}
interface Applogin {
name:String
account:String
}
type MobileApplogin implements Applogin{
type:Int
name:String
account:String
}
type DesktopApplogin implements Applogin{
type:Int
name:String
account:String
}
union SearchResult = App|LocalUser
input MyUser {
name:String
age:Int
}
type Query {
hello(name: String): User!
localtype(name:String):LocalUser!
listOfStrings: [String]!
# interface demo
login(name:String):Applogin
# for directive demo
appinfo(name:String,flag:Boolean):App
# union demo
appsearch(name:String):SearchResult
}
# app info
type App {
# app name
name:String
# app type
type:Int
}
type Mutation{
addUser(input:MyUser):String
}
scalar User {
name: String
age: Int
}
参考效果
- interface

- union

参考资料
https://github.com/rongfengliang/graphql-yoga-dockerdemo
http://graphql.cn/learn/schema/#interfaces
graphql-yoga interface && union 使用的更多相关文章
- [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学习之原理篇
前言 在上一篇文章基础篇中,我们介绍了GraphQL的语法以及类型系统,算是对GraphQL有个基本的认识.在这一篇中,我们将会介绍GraphQL的实现原理.说到原理,我们就不得不依托于GraphQL ...
- 深入GraphQL 的使用语法
深入GraphQL 的使用语法 对于GraphQL 的使用语法在上一节中已经大概介绍了基本的使用方式了,这一篇将会对上一篇入门做拓展,努力将所有的使用语法都覆盖到. 1. 终端语法 首先是介绍在前端查 ...
- [php-src]窥探Php内核中的数组与面向对象
内容均以php5.6.14为例. 扩展中定义一个类有以下四步: #1. 声明一个存储类信息的指针. zend_class_entry *errs_ce; #2. 定义方法的参数信息,类的方法实现. Z ...
- iOS:消除项目中警告
引言: 在iOS开发过程中, 我们可能会碰到一些系统方法弃用, weak.循环引用.不能执行之类的警告. 有代码洁癖的孩子们很想消除他们, 今天就让我们来一次Fuck 警告!! 首先学会基本的语句: ...
- 【转】clang warning 警告清单(备查,建议直接command + F 速查 )
Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belon ...
- 使用#pragma阻止一些warnings
这篇博客的内容都是记的网上的.是流水账.只是记录下来以便日后之有,避免每次重新google. #pragma除了可以用来把不同功能的代码进行分隔组织外还可以用来disable一些warnings.这在 ...
- linux编程获取本机网络相关参数
getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程 ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...
- php类的实现
zend_class_entry typedef struct _zend_class_entry zend_class_entry; struct _zend_class_entry { char ...
随机推荐
- Python 安装pytz
1. https://pypi.org/project/pytz/#files 2. 下载上图标黄的文件, 3. pip install 4. from pytz import ...
- python webdriver 显示等待-自动登录126邮箱,添加联系人
脚本内容:#encoding=utf-8#author-夏晓旭from selenium import webdriverimport timefrom selenium.webdriver.supp ...
- 【转载】package-info
本文是转载,原文地址:http://strong-life-126-com.iteye.com/blog/806246 package-info.java对于经常使用外部包的程序员来说应该是熟悉陌生人 ...
- SQL学习笔记一之初识数据库
阅读目录 一 数据库管理软件的由来 二 数据库概述 三 mysql介绍 四 下载安装 五 mysql软件基本管理 六 初识sql语句 一 数据库管理软件的由来 基于我们之前所学,数据要想永久保存,都是 ...
- 微信小程序:工具配置 project.config.json
微信小程序:工具配置 project.config.json 一.项目配置文件project.config.json 小程序开发者工具在每个项目的根目录都会生成一个 project.config.js ...
- Puppeteer前端自动化测试实践
本篇内容将记录并介绍使用Puppeteer进行自动化网页测试,并依靠约定来避免反复修改测试用例的方案.主要解决页面众多时,修改代码导致的牵连错误无法被发现的运行时问题.文章首发于个人博客.对前端感兴趣 ...
- tomcat跟目录下work文件夹的作用
work目录只是tomcat的工作目录,也就是tomcat把jsp转换为class文件的工作目录. jsp,tomcat的工作原理:当浏览器访问某个jsp页面时,tomcat会在work目录里把这个j ...
- python应用-随机漫步
对python应用的一个巩固,以及熟悉matplotlib的用法 效果如下: # -*- coding: utf-8 -*- """ Created on Fri Sep ...
- BZOJ 4011 【HNOI2015】 落忆枫音
题目链接:落忆枫音 以下内容参考PoPoQQQ大爷的博客 首先我们先来考虑一下如果没有新加入的那条边,答案怎么算. 由于这是一个\(DAG\),所以我们给每个点随便选择一条入边,最后一定会构成一个树形 ...
- linux shell 按行循环读入文件方法
转http://blog.csdn.net/hittata/article/details/7042779 #/bin/bash printf "******************** ...