While using Select it still gives access to other properties in GraphiQL #1158
-
Nexus Report{
"node": "v14.4.0",
"os": {
"platform": "darwin",
"release": "19.5.0"
},
"nexus": "^0.25.0",
"plugins": [
"nexus-plugin-prisma"
],
"otherDependencies": {
"@prisma/client": "2.1.3",
"graphql-yoga": "^1.18.3"
},
"devDependencies": {
"@prisma/cli": "^2.1.3",
"nodemon": "^2.0.4",
"rimraf": "^3.0.2"
},
"hasAppModule": true,
"packageManager": "npm",
"errorsWhileGatheringReport": {
"gettingLayout": null,
"gettingPluginManifests": null
}
} Bug descriptionWhen I am using Prisma Although they are always returned as How to reproduceI am using Prisma schema.extendType({
type: 'Query',
definition(t) {
t.crud.blogs()
t.crud.blog({
alias: 'blog',
})
t.field('getAllTitles', {
type: 'Blog',
list: true,
resolve(root, args, ctx) {
return ctx.db.blog.findMany({
select: {
title: true,
},
})
},
}) My schema is simple as: model Blog {
id String @default(cuid()) @id
title String
} Expected behaviorNow when I type the following in GraphiQL, I only expect it to return all {
getAllTitles {
title
}
} But I can also type {
getAllTitles {
id
title
}
} And I get even results like: [
{
"title": "Join the Prisma Slack on https://slack.prisma.io",
"id": null
},
{
"title": "Follow Prisma on Twitter",
"id": null
}
] I don't think Environment & setup
ScreenshotAnd I also get a red squiggly line over resolve saying:
I've simplified the Schema above but I have 9 other fields. The red squiggly lines won't let me generated the build using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @deadcoder0904 👋, this is not a bug. Selecting which properties you want to query from your db is completely independent from your GraphQL schema. Your GraphQL schema will not ever change based on the field you query from your database. Instead, your GraphQL schema is a contract that your database queries need to fulfill. In fact, the red squiggly lines are telling you that you are not fulfilling that contract as your resolver expect you to return something of type I think what you're trying to achieve is to optimize your database queries based on what the user query from a GraphQL perspective. While I personally do not recommend that pattern, since some of your child resolvers might rely on some of your data to always be present, you can try using https://paljs.com/plugins/select, which will convert the incoming GraphQL query to a Prisma's This way, you won't need to have a separate |
Beta Was this translation helpful? Give feedback.
Hey @deadcoder0904 👋, this is not a bug.
Selecting which properties you want to query from your db is completely independent from your GraphQL schema. Your GraphQL schema will not ever change based on the field you query from your database.
Instead, your GraphQL schema is a contract that your database queries need to fulfill.
In fact, the red squiggly lines are telling you that you are not fulfilling that contract as your resolver expect you to return something of type
Blog
, which is, an object with anid
andtitle
property. But you are only returning a fieldtitle
.I think what you're trying to achieve is to optimize your database queries based on what the user query from a GraphQL persp…