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.
CodePudding user response:
The error occurs in the middle of defining an object literal so you need to provide field name before t.field, eg foo:
{
type: 'Post',
resolve: (parent, _, context: Context) => {
return context.prisma.user
.findUnique({
where: { id: parent.id || undefined }
})
.posts()
},
foo: t.field('profile', {
type: 'Profile',
resolve: (parent, _, context) => {
return context.prisma.user
.findUnique({
where: { id: parent.id }
})
.profile()
}
})
}
CodePudding user response:
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