我正在嘗試為prisma.io 嘗試和示例,并使用其中一個示例,我收到一個錯誤,抱怨想要一個,,但我不知道為什么。這是代碼:
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()
},
})
})
},
})
嘗試編譯代碼時出現以下錯誤:
[ERROR] 09:24:23 ? Unable to compile TypeScript:
src/schema.ts:263:8 - error TS1005: ',' expected.
263 t.field ('profile',{
~
它似乎希望它在位置 8,但沒有意義。任何幫助表示贊賞。我不是一個開發人員,只是試圖從他們的 github 上完成這個例子。
uj5u.com熱心網友回復:
您有語法錯誤。
這是因為它t.field(...)是一個計算為一個值的函式呼叫,并且您將此值放置在一個物件文字宣告中,它需要一個欄位串列。當您將函式呼叫分配給欄位名稱時,錯誤將消失。下面我用??和注釋注釋了你的代碼,并將函式呼叫值分配給“物種”。這可能不是您想要的,但它可以幫助您理解語法錯誤。
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
},
})
uj5u.com熱心網友回復:
該錯誤發生在定義物件字面量的中間,因此您需要在 t.field 之前提供欄位名稱,例如 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()
}
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/373496.html
標籤:打字稿
