Issue
I am working on trying out and example for prisma.io and using one of the examples I get an error complaining about wanting a ,
and I can't figure out why. Here is the code:
const Profile = objectType({
name: 'Profile',
definition(t) {
t.nonNull.int('id')
t.string('bio')
t.field('user', {
type: 'User',
resolve: (parent, _, context) => {
return context.prisma.profile
.findUnique({
where: { id: parent.id || undefined },
})
.user()
},
})
},
})
const User = objectType({
name: 'User',
definition(t) {
t.nonNull.int('id')
t.string('name')
t.nonNull.string('email')
t.nonNull.list.nonNull.field('posts', {
type: 'Post',
resolve: (parent, _, context: Context) => {
return context.prisma.user
.findUnique({
where: { id: parent.id || undefined },
})
.posts()
},
t.field ('profile',{
type: 'Profile',
resolve: (parent,_,context) =>{
return context.prisma.user.findUnique({
where: {id: parent.id}
}).profile()
},
})
})
},
})
I get the following error when it tries to compile the code:
[ERROR] 09:24:23 ⨯ Unable to compile TypeScript:
src/schema.ts:263:8 - error TS1005: ',' expected.
263 t.field ('profile',{
~
It seems to want it in position 8, but doesn't make sense. Any help is appreciated. I'm not a developer just trying to work through this example from their github.
Solution
You have a syntax error.
It's because it's a t.field(...)
is a function call that evaluates to a value, and you placed this value within an object literal declaration where it is expecting a list of fields. The error will go away when you assign the function call to a field name. Below I've annotated your code with 🌶's and comments, and assigned the function call value to "species". That may not be what you want, but it helps you understand the syntax error.
const User = objectType({
name: 'User',
definition(t) {
t.nonNull.int('id')
t.string('name')
t.nonNull.string('email')
t.nonNull.list.nonNull.field('posts', { //🌶 opening brace of object literal
type: 'Post', //🌶 field "type"
resolve: (parent, _, context: Context) => { //🌶 field "resolve"
return context.prisma.user
.findUnique({
where: { id: parent.id || undefined },
})
.posts()
},
species: t.field ('profile',{ //🌶 field "species"
type: 'Profile',
resolve: (parent,_,context) =>{
return context.prisma.user.findUnique({
where: {id: parent.id}
}).profile()
},
})
}) //🌶 closing brace of the object literal
},
})
Answered By - Inigo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.