donedgardo
10/04/2018, 10:07 PM// A `main` function so that we can use async/await
async function main() {
// Read the previously created user from the database and print their posts to the console
const postsByUser = await prisma
.user({ email: "<mailto:bob@prisma.io|bob@prisma.io>" })
.posts()
console.log(`All posts by that user: ${JSON.stringify(postsByUser)}`)
}
What if i wanted to query the posts and something else like books.
How would i do that? would it require two round trip requests
const postsByUser = await prisma
.user({ email: "<mailto:bob@prisma.io|bob@prisma.io>" })
.posts()
const booksByUser = await prisma
.user({ email: "<mailto:bob@prisma.io|bob@prisma.io>" })
.books()
We loose the info way of only one query to get all data...
{
id
posts {
id
}
books {
id
}
}harrisrobin
10/04/2018, 10:26 PMharrisrobin
10/04/2018, 10:27 PMdonedgardo
10/04/2018, 10:29 PMdonedgardo
10/04/2018, 10:48 PMmutation ($name: String!){
createUser(name: $name) {
id
}
}
const variables = { name: 'Alice' }
const result = prisma.$graphql(mutation, variables)โโโ
This would work in my case native $graphqlGomah
10/05/2018, 12:30 AM# query/user.js
async function me(_, args, context) {
const id = await getUserId(context);
return context.prisma.user({ id });
}
& then, in your resolvers:
...
User: {
books(root, args, context) {
return context.prisma.user({ id: root.id }).books();
},
posts(root, args, context) {
return context.prisma.user({ id: root.id }).posts();
},
},Gomah
10/05/2018, 12:31 AMdivyendu
10/05/2018, 5:56 AM$fragment syntax to achieve this result and have better type safety than schema delegation
https://www.prisma.io/docs/prisma-client/basic-data-access/reading-data-TYPESCRIPT-rsc3/#using-fragments-for-fine-grained-data-access
What do you think about it?