You can get a lot done in 2 minutes, like microwaving popcorn, sending a text message, eating a cupcake, and hooking up a GraphQL server.

您可以在2分钟内完成很多工作,例如微波炉爆米花,发送短信, 吃蛋糕以及连接GraphQL服务器

Yup. If you have an old Express.js RESTful API lying around or you're interested in incrementally adopting GraphQL, we only need 2 minutes to hook it up with a fresh new GraphQL Server.

对。 如果您有一个旧的Express.js RESTful API,或者对逐步采用GraphQL感兴趣,我们只需2分钟即可将其与新的GraphQL Server连接起来。

Ready? Set. Go!

准备? 组。 走!

Let's say that your server looked something like the following.

假设您的服务器如下所示。

import express from 'express';
import { apiRouter } from './router';const app = express();
const port = process.env.PORT || 5000;// Existing routes for our Express.js app
app.use('/api/v1', apiRouter);app.listen(port, () => console.log(`[App]: Listening on port ${port}`))

At the root of your project, npm install apollo-server-express as a dependency.

在项目的根目录下, npm install apollo-server-express npm install为依赖项。

npm install apollo-server-express --save

Go to where your Express app is defined and import ApolloServer and gql from apollo-server-express.

转到定义Express应用程序的位置,然后从apollo-server-express导入ApolloServergql

import { ApolloServer, gql } from 'apollo-server-express'

Next, create an instance of an ApolloServer with the simplest possible GraphQL type definitions and resolvers.

接下来,使用最简单的 GraphQL 类型定义解析器创建ApolloServer的实例。

const server = new ApolloServer({typeDefs: gql`type Query {hello: String}`,resolvers: {Query: {hello: () => 'Hello world!',},}
})

Lastly, use ApolloServer's applyMiddleware method to pass in our Express.js server.

最后,使用ApolloServer的applyMiddleware方法传递我们的Express.js服务器。

server.applyMiddleware({ app })

Boom. That's it!

繁荣。 而已!

Your code should look something like this.

您的代码应如下所示。

import express from 'express';
import { v1Router } from './api/v1';
import { ApolloServer, gql } from 'apollo-server-express'const app = express();
const port = process.env.PORT || 5000;const server = new ApolloServer({typeDefs: gql`type Query {hello: String}`,resolvers: {Query: {hello: () => 'Hello world!',},}
})server.applyMiddleware({ app })app.use('/api/v1', v1Router);app.listen(port, () => {console.log(`[App]: Listening on port ${port}`)
})

If you navigate to localhost:5000/graphql, you should be able to see your GraphQL schema in the GraphQL playground.

如果导航到localhost:5000/graphql ,则应该能够在GraphQL游乐场中看到GraphQL模式。

Note: If you want to change the URL that the GraphQL endpoint sits at from /graphql to something else, you can pass in a path option to server.applyMiddleware() with the URL you want, like path: '/specialUrl'. Check out the docs for full API usage.

注意:如果要将GraphQL端点所在的URL从/graphql为其他名称,则可以将path选项与所需URL传递给server.applyMiddleware() ,例如path: '/specialUrl' 。 查看文档以了解完整的API使用情况。

How simple was that? Is your popcorn finished?

⚡如何在2分钟内将GraphQL服务器添加到RESTful Express.js API相关推荐

  1. 服务器创建多个dhcp服务_如何在15分钟内创建无服务器服务

    服务器创建多个dhcp服务 by Charlee Li 通过李李 如何在15分钟内创建无服务器服务 (How to create a serverless service in 15 minutes) ...

  2. es6 ... 添加属性_如何在10分钟内免费将HTTPS添加到您的网站,以及为什么您现在不止需要这样做......

    es6 ... 添加属性 by Ayo Isaiah 通过Ayo Isaiah 如何在10分钟内免费将HTTPS添加到您的网站,以及为什么现在比以往更需要这样做 (How to add HTTPS t ...

  3. iis 网站添加 身份验证_在10分钟内将身份验证添加到任何网页

    iis 网站添加 身份验证 This content is sponsored via Syndicate Ads 该内容是通过辛迪加广告 赞助的 Adding authentication to w ...

  4. 如何在20分钟内批量部署20台ESXi服务器?

    如何在20分钟内批量部署20台ESXi服务器? https://mp.weixin.qq.com/s?__biz=MjM5NTk0MTM1Mw==&mid=2650642256&idx ...

  5. github创建静态页面_如何在10分钟内使用GitHub Pages创建免费的静态站点

    github创建静态页面 Static sites have become all the rage, and with good reason – they are blazingly fast a ...

  6. 以太坊区块链同步_以太坊69:如何在10分钟内建立完全同步的区块链节点

    以太坊区块链同步 by Lukas Lukac 卢卡斯·卢卡奇(Lukas Lukac) Ethereu M 69:如何在10分钟内建立完全同步的区块链节点 (Ethereum 69: how to ...

  7. 如何在5分钟内通过身份验证构建RESTful API —全部从命令行(第1部分)

    by Niharika Singh 由Niharika Singh 如何在5分钟内通过身份验证构建RESTful API -全部从命令行(第1部分) (How to Build a RESTful A ...

  8. 如何在10分钟内开始使用MongoDB

    by Navindu Jayatilake 通过纳文杜·贾亚提莱克 如何在10分钟内开始使用MongoDB (How to get started with MongoDB in 10 minutes ...

  9. 如何在1分钟内CSDN收益1000万,走上人生巅峰?!

    事情的起因源于前几日CSDN专栏作者群中有位同志自曝收益:426584.46元(不用数了42万+,未证实是否属实),瞬间刷屏. 那么作为一位普通的技术分享者,是否有机会利用开源项目短时间内赢取白富美. ...

最新文章

  1. ***:***之路的必备技能
  2. JavaWeb实现文件上传下载功能实例解析
  3. C 语言编程 — 高级数据类型 — 数组
  4. ZeroMQ接口函数之 :zmq_pgm – ØMQ 使用PGM 进行可靠的多路传输
  5. linux 切换用户_Linux 用户态切换到内核态的 3 种方式
  6. 你需要了解的有关.NET日期时间的必要信息
  7. P2371-[国家集训队]墨墨的等式【同余最短路】
  8. android源码出现的@字符代表什么意思
  9. java 循环效率_Java For循环效率测试
  10. TCL_事务控制语言
  11. 逆向某某单车-iOS客户端
  12. About abstract class.
  13. 行政管理专业考计算机研究生分数,行政管理学,考研,历年分数线是多少?
  14. 科大讯飞语音识别测试
  15. Ghost XP_sp3电脑装机终极版V9.6 【雪豹】
  16. 微PE系统盘制作 / 重装系统
  17. Python 计算变上限二重积分的数值模拟基础
  18. linux常用操作命令总结
  19. 2012年黑龙江省测绘地理信息质检人员暨国家2000大地坐标系培训合格人员名单
  20. flash在线视频播放器

热门文章

  1. 灵魂拷问!细数Android开发者的艰辛历程,成功入职阿里
  2. Python自动化开发01
  3. [设计模式]State模式
  4. windows 删除删除不掉的文件
  5. Kotlin的Lambda表达式以及它们怎样简化Android开发(KAD 07)
  6. CentOs中iptables配置允许mysql远程访问
  7. ELK7.8.1的Docker搭建过程
  8. 3.19PMP试题每日一题
  9. 004-docker常用命令[二]-容器操作ps,top,attach,export
  10. 数据处理不等式:Data Processing Inequality