0

i have API Documentation built with GraphQL API, with some of search i find tutorial but its not enough. the api contain authorization. like below: Login

mutation {
 login(
 input: {
 username: "customer",
 password: "123456",
 }
 ){
 token
 }
 }


and the header Headers after login

authorization: Bearer(login token),
content-language: ‘ar’

and the endpoint its subdomain http://accuratess.holol-aldiar.com.sa/

all i need its fetch data with javascript to qury the data

query {
 shipment(id: 1) {
 id
 code
 customerDue
 timeFrom
 timeTo
 latitude
 longitude
 collected
 paidToCustomer
 paidToDeliveryAgent
 deletable
 createdAt
 description
 weight
 piecesCoun
}
}

its shipment tracking

and the js code:

fetch('http://accuratess.holol-aldiar.com.sa',  {
    mode: 'no-cors',
    method: 'POST',
    headers:{"Content-Type": "application/json"},
    body: JSON.stringify({
        query:`
        
        query {
            shipment(id: 1) {
            id
            code
            customerDue
            }
        }
        
        
        `
    })
}).then(res => res.json()).then(
    data =>{
        console.log(data.data)
    }
)

Deem
  • 1
  • 2
  • You need to include your other headers in the request: `authorization: Bearer(login token), content-language: ‘ar’` - Using fetch is annoying though, life is easier with [apollo-client](https://www.apollographql.com/docs/react/) – Michel Floyd Jun 13 '23 at 19:38
  • 1
    You said `mode: 'no-cors',` which **prevents** (a) Setting headers and (b) Reading the response – Quentin Jun 13 '23 at 19:41
  • @MichelFloyd i use javascript i am don't use any framework. where exactly can i put the request – Deem Jun 13 '23 at 20:35
  • @Quentin but when remove mode:'no-cors' i face cors problem – Deem Jun 13 '23 at 20:37
  • @Deem — You face the CORS problem anyway. `mode: 'no-cors'` essentially sticks your head in the sand and tries to ignore it. You need to deal with CORS in the [correct fashion](https://stackoverflow.com/a/35553666/19068). – Quentin Jun 13 '23 at 20:39

0 Answers0