从带有反应组件apollo的变更组件的apollo服务器无法获得完整的错误抛出

时间:2019-05-04 03:30:38

标签: reactjs apollo apollo-client apollo-server

我没有从Apollo服务器获取完整的错误信息,只是获得错误的堆栈跟踪信息。我需要得到完整的错误。如何配置?

我需要得到完整的错误,因为: enter image description here enter image description here 但是在组件上,只需获取: enter image description here

enter image description here 我有解析器:

Mutation: {
    signUp: (parent, args) => {
      console.log('args', args);

      const validationErrors = {};
      if (!args.name) {
        validationErrors.name = ('Name is required')
      }

      if ( !args.email ) {
        validationErrors.email = ('Email is required')
      }

      if ( !args.password ) {
        validationErrors.password = ('Password is required')
      }

      if (_.find(authors, {email: args.email})) {
        validationErrors.email = ('This email already exists')
      }

      if (Object.keys(validationErrors).length > 0) {
        throw new UserInputError(
          'Failed to get events due to validation errors',
          { validationErrors }
        );
      }

配置客户端:

import React, { Suspense, useState, useEffect } from 'react';
import { Spinner } from 'react-bootstrap'

//style
import './App.css';
// import './themes/bootstrap/bootstrap.css'

import { ApolloProvider } from 'react-apollo'
import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { createHttpLink } from 'apollo-link-http';
import { persistCache } from 'apollo-cache-persist';

import { withClientState } from 'apollo-link-state';
import { ApolloLink } from 'apollo-link';
import { onError } from "apollo-link-error";

import resolvers from './apollo/resolvers'
import defaults from './apollo/defaults'
import AppRoute from './AppRoute'

function App() {

  const [client, setClient] = useState(null)

  async function createClient() {
    const cache = new InMemoryCache({
      dataIdFromObject: object => {
        switch (object.__typename) {
          //loggedUser is whatever type "me" query resolves to
          case 'loggedUser': return object.__typename;
          case 'PostResponse': return object.__typename;
          default: return object.id || object._id;
        }
      }
    });

    // Create an http link
    const httpLink = createHttpLink({
      uri: 'http://localhost:4000',
    });
    const error = onError(({ graphQLErrors, networkError }) => {
      if (graphQLErrors) {
        console.log(graphQLErrors)
      }

      if (networkError) {
        console.log(networkError)
      }

    })
    const stateLink = withClientState({
      cache,
      resolvers,
      defaults
    });

    try {
      // See above for additional options, including other storage providers.
      await persistCache({
        cache,
        storage: window.localStorage,
      });
    } catch (error) {
      console.error('Error restoring Apollo cache', error);
    }

    // const defaultOptions = {
    //   watchQuery: {
    //     fetchPolicy: 'cache-and-network',
    //     errorPolicy: 'all',
    //   },
    //   query: {
    //     fetchPolicy: 'cache-and-network',
    //     errorPolicy: 'all',
    //   },
    //   mutate: {
    //     errorPolicy: 'all',
    //   },
    // };

    const client = new ApolloClient({
      link: ApolloLink.from([
        error,
        httpLink,
        stateLink,
        cache
      ]),
      cache,
      // defaultOptions,
      connectToDevTools: true
    });

    //update state
    setClient(client)
  }

  useEffect(() => {
    createClient()
  },[])

  return !client ? (
    <div className='wrap-loading'>
      <Spinner animation="border" variant="primary" />
    </div>
  ) : (
    <div className="App">
      <ApolloProvider client={client}>
        <Suspense fallback={()=> <Spinner animation="border" variant="primary" />}>
          <AppRoute />
        </Suspense>
      </ApolloProvider>
    </div>
  )
}

export default App

我需要在组件上获取全部错误,当前只在组件上获得堆栈跟踪:

return (
    <Mutation
      mutation={CREATE_USER}
      onCompleted={ () => props.history.push('/login') }
      errorPolicy="all"
    >
      {(signUp, { data, loading, error }) => {
        console.log('error', error)

0 个答案:

没有答案