2017-07-25 61 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});