GithubHelp home page GithubHelp logo

Comments (7)

resdevd avatar resdevd commented on May 17, 2024 1

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.

from graphql-react.

jaydenseric avatar jaydenseric commented on May 17, 2024

I don't think options.mode = 'no-cors' is a real option:

from graphql-react.

resdevd avatar resdevd commented on May 17, 2024

Thanks for responding.

Using localhost graphql api resulted in "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/api/graphql/. (Reason: CORS request did not succeed)." error.
Originally I tried all the options I could, including following.

const Index = () => {
const {loading, cacheValue = {}} = useGraphQL({
fetchOptionsOverride(options) {
options.url = 'http://localhost:8000/api/graphql/'
// options.credentials = 'same-origin'
},

    headers :{
        "Access-Control-Allow-Origin" : "*"
    },
    operation: {
        query: `

{
allArticles {
id
} }`
} })

With trial and error, for some reason using mode: 'no-cors', gets rid of the message. Now I understand it's not an option to be used.
Using external api seems to work well with the example.
Can you please point me towards right direction how to get localhost:8000 graphql api data into nextjs?

from graphql-react.

jaydenseric avatar jaydenseric commented on May 17, 2024

Can you please format your code blocks, I can't read that.

from graphql-react.

resdevd avatar resdevd commented on May 17, 2024

My bad. Here is the index.js file content. Rest of the code is not changed from "with-graphql-react" example.

import {useGraphQL} from 'graphql-react'

const Index = () => {
    const {loading, cacheValue = {}} = useGraphQL({
        fetchOptionsOverride(options) {
            options.url = 'http://localhost:8000/api/graphql/'
            // options.credentials = 'same-origin'
        },
        headers :{
            "Access-Control-Allow-Origin" : "*"
        },
        operation: {
            query: `
{
  allArticles {
    id
  }
}
             ` }})

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

export default Index

from graphql-react.

jaydenseric avatar jaydenseric commented on May 17, 2024

headers is not a valid useGraphQL option: https://github.com/jaydenseric/graphql-react#function-usegraphql

I don't know why you are having CORS problems, I run my Next.js app at http://localhost:3002 and GraphQL API at http://localhost:3003/graphql without problems.

from graphql-react.

resdevd avatar resdevd commented on May 17, 2024

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.

from graphql-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.