首页 > 解决方案 > Int 不能表示非整数值:Int

问题描述

我有这个阿波罗查询:

  currentUser: async (root, args) => {
    const currentUser = await prisma.user.findUnique({
      where: {id: Number(args.id)},
    });
    console.log(currentUser);
    return currentUser;
  },

在操场上我可以称之为:

query Query {
  currentUser(id: 1) {
    id
  }
}

在 Apollo 服务器日志中显示:"

{
  id: 1,
  email: 'peter@**.nl',
  name: 'Peter **',
  password: '$2b$04$Dqo4LN3S5ZOnk1T1MyatOuijbvHpl281oDChHdEu1ZfEOyk7mjLqu'
}
  type Query {
    currentUser (id: Int!): User
  }

但是当我尝试从我的 React 客户端调用查询时,我得到一个错误:

const ReturnCurrentUser = gql`
  query Query {
    currentUser(id: Int) {
      id
    }
  }
`;

const {loading, error, data} = useQuery(ReturnCurrentUser, {variables: {id: 1}});

错误:

Int 不能表示非整数值:Int

标签: reactjsapolloapollo-clientreact-apolloapollo-server

解决方案


看起来我的解析器错了:

const ReturnCurrentUser = gql`
  query currentUser($id: Int!) {
    currentUser(id: $id) {
      id
    }
  }
`;

解决了这个问题。


推荐阅读