can anyone help me understanding what is happening...
# prisma-whats-new
m
can anyone help me understanding what is happening here?
return api.request<{ createUser: User }>(mutation, variables).then(r => r.createUser.id)
from line 87 of signup.ts in the auth/email-password template?
I'm trying to add an additional mutation into this process, but that return line is tripping me up.
m
Hey, I suppose you are not very familiar with TypeScript? simple smile the part inside
<>
can be omitted, so it is actually
return api.request(mutation, variables).then(r => r.createUser.id)
, which is just a
Promise
.
In order to add another mutation it is probably best to add
.then(anotherPromise)
after
api.request(mutation. variables).then(r => r.createUser.id)
so you get something like
api.request(mutation, variables).then(r => r.createUser.id).then(anotherPermission)
On the other hand you could also wrap whole functions inside a
try...catch...
and use
await
to make sure first request is fulfilled and then
await
your mutation, or just return it.
Something like this 🙂 :
Copy code
async function createGraphcoolUser(api: GraphQLClient, email: string, password: string): Promise<string> {
  const mutation = `
    mutation createGraphcoolUser($email: String!, $password: String!) {
      createUser(
        email: $email,
        password: $password
      ) {
        id
      }
    }
  `

  const variables = {
    email,
    password: password,
  }

  try {
    const user = await api.request<{ createUser: User }>(mutation, variables)
      .then(r => r.createUser.id)

    const res = await api.request(yourMutation, yourVariables)
    return res
  } catch(err) {
    throw err
  }
}
m
@matic hey, I'm just seeing this. I am not so familiar with TypeScript, although I am much more now 🙂 Thanks for taking the time to share your knowledge. I copied down your snippet for future reference. Happy New Year!
m
Np man, happy new year 😄