为了账号安全,请及时绑定邮箱和手机立即绑定

将 ApolloError 传播到客户端

将 ApolloError 传播到客户端

鸿蒙传说 2023-10-20 16:23:37
我真的很难在客户端获得自定义 Apollo 错误。这是服务器代码:...const schema = makeExecutableSchema({  typeDefs: [constraintDirectiveTypeDefs, ...typeDefs],  resolvers,  schemaTransforms: [constraintDirective()],});const server = new ApolloServer({  schema,  dataSources,  context({ req }) {    const token = req.headers.authorization;    const user = token ? getUserFromToken(token) : '';    return { user };  },  debug: false,  formatError: (err) => {     // ToDo: Generate unique token and log error    if (err!.extensions!.code == 'INTERNAL_SERVER_ERROR') {      return new ApolloError('We are having some trouble', 'ERROR', {        token: 'uniquetoken',      });    }    return err;  },  uploads: false,});...客户端代码: ...const ADD_CLAIM = gql`  mutation addClaim($claim: ClaimInput!) {    addClaim(claim: $claim) {      id    }  }`;... const [addClaim, { data, error }] = useMutation(ADD_CLAIM); ... const onSubmit = async () => {   try {    debugger;    const r = await addClaim({      variables: {        input: {          id: insured.insured,          date: '20/12/2020',      ...          therapy: treatment.treatments.map(treat => ({            id: treat.treatId,        ...          })),        },      },    });    debugger;    console.log('r', r);    } catch (err) {      debugger;      setFormError(error ? error.message : err.message);      console.log('Error:', err);    }  };...  if (error) {    debugger;    return <div>error</div>;  }我预计会收到自定义错误:“我们遇到了一些麻烦”。但是,无论我做什么,我都会得到:“响应不成功:收到状态代码 400”
查看完整描述

1 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

这是我的错误:


error.networkError.result.errors

什么没人知道?


或者


const errorLink = onError(({ graphQLErrors, networkError }) => {

  debugger;

  console.log(graphQLErrors);

  console.log(networkError);

});


const client = new ApolloClient({

  ...

  link: ApolloLink.from( [errorLink, ...]),    

});

它也有效。


是的,有时 GraphQL 是一头令人讨厌的野兽


查看完整回答
反对 回复 2023-10-20
  • 1 回答
  • 0 关注
  • 58 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信