question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using localhost graphql API results in error 400.

See original GitHub issue

I’m using “with-graphql-react” example from next.js repo. During development, I’m using django backend’s graphql api from localhost:8000. However I’m stuck at initial step with error response 400.

My index.js:

import {useGraphQL} from ‘graphql-react’

const Index = () => { const {loading, cacheValue = {}} = useGraphQL({ fetchOptionsOverride(options) { options.url = ‘http://localhost:8000/api/graphql/’ options.mode = ‘no-cors’ }, operation: { query: { allArticles { id } } } })

return cacheValue.data ? (
    
    <p>Hello</p>
) : loading ? (
    <p>Loading…</p>
) : (
    <p>Error!</p>
)

}

export default Index

This query is working fine in graphiql, however using this in nextjs results to two following alternating errors below from web inspector:

“message”: “Must provide query string.”

and

Error response

Error code: 400

Message: Bad request syntax (‘{“query”:"\n{\n allArticles {\n id\n }\n}\n "}POST /api/graphql/ HTTP/1.1’).

Error code explanation: HTTPStatus.BAD_REQUEST - Bad request syntax or unsupported method.

Can anyone point me towards right direction? Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
resdevdcommented, Mar 7, 2019

Ah got it. I’ve been using Django graphene GraphQL backend. I suspect it’s how Django handling cors resulting in this error. I’ll work on that part. I will close this now, comment on this in case of an update. Hopefully this resolves cors issue and data fetching from django graphql api into nextjs. Thanks for your help. Appreciate this library you’ve developed.

0reactions
resdevdcommented, Mar 7, 2019

If anyone using Django graphql backend and using react frontend using nextjs “with-graphql-react” example. You need to install python package django-cors-headers in your backend and follow the instructions specified in order to avoid cors issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apollo-client returns "400 (Bad Request) Error" on sending ...
400 errors generally mean there's something off with the query itself. In this instance, you've defined (and you're passing in) a variable ...
Read more >
How to Fix a 400 Bad Request Error (Causes and Fixes) - Kinsta
The 400 Bad Request error indicates that the server cannot or process the request due to a client error. Read about the common...
Read more >
400 Bad Request Error: What It Is and How to Fix It
The 400 Bad Request Error is an HTTP response status code indicating that the server was unable to process the request sent by...
Read more >
Hygraph error codes
Hygraph will return a 400 if there is an error with the request. 400 errors are typically thrown when there is a client-side...
Read more >
HTTP API Errors - commercetools documentation
Errors returned for a request. A single error response can contain multiple errors if the errors are related to the same HTTP status...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found