Yes, you could query User data while fetching TeamMember information.
Consider this example:
schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model TeamMember {
id Int @id @default(autoincrement())
team_id Int
User User? @relation(fields: [userId], references: [id])
userId Int?
}
model User {
id Int @id @default(autoincrement())
name String
email String
password String
team TeamMember[]
}
index.ts
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({
log: ['query'],
});
async function main() {
await prisma.user.create({
data: {
email: 'test@test.com',
name: 'test',
password: 'test',
team: {
create: {
team_id: 1,
},
},
},
});
console.log('Created user');
const teamWithUsers = await prisma.teamMember.findUnique({
where: {
id: 1,
},
include: {
User: true,
},
});
console.log('teamWithUsers', teamWithUsers);
}
main()
.catch((e) => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});
Here's the response:
teamWithUsers {
id: 1,
team_id: 1,
userId: 1,
User: { id: 1, name: 'test', email: 'test@test.com', password: 'test' }
}
By default relation fields are not fetched, if you need to get the relation fields data in that case you would need to specify the include clause as demonstrated in the above example.
Answer from Nurul Sundarani on Stack OverflowMaking duel queries in prisma client
Multiple queries in prisma graphql resolver - Stack Overflow
Batch multiple Client queries into one Engine request (GraphQL Query)
Why multiple queries for returning data + count?
That is not yet possible. Here's a GitHub issue that had been created you can use to monitor the feature's status and give your feedback too.
Could you also share your use case too? Your feedback will be highly appreciated as we relay it back to the team. 🙂
Prisma multiSchema is now supported as a preview feature.
See here https://www.prisma.io/docs/guides/database/multi-schema
It was introduced in version 4.3.0 https://github.com/prisma/prisma/issues/1122#issuecomment-1231773471
As the docs say you would add the preview feature...
generator client {
provider = "prisma-client-js"
previewFeatures = ["multiSchema"]
}
Then in your datasource you note the schemas...
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
schemas = ["schema1", "schema2"]
}
And finally in each model you add the @@schema attribute...
model User {
id Int @id
orders Order[]
profile Profile?
@@schema("schema1")
}
model Order {
id Int @id
user User @relation(fields: [id], references: [id])
user_id Int
@@schema("schema2")
}