apollo-client icon indicating copy to clipboard operation
apollo-client copied to clipboard

400 Bad request error happening in createHttpLink

Open apensia914 opened this issue 2 years ago • 8 comments

I am Using Apollo Client with React and trying to fetch the data.

Here's my Query that I made for seeProduct with using graphql-nexus

// See Product Query
export const SeeProductQuery = queryField("seeProduct", {
  type: "Product",
  description: "See Product Query",
  args: {
    id: nonNull(stringArg()),
  },
  resolve: (_, { id }, {}) => {
    return client.product.findUnique({ where: { id } });
  },
});

and below is my Home() component.

const SEE_PRODUCT_QUERY = gql`
  query ($seeProductId: String!) {
    seeProduct(id: $seeProductId) {
      name
      price
    }
  }
`;

export default function Home() {
  const { data } = useQuery(SEE_PRODUCT_QUERY);
  return (
    <>
      <PageTitle title="Home" />
      <Container></Container>
    </>
  );

And my setting for Apollo with React


const httpLink = createHttpLink({
  uri: "http://localhost:4500/graphql",
});

const authLink = setContext((_, { headers }) => {
  const token = localStorage.getItem("authorization");
  return {
    headers: {
      ...headers,
      authorization: token,
    },
  };
});

export const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: authLink.concat(httpLink),
});

But I am getting the following error in the console.

POST http://localhost:4500/graphql 400 (Bad Request)
(anonymous) @ createHttpLink.ts:156
Subscription @ module.js:190
subscribe @ module.js:264
(anonymous) @ index.ts:22
Promise.then (async)
(anonymous) @ index.ts:17
Subscription @ module.js:190
subscribe @ module.js:264
complete @ Concast.ts:210
Concast.start @ Concast.ts:103
Concast @ Concast.ts:83
QueryManager.getObservableFromLink @ QueryManager.ts:978
QueryManager.getResultsFromLink @ QueryManager.ts:1037
resultsFromLink @ QueryManager.ts:1382
QueryManager.fetchQueryByPolicy @ QueryManager.ts:1412
fromVariables @ QueryManager.ts:1120
QueryManager.fetchQueryObservable @ QueryManager.ts:1141
ObservableQuery.fetch @ ObservableQuery.ts:591
ObservableQuery.reobserve @ ObservableQuery.ts:720
(anonymous) @ ObservableQuery.ts:136
Subscription @ module.js:190
subscribe @ module.js:264
(anonymous) @ useQuery.ts:155
invokePassiveEffectCreate @ react-dom.development.js:23487
callCallback @ react-dom.development.js:3945
invokeGuardedCallbackDev @ react-dom.development.js:3994
invokeGuardedCallback @ react-dom.development.js:4056
flushPassiveEffectsImpl @ react-dom.development.js:23574
unstable_runWithPriority @ scheduler.development.js:468
runWithPriority$1 @ react-dom.development.js:11276
flushPassiveEffects @ react-dom.development.js:23447
(anonymous) @ react-dom.development.js:23324
workLoop @ scheduler.development.js:417
flushWork @ scheduler.development.js:390
performWorkUntilDeadline @ scheduler.development.js:157

It says error is deriving from the createHttpLink.ts:156.

I am using Graphql version of ^16.3.0 and @apollo/client ^3.5.9. Any ideas?

apensia914 avatar Mar 31 '22 10:03 apensia914

"400 Bad Request" comes from the server, not the client.

dylanwulf avatar Apr 05 '22 15:04 dylanwulf

Any updates on this issue? Experiencing something similar.

wyork63 avatar May 01 '22 19:05 wyork63

"400 Bad Request" comes from the server, not the client.

400 Bad Request is a client-side error message.

daydy2 avatar Jul 14 '22 17:07 daydy2

Thanks for sharing this issue!

A few thoughts:

  1. Could you try upgrading to the most current version of our client? v 3.6.8?
  2. As @dylanwulf mentioned, the 400 seems to be pointing to an issue with the request being made to your server. I noticed you have the uri set to uri: "http://localhost:4500/graphql" So i'd double check if that is actually running or if you server configuration is perhaps not probable set up to work with the Apollo Client.
  3. Here are the docs on http link
  4. I also found a sample application here with Nexus + Apollo, maybe that will help you track down the issue.

jpvajda avatar Jul 15 '22 20:07 jpvajda

Same issue, did somebody fixed? I really need it.

Viistorrr avatar Jul 22 '22 00:07 Viistorrr

I had the same problem, the thing was a syntax error in my Types in my client (I use fragments) so all my calls (queries and mutations) were wrong. I hope it helps you

ChampiMagic avatar Aug 09 '22 18:08 ChampiMagic

I had the same problem, the thing was a syntax error in my Types in my client (I use fragments) so all my calls (queries and mutations) were wrong. I hope it helps you

Can you please elaborate, on how you resolved your issue?

BasudevBharatBhushan avatar Aug 23 '22 18:08 BasudevBharatBhushan

I had the same problem, the thing was a syntax error in my Types in my client (I use fragments) so all my calls (queries and mutations) were wrong. I hope it helps you

Can you please elaborate, on how you resolved your issue?

Yes. This is my code:

My Fragment: Captura de pantalla 2022-08-28 171115 MyQuery: Captura de pantalla 2022-08-28 171050

As you can see, I use my Fragment in all my Person type queries. My problem was that in the structure of my Fragment the "adress" property was misspelled.

I hope I have solved your doubt.

ChampiMagic avatar Aug 28 '22 20:08 ChampiMagic

Did anyone figure out how to look for problems that cause this issue? I can run a query in my local host, and in the apollo chrome inspector, but I cant run the mutation to create an object - the only error message is 'bad request' and similar blob as posted above. The network tab shows the error initiator as the createHttpLink. Where do I look for what the problem could be?

MincePie avatar Dec 10 '22 22:12 MincePie

Hi all - thanks for the conversation here. Because server responses with non-2xx status codes may contain payloads that do not conform to the GraphQL specification, your UI will have to define how to handle those payloads. This can be done in Apollo Client via Error Link. Hope that helps!

bignimbus avatar Dec 12 '22 17:12 bignimbus