{"id":317,"date":"2026-11-15T11:47:37","date_gmt":"2026-11-15T03:47:37","guid":{"rendered":"https:\/\/yykcj.com\/?p=317"},"modified":"2026-11-15T11:47:37","modified_gmt":"2026-11-15T03:47:37","slug":"graphql-api-%e8%ae%be%e8%ae%a1%e4%b8%8e%e5%ae%9e%e7%8e%b0%ef%bc%9a%e4%bb%8e-schema-%e5%88%b0%e7%94%9f%e4%ba%a7%e7%8e%af%e5%a2%83%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5-3","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=317","title":{"rendered":"GraphQL API \u8bbe\u8ba1\u4e0e\u5b9e\u73b0\uff1a\u4ece Schema \u5230\u751f\u4ea7\u73af\u5883\u7684\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u9009\u62e9 GraphQL\uff1f<\/h2>\n<p>\u5728\u524d\u540e\u7aef\u5206\u79bb\u67b6\u6784\u4e2d\uff0cRESTful API \u5e38\u5e38\u9762\u4e34\u8fc7\u5ea6\u83b7\u53d6\uff08over-fetching\uff09\u6216\u83b7\u53d6\u4e0d\u8db3\uff08under-fetching\uff09\u7684\u95ee\u9898\u3002\u6bd4\u5982\u79fb\u52a8\u7aef\u53ea\u9700\u8981\u7528\u6237\u6635\u79f0\u548c\u5934\u50cf\uff0c\u5374\u4e0d\u5f97\u4e0d\u83b7\u53d6\u6574\u4e2a\u7528\u6237\u5bf9\u8c61\uff1b\u6216\u8005\u4e00\u4e2a\u9875\u9762\u9700\u8981\u8bf7\u6c42\u591a\u4e2a\u63a5\u53e3\u624d\u80fd\u62fc\u51fa\u5b8c\u6574\u6570\u636e\u3002GraphQL \u901a\u8fc7\u58f0\u660e\u5f0f\u6570\u636e\u83b7\u53d6\u89e3\u51b3\u4e86\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u5ba2\u6237\u7aef\u7cbe\u786e\u6307\u5b9a\u6240\u9700\u5b57\u6bb5\uff0c\u4e00\u6b21\u8bf7\u6c42\u83b7\u53d6\u6240\u6709\u6570\u636e\u3002<\/p>\n<p>\u4f46 GraphQL \u4e5f\u5e26\u6765\u4e86\u65b0\u7684\u6311\u6218\uff1aSchema \u8bbe\u8ba1\u3001N+1 \u67e5\u8be2\u3001\u6743\u9650\u63a7\u5236\u3001\u6027\u80fd\u76d1\u63a7\u7b49\u3002\u672c\u6587\u5c06\u5e26\u4f60\u4ece\u96f6\u5f00\u59cb\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u751f\u4ea7\u53ef\u7528\u7684 GraphQL API\uff0c\u5e76\u5206\u4eab\u6211\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u603b\u7ed3\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<h2>\u6280\u672f\u6808\u9009\u62e9<\/h2>\n<p>\u6211\u4eec\u4f7f\u7528 Node.js + Apollo Server \u4f5c\u4e3a\u670d\u52a1\u7aef\uff0c\u914d\u5408 Prisma \u4f5c\u4e3a ORM\uff0c\u6570\u636e\u5e93\u9009\u7528 SQLite\uff08\u65b9\u4fbf\u6f14\u793a\uff0c\u53ef\u65e0\u7f1d\u5207\u6362 PostgreSQL\uff09\u3002Apollo Server \u662f\u76ee\u524d\u6700\u6d41\u884c\u7684 GraphQL \u670d\u52a1\u5668\u4e4b\u4e00\uff0c\u751f\u6001\u5b8c\u5584\uff0c\u6587\u6863\u4e30\u5bcc\u3002<\/p>\n<pre><code class=\"language-bash\">\nmkdir graphql-demo &amp;&amp; cd graphql-demo\nnpm init -y\nnpm install apollo-server graphql prisma @prisma\/client\nnpm install -D prisma\nnpx prisma init\n<\/code><\/pre>\n<h2>\u6b65\u9aa4\u4e00\uff1a\u8bbe\u8ba1 Schema<\/h2>\n<p>Schema \u662f GraphQL \u7684\u6838\u5fc3\uff0c\u5b83\u5b9a\u4e49\u4e86 API \u7684\u80fd\u529b\u8fb9\u754c\u3002\u8bbe\u8ba1\u826f\u597d\u7684 Schema \u5e94\u8be5\u4ee5\u4e1a\u52a1\u9886\u57df\u4e3a\u4e2d\u5fc3\uff0c\u800c\u4e0d\u662f\u6570\u636e\u5e93\u8868\u7ed3\u6784\u3002<\/p>\n<p>\u6211\u4eec\u5148\u5b9a\u4e49\u6570\u636e\u6a21\u578b\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u535a\u5ba2\u7cfb\u7edf\uff0c\u5305\u542b\u7528\u6237\u3001\u6587\u7ae0\u548c\u8bc4\u8bba\u3002<\/p>\n<pre><code class=\"language-prisma\">\n\/\/ prisma\/schema.prisma\ndatasource db {\n  provider = &quot;sqlite&quot;\n  url      = &quot;file:.\/dev.db&quot;\n}\n\ngenerator client {\n  provider = &quot;prisma-client-js&quot;\n}\n\nmodel User {\n  id        Int      @id @default(autoincrement())\n  name      String\n  email     String   @unique\n  posts     Post[]\n  comments  Comment[]\n}\n\nmodel Post {\n  id        Int      @id @default(autoincrement())\n  title     String\n  content   String\n  published Boolean  @default(false)\n  authorId  Int\n  author    User     @relation(fields: [authorId], references: [id])\n  comments  Comment[]\n}\n\nmodel Comment {\n  id      Int    @id @default(autoincrement())\n  text    String\n  postId  Int\n  post    Post   @relation(fields: [postId], references: [id])\n  authorId Int\n  author  User   @relation(fields: [authorId], references: [id])\n}\n<\/code><\/pre>\n<p>\u8fd0\u884c <code>npx prisma migrate dev --name init<\/code> \u751f\u6210\u6570\u636e\u5e93\u548c\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u8bbe\u8ba1 GraphQL Schema\u3002\u6211\u4eec\u4f7f\u7528 SDL\uff08Schema Definition Language\uff09\u5b9a\u4e49\u7c7b\u578b\u3002<\/p>\n<pre><code class=\"language-graphql\">\n# src\/schema.graphql\ntype User {\n  id: ID!\n  name: String!\n  email: String!\n  posts: [Post!]!\n  comments: [Comment!]!\n}\n\ntype Post {\n  id: ID!\n  title: String!\n  content: String!\n  published: Boolean!\n  author: User!\n  comments: [Comment!]!\n}\n\ntype Comment {\n  id: ID!\n  text: String!\n  post: Post!\n  author: User!\n}\n\ntype Query {\n  users: [User!]!\n  user(id: ID!): User\n  posts(published: Boolean): [Post!]!\n  post(id: ID!): Post\n}\n\ntype Mutation {\n  createUser(name: String!, email: String!): User!\n  createPost(title: String!, content: String!, authorId: ID!): Post!\n  createComment(text: String!, postId: ID!, authorId: ID!): Comment!\n  publishPost(id: ID!): Post!\n}\n<\/code><\/pre>\n<p><strong>\u8bbe\u8ba1\u539f\u5219<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u975e\u7a7a\u7c7b\u578b\uff08<code>!<\/code>\uff09\u660e\u786e\u5b57\u6bb5\u662f\u5426\u53ef\u4e3a\u7a7a\uff0c\u907f\u514d\u5ba2\u6237\u7aef\u5904\u7406\u610f\u5916\u7684 null\u3002<\/li>\n<li>\u5217\u8868\u7c7b\u578b\u4f7f\u7528 <code>[Post!]!<\/code>\uff0c\u8868\u793a\u5217\u8868\u672c\u8eab\u548c\u5143\u7d20\u90fd\u975e\u7a7a\u3002<\/li>\n<li>\u67e5\u8be2\u53c2\u6570\u4f7f\u7528\u547d\u540d\u53c2\u6570\uff0c\u5982 <code>posts(published: Boolean)<\/code>\uff0c\u4fbf\u4e8e\u6269\u5c55\u3002<\/li>\n<\/ul>\n<h2>\u6b65\u9aa4\u4e8c\uff1a\u5b9e\u73b0 Resolver<\/h2>\n<p>Resolver \u662f GraphQL \u7684\u6267\u884c\u5355\u5143\u3002\u6211\u4eec\u4f7f\u7528 Apollo Server \u7684 <code>gql<\/code> \u6807\u7b7e\u548c Prisma \u5ba2\u6237\u7aef\u3002<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ src\/index.js\nconst { ApolloServer, gql } = require(&#x27;apollo-server&#x27;);\nconst { PrismaClient } = require(&#x27;@prisma\/client&#x27;);\nconst fs = require(&#x27;fs&#x27;);\nconst path = require(&#x27;path&#x27;);\n\nconst prisma = new PrismaClient();\nconst typeDefs = gql(fs.readFileSync(path.join(__dirname, &#x27;schema.graphql&#x27;), &#x27;utf8&#x27;));\n\nconst resolvers = {\n  Query: {\n    users: () =&gt; prisma.user.findMany(),\n    user: (_, { id }) =&gt; prisma.user.findUnique({ where: { id: Number(id) } }),\n    posts: (_, { published }) =&gt; {\n      const where = published !== undefined ? { published } : {};\n      return prisma.post.findMany({ where });\n    },\n    post: (_, { id }) =&gt; prisma.post.findUnique({ where: { id: Number(id) } }),\n  },\n  Mutation: {\n    createUser: (_, { name, email }) =&gt; prisma.user.create({ data: { name, email } }),\n    createPost: (_, { title, content, authorId }) =&gt;\n      prisma.post.create({ data: { title, content, authorId: Number(authorId) } }),\n    createComment: (_, { text, postId, authorId }) =&gt;\n      prisma.comment.create({ data: { text, postId: Number(postId), authorId: Number(authorId) } }),\n    publishPost: (_, { id }) =&gt;\n      prisma.post.update({ where: { id: Number(id) }, data: { published: true } }),\n  },\n  User: {\n    posts: (parent) =&gt; prisma.post.findMany({ where: { authorId: parent.id } }),\n    comments: (parent) =&gt; prisma.comment.findMany({ where: { authorId: parent.id } }),\n  },\n  Post: {\n    author: (parent) =&gt; prisma.user.findUnique({ where: { id: parent.authorId } }),\n    comments: (parent) =&gt; prisma.comment.findMany({ where: { postId: parent.id } }),\n  },\n  Comment: {\n    post: (parent) =&gt; prisma.post.findUnique({ where: { id: parent.postId } }),\n    author: (parent) =&gt; prisma.user.findUnique({ where: { id: parent.authorId } }),\n  },\n};\n\nconst server = new ApolloServer({ typeDefs, resolvers });\n\nserver.listen().then(({ url }) =&gt; {\n  console.log(`\ud83d\ude80 Server ready at ${url}`);\n});\n<\/code><\/pre>\n<p>\u542f\u52a8\u670d\u52a1\uff1a<code>node src\/index.js<\/code>\uff0c\u8bbf\u95ee <code>http:\/\/localhost:4000<\/code> \u5373\u53ef\u4f7f\u7528 GraphQL Playground \u6d4b\u8bd5\u3002<\/p>\n<h2>\u6b65\u9aa4\u4e09\uff1a\u89e3\u51b3 N+1 \u67e5\u8be2\u95ee\u9898<\/h2>\n<p>\u4e0a\u9762\u7684\u5b9e\u73b0\u6709\u4e00\u4e2a\u4e25\u91cd\u6027\u80fd\u95ee\u9898\uff1aN+1 \u67e5\u8be2\u3002\u4f8b\u5982\u67e5\u8be2 <code>users { posts { title } }<\/code>\uff0c\u4f1a\u5148\u67e5\u8be2\u6240\u6709\u7528\u6237\uff081 \u6b21\uff09\uff0c\u7136\u540e\u4e3a\u6bcf\u4e2a\u7528\u6237\u67e5\u8be2\u5176\u6587\u7ae0\uff08N \u6b21\uff09\u3002\u5f53\u7528\u6237\u91cf\u5927\u65f6\uff0c\u6570\u636e\u5e93\u538b\u529b\u5de8\u5927\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528 DataLoader\uff0c\u5b83\u901a\u8fc7\u6279\u5904\u7406\u548c\u7f13\u5b58\u5c06 N \u6b21\u67e5\u8be2\u5408\u5e76\u4e3a 1 \u6b21\u3002<\/p>\n<pre><code class=\"language-bash\">\nnpm install dataloader\n<\/code><\/pre>\n<p>\u521b\u5efa DataLoader \u5b9e\u4f8b\uff1a<\/p>\n<pre><code class=\"language-javascript\">\n\/\/ src\/loaders.js\nconst DataLoader = require(&#x27;dataloader&#x27;);\nconst { PrismaClient } = require(&#x27;@prisma\/client&#x27;);\nconst prisma = new PrismaClient();\n\nconst createLoaders = () =&gt; ({\n  userLoader: new DataLoader(async (ids) =&gt; {\n    const users = await prisma.user.findMany({ where: { id: { in: ids } } });\n    const userMap = new Map(users.map(user =&gt; [user.id, user]));\n    return ids.map(id =&gt; userMap.get(id));\n  }),\n  postsByAuthorLoader: new DataLoader(async (authorIds) =&gt; {\n    const posts = await prisma.post.findMany({ where: { authorId: { in: authorIds } } });\n    const postsMap = new Map();\n    posts.forEach(post =&gt; {\n      if (!postsMap.has(post.authorId)) postsMap.set(post.authorId, []);\n      postsMap.get(post.authorId).push(post);\n    });\n    return authorIds.map(id =&gt; postsMap.get(id) || []);\n  }),\n  commentsByAuthorLoader: new DataLoader(async (authorIds) =&gt; {\n    const comments = await prisma.comment.findMany({ where: { authorId: { in: authorIds } } });\n    const commentsMap = new Map();\n    comments.forEach(comment =&gt; {\n      if (!commentsMap.has(comment.authorId)) commentsMap.set(comment.authorId, []);\n      commentsMap.get(comment.authorId).push(comment);\n    });\n    return authorIds.map(id =&gt; commentsMap.get(id) || []);\n  }),\n});\n\nmodule.exports = { createLoaders };\n<\/code><\/pre>\n<p>\u5728 Apollo Server \u4e2d\u6ce8\u5165 loaders\uff0c\u5e76\u5728 resolver \u4e2d\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n  context: () =&gt; ({\n    loaders: createLoaders(),\n  }),\n});\n<\/code><\/pre>\n<p>\u4fee\u6539 User resolver\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nUser: {\n  posts: (parent, _, { loaders }) =&gt; loaders.postsByAuthorLoader.load(parent.id),\n  comments: (parent, _, { loaders }) =&gt; loaders.commentsByAuthorLoader.load(parent.id),\n},\n<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1aDataLoader \u7684\u6279\u5904\u7406\u51fd\u6570\u5fc5\u987b\u8fd4\u56de\u4e0e\u8f93\u5165 ids \u987a\u5e8f\u4e00\u81f4\u7684\u6570\u7ec4\uff0c\u4e14\u957f\u5ea6\u76f8\u540c\u3002\u5982\u679c\u67d0\u4e2a id \u6ca1\u6709\u5bf9\u5e94\u6570\u636e\uff0c\u5e94\u8fd4\u56de null \u6216\u7a7a\u6570\u7ec4\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/p>\n<h2>\u6b65\u9aa4\u56db\uff1a\u8ba4\u8bc1\u4e0e\u6388\u6743<\/h2>\n<p>GraphQL \u7684\u8ba4\u8bc1\u901a\u5e38\u5728 HTTP \u5c42\u901a\u8fc7 token \u5b9e\u73b0\uff0c\u6388\u6743\u5219\u5728 resolver \u4e2d\u68c0\u67e5\u3002\u6211\u4eec\u4f7f\u7528 JWT\u3002<\/p>\n<pre><code class=\"language-bash\">\nnpm install jsonwebtoken\n<\/code><\/pre>\n<p>\u5728 context \u4e2d\u89e3\u6790 token\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nconst jwt = require(&#x27;jsonwebtoken&#x27;);\nconst JWT_SECRET = &#x27;your-secret-key&#x27;;\n\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n  context: ({ req }) =&gt; {\n    const token = req.headers.authorization || &#x27;&#x27;;\n    let user = null;\n    if (token) {\n      try {\n        user = jwt.verify(token.replace(&#x27;Bearer &#x27;, &#x27;&#x27;), JWT_SECRET);\n      } catch (e) {\n        \/\/ token \u65e0\u6548\n      }\n    }\n    return { user, loaders: createLoaders() };\n  },\n});\n<\/code><\/pre>\n<p>\u5728 resolver \u4e2d\u68c0\u67e5\u6743\u9650\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nMutation: {\n  createPost: (_, { title, content, authorId }, { user }) =&gt; {\n    if (!user) throw new Error(&#x27;\u672a\u767b\u5f55&#x27;);\n    if (user.id !== Number(authorId)) throw new Error(&#x27;\u65e0\u6743\u64cd\u4f5c&#x27;);\n    return prisma.post.create({ data: { title, content, authorId: Number(authorId) } });\n  },\n},\n<\/code><\/pre>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u6743\u9650\uff0c\u53ef\u4ee5\u5c06\u6388\u6743\u903b\u8f91\u62bd\u8c61\u4e3a <code>@auth<\/code> \u6307\u4ee4\u6216\u4e2d\u95f4\u4ef6\uff0c\u907f\u514d\u5728\u6bcf\u4e2a resolver \u4e2d\u91cd\u590d\u3002<\/p>\n<h2>\u6b65\u9aa4\u4e94\uff1a\u9519\u8bef\u5904\u7406<\/h2>\n<p>GraphQL \u7684\u9519\u8bef\u5206\u4e3a\u4e24\u7c7b\uff1a\u9884\u671f\u9519\u8bef\uff08\u5982\u53c2\u6570\u6821\u9a8c\u5931\u8d25\uff09\u548c\u610f\u5916\u9519\u8bef\uff08\u5982\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\uff09\u3002\u5efa\u8bae\u4f7f\u7528\u81ea\u5b9a\u4e49\u9519\u8bef\u7c7b\uff0c\u5e76\u5728 Apollo Server \u7684 <code>formatError<\/code> \u4e2d\u7edf\u4e00\u5904\u7406\u3002<\/p>\n<pre><code class=\"language-javascript\">\nclass AuthenticationError extends Error {\n  constructor(message) {\n    super(message);\n    this.extensions = { code: &#x27;UNAUTHENTICATED&#x27; };\n  }\n}\n\nclass ForbiddenError extends Error {\n  constructor(message) {\n    super(message);\n    this.extensions = { code: &#x27;FORBIDDEN&#x27; };\n  }\n}\n\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n  context: ... ,\n  formatError: (err) =&gt; {\n    \/\/ \u751f\u4ea7\u73af\u5883\u9690\u85cf\u5185\u90e8\u9519\u8bef\u8be6\u60c5\n    if (process.env.NODE_ENV === &#x27;production&#x27; &amp;&amp; !err.extensions?.code) {\n      return new Error(&#x27;Internal server error&#x27;);\n    }\n    return err;\n  },\n});\n<\/code><\/pre>\n<p>\u5728 resolver \u4e2d\u629b\u51fa\u8fd9\u4e9b\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"language-javascript\">\nif (!user) throw new AuthenticationError(&#x27;\u8bf7\u5148\u767b\u5f55&#x27;);\nif (user.id !== Number(authorId)) throw new ForbiddenError(&#x27;\u65e0\u6743\u64cd\u4f5c&#x27;);\n<\/code><\/pre>\n<h2>\u6b65\u9aa4\u516d\uff1a\u6027\u80fd\u4f18\u5316\u4e0e\u76d1\u63a7<\/h2>\n<p>\u751f\u4ea7\u73af\u5883\u8fd8\u9700\u8981\u8003\u8651\u67e5\u8be2\u590d\u6742\u5ea6\u9650\u5236\u548c\u6027\u80fd\u76d1\u63a7\u3002<\/p>\n<p><strong>\u67e5\u8be2\u590d\u6742\u5ea6\u9650\u5236<\/strong>\uff1a\u9632\u6b62\u6076\u610f\u67e5\u8be2\uff08\u5982\u6df1\u5ea6\u5d4c\u5957\uff09\u8017\u5c3d\u670d\u52a1\u5668\u8d44\u6e90\u3002\u4f7f\u7528 <code>graphql-query-complexity<\/code> \u5e93\u3002<\/p>\n<pre><code class=\"language-bash\">\nnpm install graphql-query-complexity\n<\/code><\/pre>\n<pre><code class=\"language-javascript\">\nconst { createComplexityLimitRule } = require(&#x27;graphql-query-complexity&#x27;);\n\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n  validationRules: [\n    createComplexityLimitRule(1000, {\n      onCost: (cost) =&gt; console.log(&#x27;Query cost:&#x27;, cost),\n    }),\n  ],\n});\n<\/code><\/pre>\n<p><strong>\u6027\u80fd\u76d1\u63a7<\/strong>\uff1a\u4f7f\u7528 Apollo Server \u63d2\u4ef6\u8bb0\u5f55\u6bcf\u4e2a resolver \u7684\u8017\u65f6\u3002<\/p>\n<pre><code class=\"language-javascript\">\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n  plugins: [\n    {\n      requestDidStart() {\n        return {\n          willSendResponse({ response }) {\n            console.log(&#x27;Response:&#x27;, response);\n          },\n        };\n      },\n    },\n  ],\n});\n<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ece\u96f6\u5b9e\u73b0\u4e86\u4e00\u4e2a\u751f\u4ea7\u53ef\u7528\u7684 GraphQL API\uff0c\u5173\u952e\u70b9\u5305\u62ec\uff1a<\/p>\n<ol>\n<li><strong>Schema \u8bbe\u8ba1<\/strong>\uff1a\u4ee5\u4e1a\u52a1\u9886\u57df\u4e3a\u4e2d\u5fc3\uff0c\u4f7f\u7528\u975e\u7a7a\u7c7b\u578b\u660e\u786e\u5951\u7ea6\u3002<\/li>\n<li><strong>N+1 \u95ee\u9898<\/strong>\uff1a\u901a\u8fc7 DataLoader \u6279\u5904\u7406\u89e3\u51b3\uff0c\u8fd9\u662f GraphQL \u6027\u80fd\u7684\u5173\u952e\u3002<\/li>\n<li><strong>\u8ba4\u8bc1\u6388\u6743<\/strong>\uff1a\u5728 context \u4e2d\u89e3\u6790\u7528\u6237\uff0c\u5728 resolver \u4e2d\u68c0\u67e5\u6743\u9650\u3002<\/li>\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u9519\u8bef\u548c <code>formatError<\/code> \u7edf\u4e00\u683c\u5f0f\u3002<\/li>\n<li><strong>\u6027\u80fd\u4e0e\u5b89\u5168<\/strong>\uff1a\u9650\u5236\u67e5\u8be2\u590d\u6742\u5ea6\uff0c\u76d1\u63a7 resolver \u8017\u65f6\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u4e00\u6b65\u53ef\u4ee5\u63a2\u7d22\uff1aGraphQL \u8ba2\u9605\uff08\u5b9e\u65f6\u6570\u636e\uff09\u3001\u6301\u4e45\u5316\u67e5\u8be2\uff08\u5b89\u5168\u6027\uff09\u3001Apollo Federation\uff08\u5fae\u670d\u52a1\u67b6\u6784\uff09\u3002<\/p>\n<p>\ud83d\udca1 <strong>\u8e29\u5751\u63d0\u793a<\/strong>\uff1aDataLoader \u7684\u7f13\u5b58\u662f\u8bf7\u6c42\u7ea7\u522b\u7684\uff0c\u786e\u4fdd\u6bcf\u6b21\u8bf7\u6c42\u521b\u5efa\u65b0\u7684\u5b9e\u4f8b\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u624b\u628a\u624b\u6559\u4f60\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u751f\u4ea7\u53ef\u7528\u7684 GraphQL API\uff0c\u6db5\u76d6 Schema \u8bbe\u8ba1\u3001N+1 \u95ee\u9898\u89e3\u51b3\u3001\u8ba4\u8bc1\u6388\u6743\u3001\u9519\u8bef\u5904\u7406\u548c\u6027\u80fd\u4f18\u5316\uff0c\u5e76\u5206\u4eab\u5b9e\u9645\u9879\u76ee\u4e2d\u603b\u7ed3\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[266,265,261,5,6],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-api-","tag-apollo-server","tag-graphql","tag-node-js","tag-6"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}