I have to fetch data from the api with graphql query by passing the variables

Hi All,

I have to fetch data from the api with graphql query by passing the dynamic arguments. But not sure How to get the data using arguments.

here is my query:

import { gql, useQuery } from “@apollo/client”;

export const SORT_DATA = gql`

query getAssetResourceListing($first: Int, $after: Int, $sortBy: [String], $sortOrder: [String]) {
getAssetResourceListing(first:$first, after:$after, sortBy:$sortBy, sortOrder:$sortOrder){
edges {
node {
Name
creationDate
modificationDate
Asset {
… on asset {
assetThumb: fullpath(thumbnail: “poc_thumbnail”)
}
}
AssetType {
… on object_AssetType {Name}
}
Product {
SKU_name:SKU
}
Project {
Name
}
}
}
}
}
`;

please help me to get the data with argumenst.

You would pass the arguments into useQuery under the variables portion. From what you have shown, it would be something like:

const { loading, error, data } = useQuery(GET_DOG_PHOTO, {
  variables: { 
    first: 10,
    after: 0,
    sortBy: [ "name" ],
    sortOrder: [ "Ascending" ]
  },
});

Note: I don’t know if the variable arguments are right, not sure how they work for the API you’re working with.

We also have some great GraphQL tutorials that cover this topic and more if you’re interested in learning more. There is also our documentation on queries that could be helpful.