我真的很難在客戶端獲得自定義 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”
將 ApolloError 傳播到客戶端
鴻蒙傳說
2023-10-20 16:23:37