如何处理阿波罗客户端上的突变错误?

时间:2018-11-16 18:46:45

标签: reactjs apollo-client apollo-server

我正在尝试解决句柄如何在apollo客户端引发apollo服务器突变错误。

这是我简化的变体(createCustomer)实现:

  Mutation: {
    createCustomer: async (_, { photoFile, customer }) => {
      const rootPath = path.resolve("./public");
      const customerPath = path.join(rootPath, "/photos/customers");
      try {
        const {
          dataValues: { customerID, firstname, lastname, email, phone }
        } = await models.Customer.create(customer);
        const customerUniqueDir = path.join(customerPath, 
         `${customerID}`);
       } catch (createCustomerError) {
       // throw new apollo-server error
        throw new UserInputError();
      }
    }
}

在客户端,我得到以下错误: (第一个不是红色的错误只是客户端上catch块中的console.log)

阿波罗链接引发此错误:

enter image description here

这是服务器的响应:

enter image description here

这是apollo-client的实现:

import { ApolloClient } from "apollo-client";
import { ApolloLink } from "apollo-link";
import { ErrorLink } from "apollo-link-error";
import { withClientState } from "apollo-link-state";
import { createUploadLink } from "apollo-upload-client";
import { ApolloProvider } from "react-apollo";
import { InMemoryCache } from "apollo-cache-inmemory";
import App from "./App";


const cache = new InMemoryCache({
  addTypename: false
});

const stateLink = withClientState({
  cache,
  resolvers: {
    Mutation: {}
  },
  defaults: {
    customers: { customers: [], count: 0 }
  }
});
const uploadLink = createUploadLink({ uri: "http://localhost:8000/graphql" });
const errorLink = new ErrorLink();

const client = new ApolloClient({
  link: ApolloLink.from([stateLink, errorLink, uploadLink]),
  cache,
  connectToDevTools: true
});

ReactDOM.render(
  <BrowserRouter>
    <ApolloProvider client={client}>
        <App />
    </ApolloProvider>
  </BrowserRouter>,
  document.getElementById("root")
);

有什么解决方案如何处理客户端的变异错误?

感谢您的回答

1 个答案:

答案 0 :(得分:0)

解决方案:

错误出现在阿波罗链接中。因此,我研究了graphql-client的实现,并且意识到我忘记了使用apollo-link-http模块。

所以我添加了以下代码行:

#include <string>
#include <regex>

int main(int argc, char const *argv[]) {
  std::string s = "_apple_";

  std::regex r1("_(\\s|\\S)+_");
  std::regex r2("_[\\s\\S]+_");
  std::regex r3("_.+_");
  std::regex r4("_[pale]+_");

  std::smatch sm;
  printf("r1:%d r2:%d r3:%d r4:%d\n", 
        std::regex_match(s, sm, r1), 
        std::regex_match(s, sm, r2), 
        std::regex_match(s, sm, r3), 
        std::regex_match(s, sm, r4));

  return 0;
}