rps是什么| 为什么佛山有三个车牌| 客单价什么意思| 鱼龙是什么| 胃疼挂什么科室| 生理期腰疼是什么原因| 小孩口腔溃疡是什么原因| 职别是什么意思| 特别怕热爱出汗是什么原因| 农历5月17日是什么星座| 暂时无法接通是什么意思| 布吉岛什么意思| 逃出生天什么意思| 脚上为什么会长鸡眼| 觉是什么结构| 纳字五行属什么| 眼睛痒吃什么药| 什么时候立春| 属鼠的幸运色是什么颜色| 云南是什么民族| 舌根痛吃什么药好得快| 桂枝和肉桂有什么区别| 吸渣体质是什么意思| 手麻是什么原因| 高枕无忧是什么意思| 止语是什么意思| 胃火旺盛吃什么药| 螃蟹的血是什么颜色的| 烦恼是什么意思| 孕妇梦见下雨是什么意思| 什么的月亮| 薤白的俗名叫什么| 膈应人是什么意思| rt表示什么意思| 吃了拉肚子的药叫什么| 泌乳是什么意思| 打鼾是什么意思| 肚子大是什么原因造成的| 唐筛是检查什么| 正桃花是什么意思| 屎为什么是臭的| 牛肉可以炖什么| 菠菜为什么要焯水| 甲醛中毒吃什么药解毒| 画龙点睛是什么意思| 死鱼眼是什么样子的| 石英岩质玉是什么玉| 河里有什么鱼| 牡丹什么时候开放| 蓝姓是什么民族| 什么是性高潮| 朝鲜为什么闭关锁国| 金骏眉茶是什么茶| 恐龙为什么灭绝| 江小白是什么酒| 求欢是什么意思| 多巴胺是什么| 躯体化什么意思| 生小孩有什么补贴政策| 吃护肝片有什么副作用| 3.22是什么星座| 单核细胞百分比偏高是什么原因| 氨味是什么味道| 什么样的电动牙刷好| 心神不定是什么生肖| 拌凉菜需要什么调料| 什么病不能吃鲤鱼| 给朋友送什么礼物好| 什么是电汇| 早上9点到10点是什么时辰| 开通花呗有什么风险| 尿素氮肌酐比值偏高是什么原因| 溲黄是什么意思| 乌鸡不能和什么一起吃| 12580是什么号码| 二级护理是什么意思| 兵马俑是什么意思| 95年猪是什么命| 秋天是什么时候| 滑膜炎吃什么药能治好| maby什么意思| 985是什么意思| 肌酐高吃什么药| 头一直疼是什么原因| 为什么会长水痘| 汉语拼音是什么时候发明的| 大拇指脱皮是什么原因| 石青色是什么颜色| 98年属什么| 内分泌失调吃什么药效果最好| 副巡视员是什么级别| 安字属于五行属什么| 巳时是什么时间| 白发缺少什么维生素| 绿色裙子搭配什么颜色上衣| 补气血喝什么茶| 大姨夫是什么意思| 马头岩肉桂是什么茶| 印度人为什么不吃猪肉| 给孕妇送什么礼物好| 补钙有什么好处| 吃大枣有什么好处| 大便成细条状是什么病| 什么是熊猫血型| 人中深浅代表什么| 什么样的人容易得结石| 终而复始什么意思| 胃肠感冒可以吃什么水果| 子是什么属性| 利有攸往是什么意思| prp治疗是什么意思| 手指月牙代表什么意思| 局部是什么意思| cd138阳性是什么意思| 猫藓用什么药| 看到壁虎是什么征兆| 吃白饭是什么意思| 什么的天| dna是什么意思| 名分是什么意思| 为什么会得水痘| 眼镜框什么材质的好| 糟老头是什么意思| 手发抖是什么原因引起的年轻人| 阴茎不硬是什么原因| 梦见怀孕是什么预兆| 人中长痘痘什么原因| 职位是什么意思| 肚子疼拉稀是什么原因| 小马拉大车什么意思| 香菇炒什么菜好吃| 溜肉段用什么肉| 胃部间质瘤是什么性质的瘤| 春分是什么意思| 毫米后面的单位是什么| 焯水什么意思| 下午7点是什么时辰| 痔疮发痒是什么原因| 贸易壁垒是什么意思| 扁平足是什么样的| 腿硬邦邦的是什么原因| 土生土长是什么生肖| 什么是边界感| cold什么意思| 射代表什么生肖| 暑湿是什么意思| 利妥昔单抗是什么药| 牙齿酸胀是什么原因| 什么是保健品| 鲍鱼是什么| 心肌缺血吃什么补得快| 白砂糖和冰糖有什么区别| 店长的工作职责是什么| 烫伤忌口不能吃什么| 嘌呤高会引起什么症状| 高铁与动车有什么区别| gdp是什么意思| 假性宫缩是什么感觉| 农历六月六是什么节日| 什么样的春光| 宝宝睡觉出汗是什么原因| 白绫是什么意思| 佛手是什么东西| 什么可以误诊为畸胎瘤| 冠冕堂皇是什么意思| 减肥晚餐适合吃什么| 八纲辨证中的八纲是什么| 瘦西湖为什么叫瘦西湖| 舌苔白厚有齿痕是什么原因| kiv是什么意思| 老年人适合喝什么茶| 白带黄绿是什么原因| 脊髓灰质炎是什么病| 结节性红斑是什么原因引起的| 楚乔传2什么时候上映| 肠炎能吃什么食物| 臭氧是什么味道| 月经推迟量少是什么原因| 婴儿什么时候会说话| 特长是什么| who是什么组织| 荨麻疹挂什么科| 经常腰疼是什么原因女| 枸杞和什么一起泡水喝最好| aq是什么| castle是什么意思| 小腹凸起是什么原因| 双飞是什么生肖| 什么可以吃| 马桶为什么叫马桶| 晨五行属什么| 众矢之地是什么意思| 30年婚姻是什么婚| 头皮屑挂什么科| 为什么家里有蟑螂| 吃你鲍鱼是什么意思| 什么是二次元| 乸是什么意思| 被褥是什么意思| 血脂高吃什么蔬菜好| 什么是疣| 新生儿不睡觉是什么原因| 试管婴儿是什么| 餐饮sop是什么意思| 相什么无什么| wwe是什么意思| 寿终正寝是什么意思| 今年52岁属什么生肖| 相见不如怀念是什么意思| 三点水一个分读什么| 不来月经有什么危害| 儿郎是什么意思| 女性生活疼痛什么原因| sle是什么病的缩写| 红红火火是什么生肖| 什么直跳| 为什么叫八路军| 小孩改姓需要什么手续| 6月25是什么星座| 肾结石可以喝什么饮料| 卜姓氏读什么| 房早有什么危害| cook是什么意思| 6月7号什么星座| kelly是什么意思| 腰痛贴什么膏药最好| 16 5是什么码| dle是什么意思| 尿酸ua偏高是什么意思| 订盟是什么意思| 不经历风雨怎能见彩虹是什么意思| 人乳头瘤病毒是什么| 孕妇吃什么容易滑胎| 输卵管堵塞什么症状| 女人阴部黑是什么原因| 黄热病是什么病| 10月13是什么星座| 肝的反射区在什么部位| 类风湿吃什么食物好| 低血糖什么症状有哪些| 牙齿痛吃什么药| 华法林是什么药| 王允和貂蝉什么关系| 尿细菌高是什么原因| 头部爱出汗是什么原因| 国二是什么意思| 护士资格证什么时候考| 数字专辑什么意思| 阳历6月21日是什么星座| 叫什么| 自给自足是什么意思| 玫瑰糠疹是什么原因引起的| 和谐是什么| 芒果可以做什么美食| 淋球菌是什么| 医生代表什么生肖| 开火车是什么意思| 省委组织部部长什么级别| oa是什么意思| 白细胞低吃什么好| 嬴稷是秦始皇的什么人| 2003年的羊是什么命| 一什么地毯| 二甲医院是什么意思| 百度

上海举办“设计上海”展会


百度 晚明以来绘画风格流派多姿多彩,不同的画家有着不同的笔墨特征,重视这一点,就能突现不同画家的精神气质。

Apollo Server is designed to work seamlessly with MERN stack (MongoDB, Express, React, Node) applications. This tutorial shows how to add Apollo Server to an existing MERN stack project. Specifically, this tutorial demonstrates how to:

  • Run an Apollo Server instance that lets you execute GraphQL operations

  • Expose a GraphQL route in a MERN stack application

Prerequisites

This tutorial assumes that you're familiar with the command line and JavaScript. Additionally, it requires the following:

  • You've installed a recent Node.js version (v20+).

  • You've completed MongoDB's MERN stack tutorial or have your own existing MERN stack application.

    • The tutorial's code examples build off the tutorial, but you can adapt them to your application's requirements.

  • You have a MongoDB database with a records collection that has name, position, and level columns.

    • The tutorial's code examples use these column names, but you can adapt them to your database's schema.

Step 1: Install dependencies

In your server folder, run the following command to install these packages and save them in your server project's node_modules directory:

Bash
1  npm install graphql graphql-tag @apollo/subgraph @apollo/server
  • graphql is the JavaScript reference implementation for GraphQL

  • graphql-tag is a utility package to parse a GraphQL string into the standard GraphQL abstract syntax tree (AST)

  • @apollo/subgraph is a utility package for creating GraphQL microservices

  • @apollo/server is a spec-compliant GraphQL server that exposes a /graphql endpoint

Step 2: Define your GraphQL schema

Every GraphQL server (including Apollo Server) uses a schema to define the data that clients can query. The following example creates a schema for the prerequisite tutorial's records collection:

In your server's /src folder, create a schema.graphql file and paste in the following schema:

GraphQL
1type Query {
2  record(id:ID!): Record
3  records: [Record]
4}
5
6type Mutation {
7  createRecord(name: String!, position: String, level: String): Record
8  deleteRecord(id: ID!): Boolean
9  updateRecord(id: ID! name: String, position: String, level: String): Record
10}
11
12type Record {
13  id: ID
14  name: String
15  position: String
16  level: String
17}

This schema lets you perform various actions on records: fetching single or multiple records, creating new records, deleting records, and updating existing records. For more information on schema definition, check out the schema basics docs.

Step 3: Define the resolvers

Resolver functions are responsible for performing the actions defined in the schema—for example, fetching and updating records. In a MERN stack application, they're how you connect the GraphQL schema to your MongoDB instance.

In your server's /src folder, create a new resolvers.js file and paste in the following resolvers:

JavaScript
1import db from "./db/connection.js";
2
3const resolvers = {
4  Record: {
5    id: (parent) => parent.id ?? parent._id,
6  },
7  Query: {
8    async record(_, { id }) {
9      let collection = await db.collection("records");
10      let query = { _id: new ObjectId(id) };
11
12      return await collection.findOne(query);
13    },
14    async records(_, __, context) {
15      let collection = await db.collection("records");
16      const records = await collection.find({}).toArray();
17      return records;
18    },
19  },
20  Mutation: {
21    async createRecord(_, { name, position, level }, context) {
22      let collection = await db.collection("records");
23      const insert = await collection.insertOne({ name, position, level });
24      if (insert.acknowledged)
25        return { name, position, level, id: insert.insertedId };
26      return null;
27    },
28    async updateRecord(_, args, context) {
29      const id = new ObjectId(args.id);
30      let query = { _id: new ObjectId(id) };
31      let collection = await db.collection("records");
32      const update = await collection.updateOne(
33        query,
34        { $set: { ...args } }
35      );
36
37      if (update.acknowledged)
38        return await collection.findOne(query);
39
40      return null;
41    },
42    async deleteRecord(_, { id }, context) {
43      let collection = await db.collection("records");
44      const dbDelete = await collection.deleteOne({ _id: new ObjectId(id) });
45      return dbDelete.acknowledged && dbDelete.deletedCount == 1 ? true : false;
46    },
47  },
48};
49
50export default resolvers;

You may have noticed the code for each resolver function is similar to the code in your application's /record route. That's because these resolvers provide the same logic as performing CRUD operations on your records collection.

To learn more about writing resolver functions, check out the resolver docs.

Step 4: Add Apollo Server to your Express server

Now you can begin integrating Apollo Server into your Express server. Wherever you instantiate your express server (usually mern/server/server.js), import @apollo/server and its expressMiddleware. Then, instantiate and start the Apollo Server:

JavaScript
1import express from 'express';
2import cors from 'cors';
3import records from "./routes/record.js";
4
5import gql from "graphql-tag";
6import { ApolloServer } from '@apollo/server';
7import { buildSubgraphSchema } from '@apollo/subgraph';
8import { expressMiddleware } from '@as-integrations/express5';
9import resolvers from "./resolvers.js";
10import { readFileSync } from "fs";
11
12const PORT = process.env.PORT || 5050;
13const app = express();
14
15app.use(cors());
16app.use(express.json());
17
18const typeDefs = gql(
19    readFileSync("schema.graphql", {
20      encoding: "utf-8",
21    })
22  );
23
24const server = new ApolloServer({
25    schema: buildSubgraphSchema({ typeDefs, resolvers }),
26});
27// Note you must call `start()` on the `ApolloServer`
28// instance before passing the instance to `expressMiddleware`
29await server.start();
30
31app.use("/record", records);
32
33// start the Express server
34app.listen(PORT, () => {
35  console.log(`Server is running on port: ${PORT}`);
36});

Next, you'll use the middleware to integrate the previously defined resolvers into a route.

Step 5: Add the /graphql route to your server API endpoints

In the same server file, add the /graphql route:

TypeScript
1app.use("/record", records);
2// Specify the path to mount the server
3app.use(
4  '/graphql',
5  cors(),
6  express.json(),
7  expressMiddleware(server),
8);
9
10app.listen(PORT, () => {
11  console.log(`Server is running on port: ${PORT}`);
12});

This route provides access to the Apollo Server's resolver functions you previously defined. Note that the /records route hasn't been removed. That means your Express server can handle both GraphQL and RESTful routes.

Step 6: Start the server

You're ready to start your server! Run the following from your project's server directory:

Bash
1npm start

Your console output should display Server is running on port: 5050.

Step 7: Execute your first query

You can now execute GraphQL queries on the server. To execute your first query, you can use Apollo Sandbox.

Visit your MERN server in your browser at the /graphql route, which will open the Apollo Sandbox:

Apollo Sandbox

The Sandbox UI includes:

  • A URL input bar for connecting to other GraphQL servers (in the upper left)

  • Tabs for schema exploration, search, and settings (on the left)

  • An Operations panel for writing and executing queries (in the middle)

  • A Response panel for viewing query results (on the right)

To learn more about what Sandbox offers, check out the Sandbox docs.

The server supports querying the records, so let's do it! Paste this GraphQL query string for executing the records query into the Operations panel and click the run button.

GraphQL
1query GetRecords {
2  records {
3    name
4    position
5    level
6  }
7}

You should see your records appear in the Response panel.

Complete example

You can view and fork the complete server example on CodeSandbox:

Next steps

Congrats on completing the tutorial! ?? Incorporating a GraphQL server into your MERN application marks a pivotal step towards creating more efficient, flexible, and user-centric web experiences. And now that you've integrated Apollo Server into your MERN stack application, you can use GraphOS to build and scale even faster.

While this tutorial only covers the server portion of the MERN stack, the /client folder in the completed example picks up where the tutorial left off and implements @apollo/client to interact with the server. For more information on implementing the Apollo Client, head to the getting started docs.

For more hands-on learning on GraphQL and Apollo's server and client libraries, check out our interactive Odyssey tutorials. For an example that uses MongoDB Atlas in a subgraph and makes data available via the GraphOS Router, check out the Apollo Solutions repository.

note
The code in this repository is experimental and has been provided for reference purposes only.
卵巢多囊样改变是什么意思 红绿色盲是什么遗传病 澎湃的什么 小孩发烧流鼻血是什么原因 蹂躏是什么意思
55年属什么生肖 腋下疼痛什么原因 喝什么可以减肥瘦肚子 人妖是什么 乌梅是什么水果做的
夜明珠是什么东西 悠是什么意思 吃什么对子宫好 大便一粒一粒的是什么原因 严重失眠挂什么科
什么是心肌炎 屋里有蝙蝠有什么预兆 肺寒吃什么药 六味地黄丸有什么副作用 小孩咳嗽喝什么药
阴道菌群失调用什么药hcv8jop0ns2r.cn 牙冠什么材质的好fenrenren.com 微商是什么意思hcv9jop5ns6r.cn 得罪是什么意思0297y7.com 这个字叫什么hcv8jop7ns1r.cn
今年66岁属什么生肖的hcv8jop0ns8r.cn 螯合剂是什么hcv9jop1ns1r.cn 生理期不能吃什么水果hcv9jop7ns1r.cn 朝阳是什么意思hcv8jop3ns8r.cn 心理素质差是什么原因hcv8jop3ns1r.cn
手背发黄是什么原因hcv8jop2ns8r.cn 多多包涵是什么意思hcv9jop0ns3r.cn 怀孕了吃什么药能流掉hcv9jop7ns0r.cn 梅毒用什么药hcv9jop7ns1r.cn 高压偏低有什么危害hcv8jop0ns3r.cn
1998年五行属什么hcv8jop4ns7r.cn 小伙子是什么意思hcv8jop9ns7r.cn 北京居住证有什么用hcv9jop7ns1r.cn 性格是什么意思hcv9jop7ns3r.cn 蓄谋已久什么意思xinjiangjialails.com
百度 技术支持:蜘蛛池 www.kelongchi.com