graphql-kit / graphql-faker

🎲 Mock or extend your GraphQL API with faked data. No coding required.
MIT License
2.69k stars 225 forks source link

faking graphql api using gaphql faker does not seem to query fields by id #150

Open rosnk opened 3 years ago

rosnk commented 3 years ago

trying to create mock api using GraphQl faker.

Trying to achieve:

want to display categories (array list) on first render of app. and when user clicks particular category, want to call second graphql query to fetch products under that cateogry.

Basically trying to create fake graphql api to do so.

Current code

current schema:

type Picture {
  id: ID
  url: String @fake(type:imageUrl, options:{imageKeywords:["car", "bike","yatch","house"]})
}

type ProductItem {
  id: ID
  title: String @fake(type: productName)
  description: String @fake(type:lorem, options:{loremSize:paragraphs})
  picture: [Picture!]! 
}

 type Category {
  id: ID
  name: String
    @examples(
      values: [
        "car"
        "house"
        "electronics"
      ]
    )
  total: Int
  category_picture:String @fake(type:imageUrl, options:{imageKeywords:["car", "bike","yatch","house"]})
  category_items: [ProductItem!] @listLength(min: 2, max: 2)

}

type Query {
  allCategory: [Category!]
  productByItemId(id: ID!): ProductItem

} 

current query:


query{
  allCategory{
    id
    name
    category_picture
    category_items {
      id
    }
  }, 
  productByItemId(id:"NzAyMjA3ODg0MA==") {
    id
  }
}

output result:


{
  "data": {
    "allCategory": [
      {
        "id": "MzAwMzM3ODg0MQ==",
        "name": "electronics",
        "category_picture": "https://source.unsplash.com/random/?car,bike,yatch,house",
        "category_items": [
          {
            "id": "NzcyMDAwNzc4MA=="
          },
          {
            "id": "MzIwNjkzNzI4NQ=="
          }
        ]
      },
      {
        "id": "MzYwNzgyNDQ0",
        "name": "car",
        "category_picture": "https://source.unsplash.com/random/?car,bike,yatch,house",
        "category_items": [
          {
            "id": "ODkwNzEwODU5Nw=="
          },
          {
            "id": "MTQ3NjU2MTg5MA=="
          }
        ]
      },
      {
        "id": "MTI5NDM4MDc5OA==",
        "name": "house",
        "category_picture": "https://source.unsplash.com/random/?car,bike,yatch,house",
        "category_items": [
          {
            "id": "MjEzMjQyNDE4Mw=="
          },
          {
            "id": "MzAxOTM0NjMxNw=="
          }
        ]
      },
      {
        "id": "MzUzODk1NzY2Ng==",
        "name": "electronics",
        "category_picture": "https://source.unsplash.com/random/?car,bike,yatch,house",
        "category_items": [
          {
            "id": "MTMwODcyMDc2"
          },
          {
            "id": "NDQ5OTg2ODQ2Ng=="
          }
        ]
      }
    ],
    "productByItemId": {
      "id": "OTYyODEyNzYwNg=="
    }
  }
}

Problem

productByItemId(id:"NzAyMjA3ODg0MA==").... in this line that id is no where to be found.

I basically want to mock and use fake graphql api in my project...something like fetching category and also fetching products that matches id of particular category. Any solution is highly appreciated.... please also recommend any alternatives to graphql faker if there are any.