0

I've not encountered this before, but simply 1 mutation will create 2 entries:

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 before mutation:

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

Mutation:

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

Variables

{ "name": "Jamie" }

Data after mutation:

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

I execute these queries in graphiql:

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
  • Without seeing the resolver code and other relevant backend code, we can only guess at the underlying cause. – Daniel Rearden Aug 14 '19 at 14:20
  • Interesting, I will update question. I us postgraphile, so everything is automatically generated. Updating quesiton now – Jamie Hutber Aug 14 '19 at 14:32
  • Here we go, this is everything that is my scheme.json otherwise, for backend code, there is nothing. Just the executing of the mutation inside GraphiQL – Jamie Hutber Aug 14 '19 at 14:53

1 Answers1

1

You're not creating two entries here; your mutation:

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

is requesting the query field on the mutation payload, which gives you access to the entire GraphQL schema again. You're then using that to query allAdminConfigCategories which will give you all of the admin config categories you're allowed to see (not just the one you just created).

The mutation you want is probably more like this:

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

Here we're just querying the newly created AdminConfigCategory directly from the mutation payload.

Benjie
  • 7,701
  • 5
  • 29
  • 44
  • Thank you very much @Benjie, I have to say, the only reason for using the `Query` inside the mutation was my understanding that this was how you updated the internal cache. As I've another question that means the cache does update but view isn't updating. So including query does create 2 entries for me in the DB, removing query means only 1 is added, both mutations will update the cache. – Jamie Hutber Aug 15 '19 at 09:40
  • So the main question here :D is, why does including query create 2 entries? – Jamie Hutber Aug 15 '19 at 09:41
  • For your cache to update you also need to request any fields that your cache needs to update - currently you're only fetching "id" so there's no other attributes to update. It doesn't create 2 entries, it only creates one, but then you're fetching that one and any previously created one too. – Benjie Aug 16 '19 at 09:45