Hello! Anything to watch out for when attempting t...
# prisma-whats-new
p
Hello! Anything to watch out for when attempting to save data to a
json
field within a Schema Extension Function?
a
Yes, this forum post gives some suggestions and pitfalls: https://www.graph.cool/forum/t/json-field-type-invalid-input/210
p
Right, so if I’m getting the results as
.json()
back from Stripe I’d better
JSON.stringify(result.json())
then.
a
Yes, once if you use variables in your mutation, twice if you use inline.
p
I’m sorry, can I get back to you again concerning this? I’ve got an object “result” coming back from Stripe. If I pass it to my mutation as is, I get an error that [object Object] is not JSON
If I use JSON.stringify(result), I still get an error though: Invalid Input
a
What does the result object look like?
p
Copy code
{"response":{"error":"Syntax error while parsing GraphQL query. Invalid input \"\"{\"id\"\", expected Value or Argument (line 3, column 99):\n          updateUser(id: \"cj3dmjxzk47xk0190667es0rx\", stripeId: \"cus_BJdT8zbQ569hza\", stripeData: \"{\"id\":\"cus_BJ
etc.
a
That's not the result object from Stripe
p
Copy code
function createStripeCustomer() {
    return stripe.customers.create({
      email: email,
      source: source,
    })
  }
That’s the function, as simple as that. It returns the customer object.
a
Yet the response you pasted above is from the updateUser mutation
p
That’s right, cause I do this:
Copy code
return createStripeCustomer()
    .then(result => {
    	if (result.error) {
          throw new Error(result.error.type + ': ' + result.error.message)
        }
    	return updateGraphcoolUser(userId, result.id, JSON.stringify(result))
  	})
a
I think it needs another JSON.stringify, like I mentioned
p
Yeah that’s why I tried
JSON.stringify(JSON.stringify(result))
but that just increased the number of slashes in the error lol
a
Can you log just
result
before the return statement?
p
Copy code
"logs":["2017-08-31T21:43:24.995Z: {\"id\":\"cus_BJdjBoxxJZYPYL\",\"object\":\"customer\",\"account_balance\":0,\"created\":1504215804,\"currency\":null
etc.
a
And the updateGraphcoolUser method?
p
Copy code
mutation {
          updateUser(id: "${userId}", stripeId: "${stripeId}", stripeData: "${stripeData}") {
            id,
            stripeData
          }
        }
…where
stripeData
is a Json field and the other two are `String`s
a
I think the solution on the forum was JSON.stringify twice, and then without the quotes around
${stripeData}
in the mutation
p
Oh… why are the quotes around
${stripeData}
significant?
a
Because they are already added by the second JSON.stringify, so putting them in the mutation too basically puts
""
as stripeData value, followed by the rest of the Json value that can't be interpreted
p
Indeed, that worked.
a
So you have:
{ a: 1}
. JSON.stringify turns that into
{"a": "1"}
Now the second stringify turns that into
"{\"a\": \"1\"}"
p
Copy code
mutation {
          updateUser(id: "${userId}", stripeId: "${stripeId}", stripeData: ${JSON.stringify(stripeData)}) {
            id,
            stripeData
          }
        }
Yup, I think I got it.
So I changed as above, and left only one
JSON.stringify(result)
Thanks a lot for taking the time to respond
a
So putting quotes in the mutation turns that into:
""{\"a\": \"1\"}""
which is too much 🙂
😄 1
Great to see it's working now
Just out of interest, you still have a JSON.stringify in your call to the updateGraphcoolUser function as well?
p
Yes
So indeed there are two `JSON.stringify`s: one in the mutation (without quotes) and the other when calling the mutation
a
Exactly
Good to know that the same solution applies to your case.
p
👍
a
I would strongly urge you to switch to variables though. They save you from half of this quoting misery 🙂
p
Switch to variables?
Aren’t these already variables?
Do you have an example online?
a
No, this is inline JS literal template, not variables
p
Oh, so you mean not using a literal template mutation at all?
To be honest this is the only way I know how to do it, I don’t think I’ve seen an example of an alternative yet.
a
Copy code
mutation($userId: ID, $stripeId: String, $stripeData: Json) {
          updateUser(id: $userId, stripeId: $stripeId, stripeData: $stripeData) {
            id,
            stripeData
          }
        }
p
So you’re calling updateUser directly within the inline function?
a
Wait, I'll try to find a better example
You're using
graphcool-lib
, so
request(...)
becomes
api.request(...)
, but it shows you how to use variables
p
Ohhhhh…
So api.request actually takes either a string literal or a set of arguments?
a
api.request(query)
, or
api.request(query, variables)
p
Right
a
variables
being a json object with all the variables
p
Just like one I would have given apollo on my client
a
Exactly like apollo-client
p
In that case I wouldn’t have needed any JSON.stringify?
Or would I have still needed at least one?
a
You wouldn't need the second one
p
That make sense
Why thanks!
😎 1
a
Because the Graphcool Json Type is actually a Json string, not a Json object
p
Yup
a
Also, your mutations become a lot more readable, because you loose the template literal stuff. I would advise you to test it out in the Playground, to get 'the hang of it'
p
That’s what I’ll do 🙂
👍🏻 1
So I don’t know what time zone you’re on, but I’ll say good night from my side!
You seem to be on UK time…
a
Same local time as you, it seems (0:02)
p
Oh yeah, my bad, my laptop is still set to GMT lol
a
Well, anyways, thank you, good luck, and good night, whenever that is 🙂
🙏 1