Jeff
Jeff

Reputation: 35

How do I pass a variable on the graphql filter?

In the section below, ref, and queryName work fine (so long as you pass the full graphql query, and not just the name of it. In any case, columnName does not seem to accept a variable as an input. When the post request is examined, it just says "columnName" instead of id or whatever I happen to use as the search criteria when I specified columnName on the function call.

export async function fetchItems(ref, queryName, columnName) {
     const queryNameData = await API.graphql(graphqlOperation(queryName, {
          filter: {
               columnName: {
                    contains: ref,
               },
          }
     }))

     const { items } = queryNameData.data
     return (items)
}

Here is an example of the variables section of the graphql post to dynamodb:

{
    variables: {
        filter: {
            columnName: {
                contains: "062"
            }
        }
    }
}

The queryName and ref are fine, but in the present case "columnName" should be "id" as that is what I set columName equal to for the function call.

Upvotes: 0

Views: 1083

Answers (1)

xadm
xadm

Reputation: 8418

Prepare filter object using passed params like:

export async function fetchItems(ref, queryName, columnName) {
     let filter = {};
     filter[columnName] = { contains: ref };
     const queryNameData = await API.graphql(graphqlOperation(queryName, { filter }))

Upvotes: 1

Related Questions