Skip to content

Query with variables return null #1296

Closed
@kejoRx

Description

@kejoRx
const typeDefs = `
    type Item  {
        id: ID!
        sku: String!
        name: String!
        attribute_set_id: Int!
        price: Float
        status: Int!
        visibility: Int!
        type_id: String!
        created_at: String!
        updated_at: String!
        product_links: [String]
        tier_prices: [Int]
        custom_attributes: [CUSTOM_ATTRIBUTES]
    }
        
    union CUSTOM_ATTRIBUTES = CustomString | CustomArray
    
   type CustomString {
    attribute_code: String
    value: String
  }
  
  type CustomArray {
    attribute_code: String
    value: [String]
    
  } 

  type Query {
    allItems(value: [String]!): [Item]!
    
  }
`;

Resolver func

export const resolvers = {
    Query: {
        allItems: (_, { value }) => getAllLinks().then(result => filter(result, {value: value}))
        //allItems: () => getAllLinks(),

    },

Graphiql query:

query FetchItems($values : [String]!){
  allItems(value : $values){
    id
    name
    custom_attributes{
      __typename
      ... on CustomString{
        attribute_code
        valString: value
      }
      ... on CustomArray{
        attribute_code
        valArray: value
      }
    }
  }
}

Query Variables:

{
  "values": ["3"]
}

Output:

{
  "data": {
    "allItems": []
  }
}

It should return items that have "3" (or any other number) as string in value in typename CustomArray (that's where is category_ids), but it returns null. Why?

This is the first item in api:

{
                "id": 1,
                "name": "Some name",
                "some others" : "that not matter"
                "custom_attributes": [
                    {
                        "attribute_code": "description",
                        "value": "<p>Here's is description</p>"
                    },
                    {
                        "attribute_code": "image",
                        "value": "/m/b/mb01-blue-0.jpg"
                    },
                    {
                        "attribute_code": "small_image",
                        "value": "/m/b/mb01-blue-0.jpg"
                    },
                    {
                        "attribute_code": "thumbnail",
                        "value": "/m/b/mb01-blue-0.jpg"
                    },
                    {
                        "attribute_code": "category_ids",
                        "value": [
                            "3",
                            "4"
                        ]
                    },
                    {
                       "some others" : "that not matter as well"
                    },
                   
                ]
            },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions