如何将 objectId 从反应前端传递到服务器端 graphql?

How do I pass in objectId from the react front end to server-side graphql?

所以我正在尝试使用 useMutation 挂钩调用 graphql 突变,它应该存储一条新记录,其中包含对另一个集合的 objectId 引用 mongodb。

const handleSubmit = async (e) => {
    e.preventDefault();
    const ticket = {
      ticketOwner: "60bd776e3e088a27ecae9dbb",
      venue: "Chase Center" 
    };
    console.log(ticket);
    try {
    await createTicket({variables: {ticket}});
    } catch (e) {
      console.log(e);
    }
    handleClose();
  }

变化是下面的这个片段,我已经在操场上测试它以验证服务器端代码是否设置并且 运行 正确。

export const CREATE_TICKET = gql`
    mutation ($ticketOwner: ID!, $venue: String!) {
        createTicket(ticketOwner: $ticketOwner, venue: $venue) {
_id
venue}}`;

每次我用我的反应前端执行 handlesubmit(按钮事件处理程序)时,我总是得到 400 http://localhost:3000/graphql 400 (Bad Request) 所以我认为这可能与我传递 id 引用的方式有关引用的集合。有没有人对如何从前端处理它有更好的方法? (我知道它现在是为了测试目的而硬编码的。)

检查网络选项卡以查看错误内容。另请查看 Apollo Client createHttpLink URI 参数中是否有尾部正斜杠。

应该是这样的

const httpLink = createHttpLink({
  uri: "http://localhost:5000/graphql",
});

最后,您可能需要在后端安装 cors,但首先检查网络选项卡以查看您从服务器获得的内容