node 服务端
代码代码抽取层次分为html
schema
定义node
类型
业务对象
查询
更改
resolvers
实现git
查询
方法
typeResolvers
实现github
自定义类型
标准类型
utils
工具npm
mock
. ├── index.js | 主程序 ├── resolvers | 查询 更新 实现 │ ├── Mutations.js | 更新 │ ├── Querys.js | 查询 │ └── index.js | 入口 ├── schema | 模型定义 │ ├── business | 业务定义 │ │ ├── comment.graphql | 评论 │ │ └── post.graphql | 文章 │ └── std | 类型定义 │ ├── Mutation.graphql | 更新 │ ├── Query.graphql | 查询 │ ├── enum.graphql | 枚举 │ ├── interface.graphql | 接口 │ ├── scalar.graphql | 自定义类型 │ └── union.graphql | 联合 ├── typeResolvers | 类型实现 │ ├── index.js | 入口 │ ├── interfaces.js | 接口 │ ├── scalars.js | 自定义类型 │ ├── types.js | 业务类型 │ └── unions.js | 联合 └── utils | 工具 └── mock.js | 模拟
说明bash
Graphql
组件层次来切分的,适合小业务项目子业务模块
的目录schema
定义采用文件扩展名 .graphql
规范点总有好处的,你能够写成 .gpl
或者 .graphql
模块化
VSCode
语法支持插件 GraphQL for VSCode
我主要用语法高亮
require-graphql-file
支持模块化 .graphql
文件npm i -S require-graphql-file
index.js
import requireGraphQLFile from 'require-graphql-file' ... // 读取 schema const typeDefs = [ requireGraphQLFile('./schema/std/scalar'), // requireGraphQLFile('./schema/std/enum'), // requireGraphQLFile('./schema/std/interface'), // requireGraphQLFile('./schema/std/union'), requireGraphQLFile('./schema/business/post'), requireGraphQLFile('./schema/business/comment'), requireGraphQLFile('./schema/std/Query'), requireGraphQLFile('./schema/std/Mutation') ] ... // 合并 schema const schema = makeExecutableSchema({ typeDefs, ... })
其实就是读取文本
文件,而后合并了传参给makeExecutableSchema
schema
定义按 业务类型
和 组件类型
切分工具
业务类型
的文件名都是有意义的业务名称 Post
Comment
组件类型
的名字能够很肯定 enum
interface
union
scalar
方便查找├── schema | 模型定义 │ ├── business | 业务定义 │ │ ├── comment.graphql | 评论 │ │ └── post.graphql | 文章 │ └── std | 类型定义 │ ├── Mutation.graphql | 更新 │ ├── Query.graphql | 查询 │ ├── enum.graphql | 枚举 │ ├── interface.graphql | 接口 │ ├── scalar.graphql | 自定义类型 │ └── union.graphql | 联合
代码参考post
见上文学习
分解器
代码我认为 分解器
代码有两种
Query查询
Mutations更新
, 我都放在 resolvers
目录interface
scalar
type
union
, 都放在 typeResolvers
目录├── resolvers | 查询 更新 实现 │ ├── Mutations.js | 更新 │ ├── Querys.js | 查询 │ └── index.js | 入口 ├── typeResolvers | 类型实现 │ ├── index.js | 入口 │ ├── interfaces.js | 接口 │ ├── scalars.js | 自定义类型 │ ├── types.js | 业务类型 │ └── unions.js | 联合
代码参考
import resolvers from './resolvers' import typeResolvers from './typeResolvers' ... // 合并 schema const schema = makeExecutableSchema({ typeDefs, resolvers, typeResolvers, resolverValidationOptions: { requireResolversForResolveType: false } })
resolvers
typeResolvers
目录下写了 index.js
进行合并mock
mock
数据如何模拟请移步 06-graphql-采用-mockjs-mock数据
# 请求 { posts { id title content author addtime comments { id message author addtime } } } # 输出 { "data": { "posts": [ { "id": 90932, "title": "十分长现维整", "content": "常气火热成或", "author": "文霞", "addtime": "10:21:14", "comments": [ { "id": 30874, "message": "十称军题片格员主实", "author": "局反重间半何", "addtime": "22:05:28" }, { "id": 7088, "message": "克此快候己林层省", "author": "备从当提属", "addtime": "15:45:27" }, { "id": 74989, "message": "部织温制流统响教广", "author": "领重你包计", "addtime": "03:06:03" }, { "id": 24501, "message": "识消己满军子酸", "author": "调始相内向中取造还比", "addtime": "05:15:38" }, { "id": 91765, "message": "重开流报着色运党快但", "author": "儿动心里放改声口立", "addtime": "04:53:28" }, { "id": 48731, "message": "气即感共就林始", "author": "进在层只做", "addtime": "10:29:44" } ] }, { "id": 67359, "title": "开被几此图位便目做", "content": "应众级向由把", "author": "孟杰", "addtime": "16:49:30", "comments": [ { "id": 28432, "message": "大命格我就战历", "author": "亲但美在号场米商", "addtime": "13:18:11" }, { "id": 36258, "message": "红条由近的备级", "author": "自团人老以音状装指号", "addtime": "13:17:17" }, { "id": 90117, "message": "么整片外习达离", "author": "满步该律特", "addtime": "16:10:35" }, { "id": 14731, "message": "场等九百市很安", "author": "白石价和林风农", "addtime": "18:33:41" }, { "id": 23523, "message": "二面化权文及照万", "author": "运现里新政过铁", "addtime": "17:51:59" }, { "id": 77423, "message": "划群低矿流按看实养", "author": "个为然工些效", "addtime": "21:56:41" } ] } ] } }