最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Graphql Cant Return Array - Stack Overflow

programmeradmin3浏览0评论

I am using Apollo-Server and trying to create a REST query against the IEX REST API which returns back data that looks like this:

{
  "symbol": "AAPL",
  "panyName": "Apple Inc.",
  "exchange": "Nasdaq Global Select",
  "industry": "Computer Hardware",
  "website": "",
  "description": "Apple Inc is an American multinational technology pany. It designs, manufactures, and markets mobile munication and media devices, personal puters, and portable digital music players.",
  "CEO": "Timothy D. Cook",
  "issueType": "cs",
  "sector": "Technology",
  "tags": [
      "Technology",
      "Consumer Electronics",
      "Computer Hardware"
  ]
}

I am using datasources. My typeDefs and resolvers look something like this:

const typeDefs = gql`
    type Query{
        stock(symbol:String): Stock
    }

    type Stock {
        panyName: String
        exchange: String
        industry: String
        tags: String!
    }
`;
const resolvers = {
    Query:{
        stock: async(root, {symbol}, {dataSources}) =>{
            return dataSources.myApi.getSomeData(symbol)
        }
    }
};

The Datasource file looks like this:

class MyApiextends RESTDataSource{
    constructor(){
        super();
        this.baseURL = '.0';
    }

    async getSomeData(symbol){
        return this.get(`/stock/${symbol}/pany`)
    }
}

module.exports = MyApi

I can run a query and get data back, but it is not formatting in an array and is throwing an error when I run a query like so:

query{
  stock(symbol:"aapl"){
    tags
  }
}

Error:

{
  "data": {
    "stock": null
  },
  "errors": [
    {
      "message": "String cannot represent value: [\"Technology\", \"Consumer Electronics\", \"Computer Hardware\"]",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "stock",
        "tags"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: String cannot represent value: [\"Technology\", \"Consumer Electronics\", \"Computer Hardware\"]",

The data I am expecting (technology, consumer electronics, and puter hardware) are correct, but not returning in an array. I tried to make a new type for tags, and set the it with a tag property, but the value just returns null.

I am very new to graphql, so any feedback is appreciated!

I am using Apollo-Server and trying to create a REST query against the IEX REST API which returns back data that looks like this:

{
  "symbol": "AAPL",
  "panyName": "Apple Inc.",
  "exchange": "Nasdaq Global Select",
  "industry": "Computer Hardware",
  "website": "http://www.apple.",
  "description": "Apple Inc is an American multinational technology pany. It designs, manufactures, and markets mobile munication and media devices, personal puters, and portable digital music players.",
  "CEO": "Timothy D. Cook",
  "issueType": "cs",
  "sector": "Technology",
  "tags": [
      "Technology",
      "Consumer Electronics",
      "Computer Hardware"
  ]
}

I am using datasources. My typeDefs and resolvers look something like this:

const typeDefs = gql`
    type Query{
        stock(symbol:String): Stock
    }

    type Stock {
        panyName: String
        exchange: String
        industry: String
        tags: String!
    }
`;
const resolvers = {
    Query:{
        stock: async(root, {symbol}, {dataSources}) =>{
            return dataSources.myApi.getSomeData(symbol)
        }
    }
};

The Datasource file looks like this:

class MyApiextends RESTDataSource{
    constructor(){
        super();
        this.baseURL = 'https://api.iextrading./1.0';
    }

    async getSomeData(symbol){
        return this.get(`/stock/${symbol}/pany`)
    }
}

module.exports = MyApi

I can run a query and get data back, but it is not formatting in an array and is throwing an error when I run a query like so:

query{
  stock(symbol:"aapl"){
    tags
  }
}

Error:

{
  "data": {
    "stock": null
  },
  "errors": [
    {
      "message": "String cannot represent value: [\"Technology\", \"Consumer Electronics\", \"Computer Hardware\"]",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "stock",
        "tags"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: String cannot represent value: [\"Technology\", \"Consumer Electronics\", \"Computer Hardware\"]",

The data I am expecting (technology, consumer electronics, and puter hardware) are correct, but not returning in an array. I tried to make a new type for tags, and set the it with a tag property, but the value just returns null.

I am very new to graphql, so any feedback is appreciated!

Share Improve this question edited Sep 20, 2018 at 12:19 foo_bar_zing asked Sep 20, 2018 at 3:40 foo_bar_zingfoo_bar_zing 1131 silver badge10 bronze badges 2
  • Wele to SO! It would be helpful to see your datasource code as well. Can you update your question to include that as well? – Daniel Rearden Commented Sep 20, 2018 at 12:08
  • Ok I added it to the question – foo_bar_zing Commented Sep 20, 2018 at 12:20
Add a ment  | 

1 Answer 1

Reset to default 5

Inside your type definition for Stock, you're defining the type for the tags field as String!:

tags: String!

That tells GraphQL to expect a String value that will not be null. The actual data being returned by the REST endpoint, however, is not a String -- it's an array of Strings. So your definition should minimally look like this:

tags: [String]

If you want GraphQL to throw if the tags value is null, add an exclamation point to the end to make it non-nullable:

tags: [String]!

If you want GraphQL to throw if any of the values inside the array are null, add an exclamation point inside the brackets. You can also bine the two:

tags: [String!]!
发布评论

评论列表(0)

  1. 暂无评论