2017-07-25 39 views
0

我開始使用graphql,我試圖刪除graphql的節點,但我沒有得到它。刪除突變不起作用

這裏是我的解析:

export default { 
    Query: { 
    allLinks: async (root, data, { mongo: { Links } }) => 
     Links.find({}).toArray() 
    }, 
    Mutation: { 
    createLink: async (root, data, { mongo: { Links }, user }) => { 
     const newLink = Object.assign({ postedById: user && user._id }, data); 
     const response = await Links.insert(newLink); 
     return Object.assign({ id: response.insertedIds[0] }, newLink); 
    }, 
    removeLink: async (root, { id }, { mongo: { Links }, user }) => { 
     const newLink = Object.assign({ postedById: user && user._id }); 
     const response = await Links.remove(id); 
     return Object.assign(response, newLink); 
    }, 
    createUser: async (root, data, { mongo: { Users } }) => { 
     const newUser = { 
     name: data.name, 
     email: data.authProvider.email.email, 
     password: data.authProvider.email.password 
     }; 
     const response = await Users.insert(newUser); 
     return Object.assign({ id: response.insertedIds[0] }, newUser); 
    }, 
    signinUser: async (root, data, { mongo: { Users } }) => { 
     const user = await Users.findOne({ email: data.email.email }); 
     if (data.email.password === user.password) { 
     return { token: `token-${user.email}`, user }; 
     } 
    } 
    }, 

    Link: { 
    id: root => root._id || root.id, 
    postedBy: async ({ postedById }, data, { dataloaders: { userLoader } }) => { 
     return await userLoader.load(postedById); 
    } 
    }, 
    User: { 
    id: root => root._id || root.id 
    } 
}; 

所有突變都工作正常少removeLink。

當我運行removeLink突變我得到這個錯誤:

MongoError: Wrong type for 'q'. Expected a object, got a string.

我知道什麼是錯的,但我不知道是什麼。

+0

你是什麼意思,當你說這是「工作不正常」? GraphQL是否返回任何錯誤?如果是的話,什麼?此外,這個問題可能與您提交給GraphQL端點的查詢或您的類型定義有關......提供這些信息可能有助於指出問題所在。 –

+0

對不起,我忘記報告錯誤。我會更新我的問題 –

回答

1

您應該使用deleteOne()而不是remove(),因爲remove()已棄用。也沒有任何理由發回你最近刪除的鏈接。

嘗試是這樣的(不知道你的代碼的其餘部分,所以我無法測試它):

removeLink: async (root, { id }, { mongo: { Links }, user }) => { 
    return await Links.deleteOne({ id }); 
}, 

如果你仍想返回刪除鏈接:

removeLink: async (root, { id }, { mongo: { Links }, user }) => { 
    const newLink = Object.assign({ postedById: user && user._id }); 
    const response = await Links.deleteOne({ id }); 
    return Object.assign(response, newLink); 
}, 
+0

你需要什麼信息?我的模式?隨着你的答案,我得到了這個錯誤:「不能返回null不可空字段Link.id.」 –

+1

在您的模式中,您不應期待爲removeLink突變發回鏈接。如果你仍然希望返回被刪除的鏈接,請將'Links.remove()'替換爲我的'Links.deleteOne()'到你的代碼中,看看它是否有效。 (如果你想看一下,我已經用更完整的代碼編輯了我的答案。) –

+0

我不想返回被刪除的鏈接,我只是不知道如何做這個remotion。但是,我在我的模式中返回了什麼? 'removeLink(id:ID!):?' –

0

你的問題似乎與你如何使用MongoDB而不是GraphQL。如果你看the docs for the Collection.remove()方法,你會發現你可以將它稱爲「查詢」,Mongo將刪除所有符合條件的項目。

就你而言,你的查詢看起來是無效的。您正在將它傳遞給字符串id,但您應該將它傳遞給對象{ id: <some value> }。我認爲你想要的行是:

const response = await Links.remove({ id: id});