Also, what's the best way to call a nexus mutation...
# orm-help
a
Also, what's the best way to call a nexus mutation from backend? Do I need to directly call it using
prisma
or is there a way to call mutation directly? I am doing Oauth so I get the data on the backend only. If I do send it back to client using
res.send
& then do mutation
createUser
, then its 2 steps. If I don't send it to client but do it on the backend, then I don't use the mutation
createUser
but directly do it so its 1 step. What is your recommended approach?
r
You can separate the logic required in the mutation in a function and call the function wherever you require. This way you don’t need to call the mutation explicitly.
👍 1
a
thank you, did it like this:
Copy code
interface ICreateUser {
	userId: string
	username: string
	name: string
}

export const createUser = async ({ userId, username, name }: ICreateUser) => {
	return await prisma.user.create({
		data: {
			id: userId,
			username,
			name,
			settings: {
				connect: {
					id: userId,
				},
			},
		},
	})
}
maybe my prisma schema is wrong. all i want is each
user
to have a
settings
column. for each
user
, it might be different due to the fact that
timezone
&
userId
aka
user
being different.
when i tried making it optional, i got this automatically after saving through prisma vscode extension with red-squiggly lines:
Copy code
model User {
  id       String  @id @default(cuid())
  username String  @unique
  name     String?
  email    String? @unique
  image    String?

  // relations
  settings   Settings? @relation(fields: [settingsId], references: [id], map: "User_settingsId_fkey")
  teams      Team[]
  settingsId String
}

model Settings {
  id       String @id @default(cuid())
  timezone String @default("UTC")

  // relations
  user   User   @relation(fields: [userId], references: [id])
  userId String
}
Copy code
Error parsing attribute "@relation": The relation fields `user` on Model `Settings` and `settings` on Model `User` both provide the `references` argument in the @relation attribute. You have to provide it only on one of the two fields.
Error parsing attribute "@relation": The relation fields `user` on Model `Settings` and `settings` on Model `User` both provide the `fields` argument in the @relation attribute. You have to provide it only on one of the two fields.
i tried removing
id
on
Settings
but still gives error
r
No need to remove
id
on
Settings
. When you make a relation optional, you also need to make the corresponding foreign key optional, which is why you are getting this error.
settings
and
settingsId
both need to be optional.
a
yes, that worked but another weird error arose:
Copy code
Error: 
Invalid `prisma.user.create()` invocation:
  An operation failed because it depends on one or more records that were required but not found. No 'Settings' record to connect was found was found for a nested connect on one-to-one relation 'SettingsToUser'.
Copy code
model User {
  id       String  @id @default(cuid())
  username String  @unique
  name     String?
  email    String? @unique
  image    String?

  // relations
  teams Team[]

  settings   Settings?
  settingsId String?
}

model Settings {
  id       String @id @default(cuid())
  timezone String @default("UTC")

  // relations
  user   User   @relation(fields: [userId], references: [id])
  userId String @unique
}
this is my schema bdw which gives the above error
solved it
had to do
settings: { create: {} }