简体   繁体   English

如何在GraphQL中进行相关的突变?

[英]How to make related mutations in GraphQL?

How can we implement associated mutations in GraphQL? 我们如何在GraphQL中实现相关的突变?

I decided to make a portfolio for myself to study GraphQL and also to learn a new tool. 我决定为自己准备一份档案袋,以学习GraphQL并学习一种新工具。 I didn't have any difficulties until the moment when I needed to add an entry to my portfolio and images attached to it. 直到需要将条目添加到我的作品集和附加图像的那一刻,我才遇到任何困难。

When you add an entry to a portfolio, it has related entities that are represented as additional images. 当您将条目添加到投资组合时,它具有相关的实体,这些实体表示为其他图像。 They have a title, description, and multiple tags. 它们具有标题,描述和多个标签。 The images, like the portfolio record itself, are in different tables and are linked by the ID of the portfolio record. 图像与投资组合记录本身一样,位于不同的表中,并通过投资组合记录的ID进行链接。

Here is where the question arises: how to add an entry with additional images in one mutation and link them, and how to do it correctly? 问题出在这里:如何在一个突变中添加带有其他图像的条目并将其链接,以及如何正确进行?

I searched the forums and clear manual I have not found. 我搜索了论坛,并清除了未找到的手册。 I have a couple of ideas on how to do this: use subscriptions or perform mutations through Promise. 对于如何执行此操作,我有两个想法:使用订阅或通过Promise执行变异。 I suppose the first method is correct, but there are still doubts. 我认为第一种方法是正确的,但仍然存在疑问。 Prompt how correctly to work with such mutations. 提示如何正确处理此类突变。 Thank you for listening. 感谢您的收听。

In development I use GraphQL, Apollo, Vue and Laravel. 在开发中,我使用GraphQL,Apollo,Vue和Laravel。

Table structure: 表结构:

CREATE TABLE IF NOT EXISTS `portfolio_works` (
  `id` int(10) unsigned NOT NULL,
  `portfolio_id` int(10) unsigned NOT NULL,
  `title` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `image` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` text COLLATE utf8mb4_unicode_ci,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `portfolio_work_images` (
  `id` int(10) unsigned NOT NULL,
  `portfolio_id` int(10) unsigned NOT NULL,
  `portfolio_work_id` int(10) unsigned NOT NULL,
  `image` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `title` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

I think you are overthinking things. 我认为您想得太多。 Here is an approach: 这是一种方法:

type Portfolio {
    id: ID!
    portfolioImages: [PortfolioImages]
    title: String
    image: String
    description: String
}

type PortfolioImages {
    id: ID!
    portfolio: Portfolio
    image: String
    title: String
}

input PortfolioInput {
    id: ID!
    title: String!
    image: String!
    description: String
}

input PortfolioImagesInput {
    image: String
    title: String
}

extend type RootMutation {
    # Creates a new portfolio
    createPortfolio(item: PortfolioInput!, images: PortfolioImagesInput): Portfolio
}

Then inside your function that maps to the createPortfolio mutation you would create a MySQL transaction to create the necessary rows of data for your relationship and then COMMIT the transaction . 然后,在映射到createPortfolio突变的函数内部,您将创建一个MySQL事务以为关系创建必要的数据行,然后COMMIT事务 Example of doing a transaction: https://github.com/mysqljs/mysql#transactions 进行交易的示例: https : //github.com/mysqljs/mysql#transactions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM