Home > Mobile >  React apollo sends POST request instead of GET while querying graphql
React apollo sends POST request instead of GET while querying graphql

Time:06-15

I'm trying to get more familiar with graphql using react-apollo and I'm stuck for a while now.

I just want to query movies by name from a gql server, but no luck so far. My problem is that when I make the request I get an error that says:

POST https://tmdb.sandbox.zoosh.ie/dev/grphql 400

However I want to make a GET request. I tried to specify the request method in the apollo client, but no luck either.

index.js

import React from "react";
import ReactDOM from "react-dom/client";
import {
ApolloClient,
InMemoryCache,
ApolloProvider,
HttpLink,
} from "@apollo/client";

import "./index.css";
import App from "./App";

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: new HttpLink({
    uri: "https://tmdb.sandbox.zoosh.ie/dev/grphql",
    method: "GET",
  }),
  connectToDevTools: true,
});

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <ApolloProvider client={client}>
      <App />
    </ApolloProvider>
  </React.StrictMode>
);

MovieList.js

import React from "react";
import { useQuery, gql } from "@apollo/client";

const SEARCH_MOVIES = gql`
  query SearchMovies($movieTitle: String!) {
    movies(query: $movieTitle) {
      id
      name
      score
      genres {
        name
      }
      overview
      releaseDate
    }
  }
`;

const MovieList = () => {
  const { loading, error, data } = useQuery(SEARCH_MOVIES, {
    variables: {
      movieTitle: "fight club",
    },
  });

  if (loading) return <p>Loading...</p>;
  if (error) return <p>Error :(</p>;

  return (
    <>
      <div>MovieList</div>
      <ol>
        {data.movies.map((movie) => (
          <li key={movie.id}>{movie.name}</li>
        ))}
      </ol>
    </>
  );
};

export default MovieList;

Now I would appriciate, if someone could help me out what the problem might be, because my eyes can't see it. I searched all over the internet, but didn't find any usable resource regarding the topic.

Thanks for the replies in advance!:)

CodePudding user response:

You can use useGETForQueries as constructor options. docs

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: new HttpLink({
    uri: "https://tmdb.sandbox.zoosh.ie/dev/grphql",
    useGETForQueries: true
  }),
  connectToDevTools: true,
});
  • Related