Graphql

GraphQL 渐进学习 07-graphql-node-ser

2018-05-13  本文已影响37人  会煮咖啡的猫咪

GraphQL 渐进学习 07-graphql-node-server-模块化

目标

代码

目录结构

.
├── 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                             | 模拟

步骤

1. schema 定义采用文件扩展名 .graphql

规范点总有好处的,你可以写成 .gpl 或者 .graphql

2. 安装 VSCode 语法支持插件 GraphQL for VSCode

我主要用语法高亮

3. 安装 npm 插件 require-graphql-file 支持模块化 .graphql 文件

npm i -S require-graphql-file
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

4. 编写 schema 定义

├── schema                                  | 模型定义
│   ├── business                            | 业务定义
│   │   ├── comment.graphql                 | 评论
│   │   └── post.graphql                    | 文章
│   └── std                                 | 类型定义
│       ├── Mutation.graphql                | 更新
│       ├── Query.graphql                   | 查询
│       ├── enum.graphql                    | 枚举
│       ├── interface.graphql               | 接口
│       ├── scalar.graphql                  | 自定义类型
│       └── union.graphql                   | 联合

见上文

5. 编写 分解器 代码

├── 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
  }
})

6. 工具 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"
          }
        ]
      }
    ]
  }
}

参考

上一篇 下一篇

猜你喜欢

热点阅读