0

Hello i'm trying to consume a query made with graphql into react with apollo but any time the component is loaded throws me this error:

Error: "Network error: NetworkError when attempting to fetch resource."

I'm trying to figure out why is not working properly. when i checked my server console showed me this message:

ActionController::RoutingError (No route matches [OPTIONS] "/graphiql"):

but if i go to http://localhost:3000/graphiql graphql server its running and working with all the queries.

the configuration of my index.js is:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { BrowserRouter } from 'react-router-dom';

// 1
import { ApolloProvider } from 'react-apollo'
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'


// 2
const httpLink = createHttpLink({
  uri: 'http://localhost:3000/graphiql'
})


// 3
export const client = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache()
})



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

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers:
serviceWorker.unregister();

the component that connect and consume the query is:

        // React
    import React, { Fragment } from 'react';

    // React apollo
    import { graphql } from 'react-apollo';
    import * as compose from 'lodash.flowright';

    // import queries
    import queries from './queries';

    // React bootstrap
    import { Container, Row, Form, Button } from 'react-bootstrap';



    class LoginForm extends React.Component {
        render() {

            const { posts } = this.props;
            console.log('que trae ', posts);
            return(
                <Fragment>
                <h1>hello</h1>
                </Fragment>
            );
        }
    }

    export default compose(
        graphql(queries.posts, { name: 'posts' }),
    )(LoginForm);

and the querie itself is:

    import gql from 'graphql-tag';

const queries = {
    posts: gql`
    query posts {
        title
    }
    `,
}

export default queries;
Richi
  • 438
  • 5
  • 18
  • This is not a client side issue. Your server needs to be configured to handle CORS requests as per the linked post. – Daniel Rearden Feb 08 '20 at 20:48
  • hello @DanielRearden i have configured CORS in the server side in rails but got the same problem. – Richi Feb 08 '20 at 21:17
  • 1
    Also it appears you're pointing your client at `/graphiql`. Is that the endpoint your server is listening for GraphQL requests or is it something else, like `/graphql`? – Daniel Rearden Feb 08 '20 at 21:29
  • Yes @Daniel Rearden, i made all the tests in localhost:3000/graphiql – Richi Feb 08 '20 at 21:32
  • 1
    `/graphiql` is a playground/testing tool, not your api endpoint .... open browser dev tools and play with `graphiql` - you should see (network tab) requests to your endpoint, probably `/graphql` – xadm Feb 08 '20 at 23:54
  • yes but i try it on postman with /graphql and still nothing @xadm i get No route matches [GET] "/graphql" – Richi Feb 09 '20 at 02:08
  • becuse it should be POST ? – xadm Feb 09 '20 at 08:12
  • thanks guys! @xadm and Daniel Rearden the problemas first: /graphql and i didnt set up cors in the server side second i had to configure application controller because it showed me erorr with token! – Richi Feb 09 '20 at 16:10

0 Answers0