前置资源

【官网】:https://www.apollographql.com/
【github】:https://github.com/apollographql

入门示例

安装命令: npm install apollo-server graphql

  1. const { ApolloServer, gql } = require('apollo-server');
  2. // 1. 定义 schema
  3. const typeDefs = gql`
  4. type Book {
  5. title: String
  6. author: String
  7. }
  8. type Query {
  9. books: [Book]
  10. }
  11. `;
  12. // 数据准备
  13. const books = [
  14. {
  15. title: 'The Awakening',
  16. author: 'Kate Chopin',
  17. },
  18. {
  19. title: 'City of Glass',
  20. author: 'Paul Auster',
  21. },
  22. ];
  23. // 2. 定义 resolver
  24. const resolvers = {
  25. Query: {
  26. books: () => books,
  27. },
  28. };
  29. // 3. 创建 server
  30. const server = new ApolloServer({ typeDefs, resolvers });
  31. // 4. 启动web服务
  32. server.listen().then(({ url }) => {
  33. console.log(`🚀 Server ready at ${url}`);
  34. });

整合express

安装命令: npm install apollo-server-express

此时你将不需要apollo-server,将其卸载 npm un apollo-server

  1. const express = require('express');
  2. const { ApolloServer, gql } = require('apollo-server-express');
  3. // 1. 定义 schema
  4. const typeDefs = gql`
  5. type Book {
  6. title: String
  7. author: String
  8. }
  9. type Query {
  10. books: [Book]
  11. }
  12. `;
  13. // 数据准备
  14. const books = [
  15. {
  16. title: 'The Awakening',
  17. author: 'Kate Chopin',
  18. },
  19. {
  20. title: 'City of Glass',
  21. author: 'Paul Auster',
  22. },
  23. ];
  24. // 2. 定义 resolver
  25. const resolvers = {
  26. Query: {
  27. books: () => books,
  28. },
  29. };
  30. async function startApolloServer() {
  31. const app = express();
  32. const server = new ApolloServer({
  33. typeDefs,
  34. resolvers,
  35. });
  36. await server.start();
  37. // 将ApolloServer和express进行整合
  38. server.applyMiddleware({ app });
  39. app.use((req, res) => {
  40. res.status(200);
  41. res.send('Hello!');
  42. res.end();
  43. });
  44. await new Promise(resolve => app.listen({ port: 4000 }, resolve));
  45. console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
  46. return { server, app };
  47. }
  48. startApolloServer()

ApolloServer resolvers参数

解析器resolver可以选择接受四个位置参数:(parent, args, context, info)

args参数

args 参数是一个对象,其中包含 GraphQL 客户端操作提供的所有 GraphQL 参数。

  1. // 1. 定义 schema
  2. const typeDefs = gql`
  3. type User {
  4. id: ID!
  5. name: String
  6. }
  7. type Query {
  8. user(id: ID!): User
  9. }
  10. `;
  11. const users = [
  12. {
  13. id: '1',
  14. name: 'Elizabeth Bennet'
  15. },
  16. {
  17. id: '2',
  18. name: 'Fitzwilliam Darcy'
  19. }
  20. ];
  21. // 2. 定义 resolver
  22. const resolvers = {
  23. Query: {
  24. user(parent, args, context, info) {
  25. return users.find(user => user.id === args.id);
  26. }
  27. },
  28. };

parent参数

parent为此字段父项的解析器的返回值(即解析器链中的前一个解析器)。对于没有父级的顶级字段的解析器(例如 Query 的字段),该值是从传递给 Apollo Server 的构造函数的 rootValue 函数中获取的。

  1. // 1. 定义 schema
  2. const typeDefs = gql`
  3. # A library has a branch and books
  4. type Library {
  5. branch: String!
  6. books: [Book!]
  7. }
  8. # A book has a title and author
  9. type Book {
  10. title: String!
  11. author: Author!
  12. }
  13. # An author has a name
  14. type Author {
  15. name: String!
  16. }
  17. # Queries can fetch a list of libraries
  18. type Query {
  19. libraries: [Library]
  20. }
  21. `;
  22. //数据准备
  23. const libraries = [
  24. {
  25. branch: 'downtown'
  26. },
  27. {
  28. branch: 'riverside'
  29. },
  30. ];
  31. const books = [
  32. {
  33. title: 'The Awakening',
  34. author: 'Kate Chopin',
  35. branch: 'riverside'
  36. },
  37. {
  38. title: 'City of Glass',
  39. author: 'Paul Auster',
  40. branch: 'downtown'
  41. },
  42. ];
  43. // 2. 定义 resolver
  44. const resolvers = {
  45. Query: {
  46. libraries() {
  47. return libraries;
  48. }
  49. },
  50. Library: {
  51. books(parent) {
  52. return books.filter(book => book.branch === parent.branch);
  53. }
  54. },
  55. Book: {
  56. author(parent) {
  57. return {
  58. name: parent.author
  59. };
  60. }
  61. }
  62. };

针对该架构的有效查询如下:

  1. query GetBooksByLibrary {
  2. libraries {
  3. books {
  4. author {
  5. name
  6. }
  7. }
  8. }
  9. }

此查询的结果解析器链与查询本身的层次结构相匹配:
image.png
这些解析器按上面显示的顺序执行,通过父参数将它们的返回值传递给链中的下一个解析器。解析器链如下所示(每个子链并行执行):
image.png