1

I´m working in back graphql API with Laravel and Lighthouse.

I have a table with a column named "config" that stores json data.

I´m trying to create a new register to that table.

I have a mutation:

createOrderTemplate(name: String!, config: JSON!): OrderTemplate @create

The schema is:

type OrderTemplate {
    id: ID!
    name: String!
    config: JSON! 
}

I´ve tried the mutation in graphql-playground

mutation{
  createOrderTemplate(
    name:"SomeName",
    config:
    [{
      "w_name": "Name1",
      "w_code": "001",
      "place": "Place1",
      "job": "job1",
      "data": [
        {
          "name": "name1",
          "quantity": 2
        },
        {
          "name": "name2",
          "quantity": 2
        },
        {
          "name": "name3",
          "quantity": 1
        },
        {
          "name": "name4",
          "quantity": 2
        }
      ]
    },
{
      "w_name": "Name2",
      "w_code": "002",
      "place": "Place2",
      "job": "job2",
      "data": [
        {
          "name": "name1",
          "quantity": 2
        },
        {
          "name": "name2",
          "quantity": 2
        },
        {
          "name": "name3"
          "quantity": 1
        },
        {
          "name": "name4",
          "quantity": 2
        }
      ]
    }]
){
    id
    name
    config
  }

When I typed this I get an error, all is colored red and it does not execute anything.

What am I doing wrong?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Jana
  • 11
  • 2

1 Answers1

0

Firstly, try to run that query in Insomnia, so you could see the exception that is thrown.

Moreover, I don't think that config gets a valid JSON value. Try first with a simple JSON like {key: 'value'}

Enzo Notario
  • 639
  • 4
  • 9
  • I´ll try! Thank you! – Jana Sep 14 '21 at 06:15
  • I´ve tried as yoy say with the following mutation: mutation{ createOrderTemplate( name:"Name" config:[{w_code:"Name Surname"}] ){ id name config } } But I get the message: "debugMessage": "Field \"createOrderTemplate\" argument \"config\" requires type JSON, found [{w_code: \"Name Surname\"}]; array_map(): Argument #2 ($array) must be of type array, GraphQL\\Language\\AST\\NodeList given", "message": "Internal server error" – Jana Sep 14 '21 at 06:20
  • I can put JSON directly on my data base with the schema [ {"key":"value", "key":"value", "key": [ {"key":"value"},{"key":"value"} ], {"key":"value", "key":"value", "key": [ {"key":"value"},{"key":"value"} ] } ] but It doesn´t work on my back. Is there any way to do that? I really need it – Jana Sep 14 '21 at 06:29
  • Try with `mutation{ createOrderTemplate( name:"Name" config:{w_code:"Name Surname"} ){ id name config } }` – Enzo Notario Sep 14 '21 at 14:08
  • Hi! Thank you very much! I´ve tried your suggested mutation and it worked, but when I try to insert the rest or arguments appears the error again. – Jana Sep 15 '21 at 06:38
  • The complet data to insert into config column will be something like that: [{"w_name": "Name","w_code": "001","workplace": "Location","job": "Job","garments": [{"name": "name","size": "L","quantity": 2},{"name": "name","size": "L","quantity": 2},{ "name": "name","size": "L","quantity": 1},{"name": "name","size": "M","quantity": 2}]},{"w_name": "Name","w_code": "002","workplace": "Location","job": "Job","garments": [{"name": "name","size": "L","quantity": 2},{"name": "name","size": "L","quantity": 2},{"name": "name","size": "L","quantity": 1},{"name": "name","size": "M","quantity": 2}]}] – Jana Sep 15 '21 at 06:42
  • Hi again! I´m using this JSON scalar https://gist.github.com/lorado/0519972d6fcf01f1aa5179d09eb6a315. I´ve tried to modify parseLiteral function and the mutation changed, but I wasn´t able to get successful response. Can you help me, please? – Jana Sep 15 '21 at 13:26
  • that is not a valid JSON. it should start with { }, not with [ ], so put it like `{fields: [{"w_name": "Name","w_code": "001","workplace": "Location","job": "Job","garments": [{"name": "name","size": "L","quantity": 2}, ... ]}] }` – Enzo Notario Sep 15 '21 at 13:58
  • Hi! Sorry, I get the same error: "Field \"createOrderTemplate\" argument \"config\" requires type JSON!, found {fields: [{w_name: \"Name\", w_code: \"001\", workplace: \"Location\", job: \"Job\", garments: [{name: \"name\", size: \"L\", quantity: 2}, {name: \"name\", size: \"L\", quantity: 2}]}, {w_name: \"Name\", w_code: \"001\", workplace: \"Location\", job: \"Job\", garments: [{name: \"name\", size: \"L\", quantity: 2}, {name: \"name\", size: \"L\", quantity: 2}]}]}; array_map(): Argument #2 ($array) must be of type array, GraphQL\\Language\\AST\\NodeList given" – Jana Sep 16 '21 at 06:49
  • Could it be related to the parseLiteral function of the JSON.php file? – Jana Sep 16 '21 at 06:55
  • Thank you so much for the effort and time invested to help me :) – Jana Sep 16 '21 at 06:56
  • Nice, you're welcome! Checkout this library, that implements a JSON scalar: https://github.com/mll-lab/graphql-php-scalars – Enzo Notario Sep 18 '21 at 21:44