Apollo 中的 GraphQL Mutations 介绍

GraphQL Mutations 是一种修改数据的 API 操作。 就像 QueryMutationGraphQL 架构

const schema = `
  type Query {
    getCount: CountResult
  }

  type Mutation {
    increment: CountResult
  }

  type CountResult {
    count: Int
    time: Float
  }
`;

每一位成员 Mutation type 是一种独特的 API 操作,可用于修改数据。 在上面的模式中,只有一个 Mutations: increment()increment() 操作返回一个类型的对象 CountResult

实现 Mutations

GraphQL 模式只是类型定义的列表。 您还需要实现的业务逻辑 increment() Mutations。与查询一样,您实现 increment() 作为 解析器 Mutation 类型:

const { ApolloServer, gql } = require(apollo-server);

let count = 0;

const schema = gql(`
  type Query {
    getCount: CountResult
  }

  type Mutation {
    increment: CountResult
  }

  type CountResult {
    count: Int
    time: Float
  }
`);

const resolvers = {
  Query: {
    getCount: () => ({ count, time: Date.now() })
  },
  // `increment` is just a resolver for the Mutation type
  Mutation: {
    increment: () => ({ count: ++count, time: Date.now() })
  },
  CountResult: {
    count: obj => obj.count,
    time: obj => obj.time
  }
};

const server = new ApolloServer({ typeDefs: schema, resolvers });
const handle = await server.listen();

const axios = require(axios);
// Call the `increment` mutation
await axios.post(handle.url, {
  query: mutation { increment { count, time } }
});

// After the `increment` mutation, `count` is now 1
const { data } = await axios.post(handle.url, {
  query: { getCount { count, time } }
});

data.data; // { getCount: { count: 1, time: 1581442587371 } }

请注意,要实际调用 Mutations,您需要使用字符串开始 GraphQL 查询 mutation

await axios.post(handle.url, {
  // Note mutation below. Not necessary for queries, but
  // necessary for mutations.
  query: mutation { increment { count, time } }
});

Mutations 参数

GraphQL Mutations 是一个与其他任何函数一样的函数。 您也可以将参数传递给您的 Mutations。 例如如果您想允许 increment() 使用 1 以外的值,您可以添加一个 Number 参数 increment() Mutations:

const schema = `
  type Query {
    getCount: CountResult
  }

  type Mutation {
    increment(num: Int): CountResult
  }

  type CountResult {
    count: Int
    time: Float
  }
`;

Apollo 将传入 Mutations 的参数作为第二个参数传递给 Mutations 的解析器函数:

increment: (obj, args) => {
  args.num; // Whatever the user passed in `increment()`
}

下面是一个完整的 increment() 实现:

let count = 0;

const schema = gql(`
  type Query {
    getCount: CountResult
  }

  type Mutation {
    increment(num: Int!): CountResult
  }

  type CountResult {
    count: Int
    time: Float
  }
`);

const resolvers = {
  Query: {
    getCount: () => ({ count, time: Date.now() })
  },
  // `increment` is just a resolver for the Mutation type
  Mutation: {
    increment: (obj, args) => {
      count += args.num;
      return { count, time: Date.now() };
    }
  },
  CountResult: {
    count: obj => obj.count,
    time: obj => obj.time
  }
};

const server = new ApolloServer({ typeDefs: schema, resolvers });
const handle = await server.listen();

let axios = require(axios);
// Call the `increment` mutation with an argument. Note that
// GraphQL arguments are named: you need to put `num: 5`, not
// just `5`.
await axios.post(handle.url, {
  query: mutation { increment(num: 5) { count, time } }
});

// After the `increment` mutation, `count` is now 5
const { data } = await axios.post(handle.url, {
  query: { getCount { count, time } }
});

data.data; // { getCount: { count: 5, time: 1581442587371 } }
© 版权声明
THE END
喜欢就支持一下吧
点赞873 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容