0

I've already tried all the solutions I've found on stackoverflow but I still have my problem: I got a null for any REST API's call on Graphql

on my REST API's Swagger i get :

{
  "_embedded": {
    "marqueDtoList": [
      {
        "marque": "ALPINE RENAULT",
        "cmarque": "AL",
        "_links": {
          "self": {
            "href": "http://localhost:8081/demo/api-c/referentiel/marques/v1/AL"
          },
          "modeles": {
            "href": "http://localhost:8081/demo/api-c/referentiel/marques/v1/AL/modeles",
            "templated": true
          }
        }
      },
      {
        "marque": "CITROEN",
        "cmarque": "CI",
        "_links": {
          "self": {
            "href": "http://localhost:8081/demo/api-c/referentiel/marques/v1/CI"
          },
          "modeles": {
            "href": "http://localhost:8081/demo/api-c/referentiel/marques/v1/AL/modeles",
            "templated": true
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8081/demo/api-c/referentiel/marques/v1"
    }
  }
}

my Schema is :

type Query {
    callAPI: ApiStructure
}

type ApiStructure {
  _embedded : _embedded
  _links : _links
}

type _embedded {
  marqueDtoList : [marqueDto]
}

type marqueDto {
  marque: String
  cmarque: String
  _links : _links
}

type _links {
  self: self
  modeles : modeles
}

type self {
  href : String
}

type modeles {
  href : String
  templated : Boolean
}

and my resolvers is :

  callAPI: (root, args) => {
    let url = 'http://localhost:8081/demo/api-c/referentiel/marques/v1'
    let options = {
      method: 'GET',
      headers: {
        'content-type': 'application/json',
      },
    };
    return fetch(url, options)
    .then(res => (res.json()))
    .then(json => JSON.stringify(json))
    .then(console.log)
    .catch(console.error);
  }
}

when i tried my query on graphiQL, i got this

however, console gives me exactly the same result as what I get in my swagger, does anyone know why ?

Nairo
  • 61
  • 9
  • `.then(console.log)` - this returns nothing – xadm Jun 20 '19 at 13:55
  • with or without it, I always get null – Nairo Jun 20 '19 at 14:08
  • what was logged? – xadm Jun 20 '19 at 14:34
  • By using `JSON.stringify` on result, you are returning a String instead of an Object. That whole `then` (and the one with the `console.log` as xadm pointed out) should be omitted. Please see *Understanding default resolver behavior* [here](https://stackoverflow.com/a/56319138/6024220). – Daniel Rearden Jun 20 '19 at 14:52

0 Answers0