verbb / tablemaker

A user-definable table field type for Craft CMS
MIT License
40 stars 32 forks source link

Do we support Graphql mutation on TableMakerField? #33

Open ce2cs opened 2 years ago

ce2cs commented 2 years ago

Question

I'm working on a project that automatically translates everything on multi-sites. Our approach is using GraphQL to mutate the content on a different site. But our TableMakerField seems like only has table field in the graphql mutation code.

Here is the code I tried:

Query:

{
  entries(siteId: 1, drafts: false, sectionId: 5, id: 216123) {
    ... on pages_pages_Entry {
      title
      contentBlocks {
        ... on contentBlocks_tableBlock_BlockType {
          table {
            __typename
            table
            columns {
              heading
              width
              align
            }
            rows
          }
        }
      }
    }
  }
}

Response:

{
  "data": {
    "entries": [
      {
        "title": "Components Demonstration",
        "contentBlocks": [
          {
            "table": {
              "__typename": "table_TableMakerField",
              "table": "\n            <table>\n                <thead>\n                    <tr>\n        <th align=\"left\" width=\"\">Store</th><th align=\"left\" width=\"\">North America</th><th align=\"left\" width=\"\">Europe</th><th align=\"left\" width=\"\">International</th>\n                    </tr>\n                </thead>\n\n                <tbody><tr><td align=\"left\">Shipping to</td><td align=\"left\">Canada, USA</td><td align=\"left\">Austria, Belgium, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark…</td><td align=\"left\">Australia, Japan, New Zealand, Singapore, Brazil</td></tr><tr><td align=\"left\">Waranty</td><td align=\"left\">One-year warranty. Tulip covers shipping costs for warranty service. See Terms. </td><td align=\"left\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td><td align=\"left\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td></tr>\n\n                </tbody>\n\n            </table>\n        ",
              "columns": [
                {
                  "heading": "Store",
                  "width": "",
                  "align": "left"
                },
                {
                  "heading": "North America",
                  "width": "",
                  "align": "left"
                },
                {
                  "heading": "Europe",
                  "width": "",
                  "align": "left"
                },
                {
                  "heading": "International",
                  "width": "",
                  "align": "left"
                }
              ],
              "rows": [
                [
                  "Shipping to",
                  "Canada, USA",
                  "Austria, Belgium, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark…",
                  "Australia, Japan, New Zealand, Singapore, Brazil"
                ],
                [
                  "Waranty",
                  "One-year warranty. Tulip covers shipping costs for warranty service. See Terms. ",
                  "One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.",
                  "One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms."
                ]
              ]
            }
          }
        ]
      }
    ]
  }
}

So according to the response, what I expect is compose a GraphQL mutation query to change the columns and rows on sites in other languages. However the only field I could use in the mutation is table. So I tried use the table html in the above response.

Here is the GraphQL mutation I tried:

mutation {
  save_pages_pages_Entry(
    id: "216123"
    contentBlocks: {
      sortOrder: [
        "230833"
      ]
      blocks: [
        { 
          tableBlock: { 
            id: "230833" 
            table: "\n            <table>\n                <thead>\n                    <tr>\n        <th align=\"left\" width=\"\">Store</th><th align=\"left\" width=\"\">North America</th><th align=\"left\" width=\"\">Europe</th><th align=\"left\" width=\"\">International</th>\n                    </tr>\n                </thead>\n\n                <tbody><tr><td align=\"left\">Shipping to</td><td align=\"left\">Canada, USA</td><td align=\"left\">Austria, Belgium, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark…</td><td align=\"left\">Australia, Japan, New Zealand, Singapore, Brazil</td></tr><tr><td align=\"left\">Waranty</td><td align=\"left\">One-year warranty. Tulip covers shipping costs for warranty service. See Terms. </td><td align=\"left\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td><td align=\"left\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td></tr>\n\n                </tbody>\n\n            </table>\n        "
            } 
        }
      ]
    }
    siteId: 7
  ) {
    id
  }
}

Got the error:

{
  "errors": [
    {
      "debugMessage": "Syntax error",
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      },
      "trace": [
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/helpers/BaseJson.php",
          "line": 128,
          "call": "yii\\helpers\\BaseJson::handleJsonError(4)"
        },
        {
          "file": "/var/www/html/vendor/verbb/tablemaker/src/fields/TableMakerField.php",
          "line": 51,
          "call": "yii\\helpers\\BaseJson::decode('\n            <table>\n                <thead>\n                    <tr>\n        <th align=\"left\" width=\"\">Store</th><th align=\"left\" width=\"\">North America</th><th align=\"left\" width=\"\">Europe</th><th align=\"left\" width=\"\">International</th>\n                    </tr>\n                </thead>\n\n                <tbody><tr><td align=\"left\">Shipping to</td><td align=\"left\">Canada, USA</td><td align=\"left\">Austria, Belgium, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark…</td><td align=\"left\">Australia, Japan, New Zealand, Singapore, Brazil</td></tr><tr><td align=\"left\">Waranty</td><td align=\"left\">One-year warranty. Tulip covers shipping costs for warranty service. See Terms. </td><td align=\"left\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td><td align=\"left\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td></tr>\n\n                </tbody>\n\n            </table>\n        ')"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/base/Element.php",
          "line": 4296,
          "call": "verbb\\tablemaker\\fields\\TableMakerField::normalizeValue('\n            <table>\n                <thead>\n                    <tr>\n        <th align=\"left\" width=\"\">Store</th><th align=\"left\" width=\"\">North America</th><th align=\"left\" width=\"\">Europe</th><th align=\"left\" width=\"\">International</th>\n                    </tr>\n                </thead>\n\n                <tbody><tr><td align=\"left\">Shipping to</td><td align=\"left\">Canada, USA</td><td align=\"left\">Austria, Belgium, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark…</td><td align=\"left\">Australia, Japan, New Zealand, Singapore, Brazil</td></tr><tr><td align=\"left\">Waranty</td><td align=\"left\">One-year warranty. Tulip covers shipping costs for warranty service. See Terms. </td><td align=\"left\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td><td align=\"left\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td></tr>\n\n                </tbody>\n\n            </table>\n        ', instance of craft\\elements\\MatrixBlock)"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/base/Element.php",
          "line": 3355,
          "call": "craft\\base\\Element::normalizeFieldValue('table')"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/base/Element.php",
          "line": 1764,
          "call": "craft\\base\\Element::getFieldValue('table')"
        },
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/validators/Validator.php",
          "line": 258,
          "call": "craft\\base\\Element::__get('table')"
        },
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/base/Model.php",
          "line": 367,
          "call": "yii\\validators\\Validator::validateAttributes(instance of craft\\elements\\MatrixBlock, array(3))"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/fields/Matrix.php",
          "line": 794,
          "call": "yii\\base\\Model::validate()"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/base/Element.php",
          "line": 2123,
          "call": "craft\\fields\\Matrix::validateBlocks(instance of craft\\elements\\Entry, null)"
        },
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/validators/InlineValidator.php",
          "line": 84,
          "call": "craft\\base\\Element::validateCustomFieldAttribute('field:contentBlocks', array(3), instance of yii\\validators\\InlineValidator, instance of craft\\elements\\db\\MatrixBlockQuery(1))"
        },
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/validators/Validator.php",
          "line": 261,
          "call": "yii\\validators\\InlineValidator::validateAttribute(instance of craft\\elements\\Entry, 'field:contentBlocks')"
        },
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/base/Model.php",
          "line": 367,
          "call": "yii\\validators\\Validator::validateAttributes(instance of craft\\elements\\Entry, array(1))"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/services/Elements.php",
          "line": 2552,
          "call": "yii\\base\\Model::validate()"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/services/Elements.php",
          "line": 785,
          "call": "craft\\services\\Elements::_saveElementInternal(instance of craft\\elements\\Entry, true, true, null)"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/gql/base/ElementMutationResolver.php",
          "line": 164,
          "call": "craft\\services\\Elements::saveElement(instance of craft\\elements\\Entry)"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/gql/resolvers/mutations/Entry.php",
          "line": 58,
          "call": "craft\\gql\\base\\ElementMutationResolver::saveElement(instance of craft\\elements\\Entry)"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 623,
          "call": "craft\\gql\\resolvers\\mutations\\Entry::saveEntry(null, array(3), array(2), instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 550,
          "call": "GraphQL\\Executor\\ReferenceExecutor::resolveFieldValueOrError(instance of GraphQL\\Type\\Definition\\FieldDefinition, instance of GraphQL\\Language\\AST\\FieldNode, array(2), null, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 474,
          "call": "GraphQL\\Executor\\ReferenceExecutor::resolveField(GraphQLType: Mutation, null, instance of ArrayObject(1), array(1))"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 857,
          "call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}(array(0), 'save_pages_pages_Entry')"
        },
        {
          "call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}(array(0), 'save_pages_pages_Entry')"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 859,
          "function": "array_reduce(array(1), instance of Closure, array(0))"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 490,
          "call": "GraphQL\\Executor\\ReferenceExecutor::promiseReduce(array(1), instance of Closure, array(0))"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 263,
          "call": "GraphQL\\Executor\\ReferenceExecutor::executeFieldsSerially(GraphQLType: Mutation, null, array(0), instance of ArrayObject(1))"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 215,
          "call": "GraphQL\\Executor\\ReferenceExecutor::executeOperation(instance of GraphQL\\Language\\AST\\OperationDefinitionNode, null)"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 156,
          "call": "GraphQL\\Executor\\ReferenceExecutor::doExecute()"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/GraphQL.php",
          "line": 162,
          "call": "GraphQL\\Executor\\Executor::promiseToExecute(instance of GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter, instance of GraphQL\\Type\\Schema, instance of GraphQL\\Language\\AST\\DocumentNode, null, array(2), null, null, null)"
        },
        {
          "file": "/var/www/html/vendor/webonyx/graphql-php/src/GraphQL.php",
          "line": 94,
          "call": "GraphQL\\GraphQL::promiseToExecute(instance of GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter, instance of GraphQL\\Type\\Schema, 'mutation {\n  save_pages_pages_Entry(\n    id: \"216123\"\n    contentBlocks: {\n      sortOrder: [\n        \"230833\"\n      ]\n      blocks: [\n        { \n          tableBlock: { \n          \tid: \"230833\" \n            table: \"\\n            <table>\\n                <thead>\\n                    <tr>\\n        <th align=\\\"left\\\" width=\\\"\\\">Store</th><th align=\\\"left\\\" width=\\\"\\\">North America</th><th align=\\\"left\\\" width=\\\"\\\">Europe</th><th align=\\\"left\\\" width=\\\"\\\">International</th>\\n                    </tr>\\n                </thead>\\n\\n                <tbody><tr><td align=\\\"left\\\">Shipping to</td><td align=\\\"left\\\">Canada, USA</td><td align=\\\"left\\\">Austria, Belgium, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark…</td><td align=\\\"left\\\">Australia, Japan, New Zealand, Singapore, Brazil</td></tr><tr><td align=\\\"left\\\">Waranty</td><td align=\\\"left\\\">One-year warranty. Tulip covers shipping costs for warranty service. See Terms. </td><td align=\\\"left\\\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td><td align=\\\"left\\\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td></tr>\\n\\n                </tbody>\\n\\n            </table>\\n        \"\n        \t} \n        }\n      ]\n    }\n    siteId: 7\n  ) {\n    id\n  }\n}\n', null, array(2), null, null, null, array(26))"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/services/Gql.php",
          "line": 546,
          "call": "GraphQL\\GraphQL::executeQuery(instance of GraphQL\\Type\\Schema, 'mutation {\n  save_pages_pages_Entry(\n    id: \"216123\"\n    contentBlocks: {\n      sortOrder: [\n        \"230833\"\n      ]\n      blocks: [\n        { \n          tableBlock: { \n          \tid: \"230833\" \n            table: \"\\n            <table>\\n                <thead>\\n                    <tr>\\n        <th align=\\\"left\\\" width=\\\"\\\">Store</th><th align=\\\"left\\\" width=\\\"\\\">North America</th><th align=\\\"left\\\" width=\\\"\\\">Europe</th><th align=\\\"left\\\" width=\\\"\\\">International</th>\\n                    </tr>\\n                </thead>\\n\\n                <tbody><tr><td align=\\\"left\\\">Shipping to</td><td align=\\\"left\\\">Canada, USA</td><td align=\\\"left\\\">Austria, Belgium, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark…</td><td align=\\\"left\\\">Australia, Japan, New Zealand, Singapore, Brazil</td></tr><tr><td align=\\\"left\\\">Waranty</td><td align=\\\"left\\\">One-year warranty. Tulip covers shipping costs for warranty service. See Terms. </td><td align=\\\"left\\\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td><td align=\\\"left\\\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td></tr>\\n\\n                </tbody>\\n\\n            </table>\\n        \"\n        \t} \n        }\n      ]\n    }\n    siteId: 7\n  ) {\n    id\n  }\n}\n', null, array(2), null, null, null, array(26))"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/controllers/GraphqlController.php",
          "line": 172,
          "call": "craft\\services\\Gql::executeQuery(instance of craft\\models\\GqlSchema, 'mutation {\n  save_pages_pages_Entry(\n    id: \"216123\"\n    contentBlocks: {\n      sortOrder: [\n        \"230833\"\n      ]\n      blocks: [\n        { \n          tableBlock: { \n          \tid: \"230833\" \n            table: \"\\n            <table>\\n                <thead>\\n                    <tr>\\n        <th align=\\\"left\\\" width=\\\"\\\">Store</th><th align=\\\"left\\\" width=\\\"\\\">North America</th><th align=\\\"left\\\" width=\\\"\\\">Europe</th><th align=\\\"left\\\" width=\\\"\\\">International</th>\\n                    </tr>\\n                </thead>\\n\\n                <tbody><tr><td align=\\\"left\\\">Shipping to</td><td align=\\\"left\\\">Canada, USA</td><td align=\\\"left\\\">Austria, Belgium, Bulgaria, Croatia, Cyprus, Czech Republic, Denmark…</td><td align=\\\"left\\\">Australia, Japan, New Zealand, Singapore, Brazil</td></tr><tr><td align=\\\"left\\\">Waranty</td><td align=\\\"left\\\">One-year warranty. Tulip covers shipping costs for warranty service. See Terms. </td><td align=\\\"left\\\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td><td align=\\\"left\\\">One-year warranty. Tulip does not cover shipping costs for warranty service. See Terms.</td></tr>\\n\\n                </tbody>\\n\\n            </table>\\n        \"\n        \t} \n        }\n      ]\n    }\n    siteId: 7\n  ) {\n    id\n  }\n}\n', null, null, true)"
        },
        {
          "call": "craft\\controllers\\GraphqlController::actionApi()"
        },
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/base/InlineAction.php",
          "line": 57,
          "function": "call_user_func_array(array(2), array(0))"
        },
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/base/Controller.php",
          "line": 178,
          "call": "yii\\base\\InlineAction::runWithParams(array(1))"
        },
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/base/Module.php",
          "line": 552,
          "call": "yii\\base\\Controller::runAction('api', array(1))"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/web/Application.php",
          "line": 293,
          "call": "yii\\base\\Module::runAction('graphql/api', array(1))"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/web/Application.php",
          "line": 602,
          "call": "craft\\web\\Application::runAction('graphql/api', array(1))"
        },
        {
          "file": "/var/www/html/vendor/craftcms/cms/src/web/Application.php",
          "line": 272,
          "call": "craft\\web\\Application::_processActionRequest(instance of craft\\web\\Request)"
        },
        {
          "file": "/var/www/html/vendor/yiisoft/yii2/base/Application.php",
          "line": 384,
          "call": "craft\\web\\Application::handleRequest(instance of craft\\web\\Request)"
        },
        {
          "file": "/var/www/html/public/index.php",
          "line": 21,
          "call": "yii\\base\\Application::run()"
        }
      ]
    }
  ],
  "data": {
    "save_pages_pages_Entry": null
  }
}

We got a json decode error. Am I supposed to pass a json string? What is the correct way to do it?

Additional context

No response

engram-design commented 2 years ago

We don't support GQL mutations for this field at the moment, but looking into it.

ce2cs commented 2 years ago

We don't support GQL mutations for this field at the moment, but looking into it.

So I suppose the only way is to manually change them through the control panel in different language sites, right?