GraphQL Apollo
Step 1: 创建项目
1 | mkdir graphql-example |
Step 2: 安装依赖
1 | npm install @apollo/index graphql |
使用JS
1 | touch index.js |
修改 package.json 添加启动
1 | { |
Step 3: 定义 GraphQL schema
index.js
1 | import { ApolloServer } from '@apollo/index'; |
Step 4: 定义数据集
1 | const books = [ |
Step 5: 定义 resolver
1 | // Resolvers 定义如何从schema中获取数据 |
Step 6: 创建服务器实例
单独使用
1 | // 用 schema 和 resolvers 创建 server |
和Express一起使用
1 | const index = new ApolloServer({typeDefs, resolvers}); |
Step 7: 运行
1 | npm start |
1 | 🚀 Server ready at: http://localhost:4000/ |
Step 8: 打开网页查询
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 QUANWEI!