亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 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 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號