我以前没有遇到过,但是简单的1个突变将创建2个条目:

scheme.json:

    {
      "name": "createAdminConfigCategory",
      "description": "Creates a single `AdminConfigCategory`.",
      "args": [
        {
          "name": "input",
          "description": "The exclusive input argument for this mutation. An object type, make sure to see documentation for this object’s fields.",
          "type": {
            "kind": "NON_NULL",
            "name": null,
            "ofType": {
              "kind": "INPUT_OBJECT",
              "name": "CreateAdminConfigCategoryInput",
              "ofType": null
            }
          },
          "defaultValue": null
        }
      ],
      "type": {
        "kind": "OBJECT",
        "name": "CreateAdminConfigCategoryPayload",
        "ofType": null
      },
      "isDeprecated": false,
      "deprecationReason": null
    },
    {
      "kind": "INPUT_OBJECT",
      "name": "CreateAdminConfigCategoryInput",
      "description": "All input for the create `AdminConfigCategory` mutation.",
      "fields": null,
      "inputFields": [
        {
          "name": "clientMutationId",
          "description": "An arbitrary string value with no semantic meaning. Will be included in the\npayload verbatim. May be used to track mutations by the client.",
          "type": {
            "kind": "SCALAR",
            "name": "String",
            "ofType": null
          },
          "defaultValue": null
        },
        {
          "name": "adminConfigCategory",
          "description": "The `AdminConfigCategory` to be created by this mutation.",
          "type": {
            "kind": "NON_NULL",
            "name": null,
            "ofType": {
              "kind": "INPUT_OBJECT",
              "name": "AdminConfigCategoryInput",
              "ofType": null
            }
          },
          "defaultValue": null
        }
      ],
      "interfaces": null,
      "enumValues": null,
      "possibleTypes": null
    },
    {
      "kind": "OBJECT",
      "name": "CreateAdminConfigCategoryPayload",
      "description": "The output of our create `AdminConfigCategory` mutation.",
      "fields": [
        {
          "name": "clientMutationId",
          "description": "The exact same `clientMutationId` that was provided in the mutation input,\nunchanged and unused. May be used by a client to track mutations.",
          "args": [],
          "type": {
            "kind": "SCALAR",
            "name": "String",
            "ofType": null
          },
          "isDeprecated": false,
          "deprecationReason": null
        },
        {
          "name": "adminConfigCategory",
          "description": "The `AdminConfigCategory` that was created by this mutation.",
          "args": [],
          "type": {
            "kind": "OBJECT",
            "name": "AdminConfigCategory",
            "ofType": null
          },
          "isDeprecated": false,
          "deprecationReason": null
        },
        {
          "name": "query",
          "description": "Our root query field type. Allows us to run any query from our mutation payload.",
          "args": [],
          "type": {
            "kind": "OBJECT",
            "name": "Query",
            "ofType": null
          },
          "isDeprecated": false,
          "deprecationReason": null
        },
        {
          "name": "adminConfigCategoryEdge",
          "description": "An edge for our `AdminConfigCategory`. May be used by Relay 1.",
          "args": [
            {
              "name": "orderBy",
              "description": "The method to use when ordering `AdminConfigCategory`.",
              "type": {
                "kind": "LIST",
                "name": null,
                "ofType": {
                  "kind": "NON_NULL",
                  "name": null,
                  "ofType": {
                    "kind": "ENUM",
                    "name": "AdminConfigCategoriesOrderBy",
                    "ofType": null
                  }
                }
              },
              "defaultValue": "[PRIMARY_KEY_ASC]"
            }
          ],
          "type": {
            "kind": "OBJECT",
            "name": "AdminConfigCategoriesEdge",
            "ofType": null
          },
          "isDeprecated": false,
          "deprecationReason": null
        }
      ],
      "inputFields": null,
      "interfaces": [],
      "enumValues": null,
      "possibleTypes": null
    },


突变前的数据:

{
  "data": {
    "allAdminConfigCategories": {
      "edges": []
    }
  }
}


突变:

  mutation something($name: String!) {
    createAdminConfigCategory(input: { adminConfigCategory: { name: $name } }) {
      query {
        allAdminConfigCategories {
          edges {
            node {
              id
            }
          }
        }
      }
    }
  }


变数

{ "name": "Jamie" }


突变后的数据:

{
  "data": {
    "createAdminConfigCategory": {
      "query": {
        "allAdminConfigCategories": {
          "edges": [
            {
              "node": {
                "id": 42
              }
            },
            {
              "node": {
                "id": 43
              }
            }
          ]
        }
      }
    }
  }
}


我在graphiql中执行以下查询:

javascript - Graphql创建1个突变的2个条目-LMLPHP

最佳答案

您不会在此处创建两个条目;您的突变:

mutation something($name: String!) {
  createAdminConfigCategory(input: { adminConfigCategory: { name: $name } }) {
    query { # < Here's the issue
      allAdminConfigCategories {
        edges {
          node {
            id
          }
        }
      }
    }
  }
}


正在请求突变有效负载上的query字段,这使您可以再次访问整个GraphQL模式。然后,您可以使用它查询allAdminConfigCategories,这将为您提供所有您可以看到的管理配置类别(而不仅仅是您刚刚创建的类别)。

您想要的突变可能更像这样:

mutation something($name: String!) {
  createAdminConfigCategory(input: { adminConfigCategory: { name: $name } }) {
    adminConfigCategory {
      id
    }
  }
}


在这里,我们只是直接从变异有效负载中查询新创建的AdminConfigCategory。

关于javascript - Graphql创建1个突变的2个条目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57496403/

10-12 12:34