Invalid Auth Token with Rails, Graphql, Apollo Client

后端 未结 7 1646
北海茫月
北海茫月 2021-02-09 13:06

I am trying to get a basic Rails, Graphql, Apollo-Client setup working but having trouble with 422 errors \'invalid auth token\' on the rails side.

Does my use of apoll

7条回答
  •  -上瘾入骨i
    2021-02-09 13:54

    This worked for me (I'm using react_on_rails):

    import { ApolloClient, createNetworkInterface } from 'react-apollo';
    import ReactOnRails from 'react-on-rails';
    
    const networkInterface = createNetworkInterface({
      uri: '/graphql',
      opts: {
        credentials: 'same-origin'
      }
    });
    networkInterface.use([{
      applyMiddleware(req, next) {
        if (!req.options.headers) {
          req.options.headers = {
            "X-CSRF-Token": ReactOnRails.authenticityToken()
          }
        }
        next();
      }
    }]);
    
    const client = new ApolloClient({
      networkInterface
    });
    
    export default client
    

    I was missing opts: { credentials: 'same-origin' } , resulting in the same error: Can't verify CSRF token authenticity. Completed 422 Unprocessable Entity

提交回复
热议问题