Tutorial: Building basic queries

This tutorial guides you through the process of creating basic queries with Query API.

Simple query: One field with no filters

This simple feedback query retrieves one specific field — e_email — from the first 30 records in the feedback database. Query API returns up to 30 results unless you specify a different limit, see Pagination for more details.

Example: Simplest version

query myQueryName {
  feedback {
    nodes {
      user_email: fieldData(fieldId: "e_email") {
        values
      }
    }
  }
}
Note: While the server can process requests when the query keyword and query name are omitted, Medallia recommends that you use them in your requests to easily identify and monitor the response of each query, given that you can send multiple queries in a single request.

Aliases let you rename the result of a field or an explicit operation such as aggregateQuery. Note that, in the example above, we define an alias — user_email — for the fieldData operation.

Sample response

{
  "data": {
    "feedback": {
      "nodes": [
        {
          "user_email": {
            "values": ["medalliademo_1@example.com"]
          }
        },

   ...

        {
          "user_email": {
            "values": ["medalliademo_30@example.com"]
          }
        }
      ]
    }
  },
  "errors": null,
  "_links": null,
  "_allowed": [
    "POST",
    "GET"
  ]
}

The next example is basically the same query, but this time we use surveyId as the ID for each element on the list. The current implementation of Query API uses id as a keyword that brings back the value of field a_surveyid.

Adding totalCount after the node definition brings back a count of all elements on the list.

Example: Getting more info

query myQueryName {
  feedback {
    nodes {
      surveyId: id
      user_email: fieldData(fieldId: "e_email") {
        values
      }
    }
    totalCount
  }
}

Sample response

{
  "data": {
    "feedback": {
      "nodes": [
        {
          "surveyId": "155046",
          "user_email": {
            "values": ["medalliademo_1@example.com"]
          }
        },

   ...

        {
          "surveyId": "155017",
          "user_email": {
            "values": ["medalliademo_30@example.com"]
          }
        }
      ],
      "totalCount": 37437
    }
  },
  "errors": null,
  "_links": null,
  "_allowed": [
    "POST",
    "GET"
  ]
}

Simple query: More than one field with no filters

Fetch more than one field from each record using the same structure shown above for every desired field. In the following example, in addition to e_email, we fetch the field a_fullname.

Example: Fetching more than one field

query myQueryName {
  feedback {
    nodes {
      surveyId: id
      user_email: fieldData(fieldId: "e_email") {
        values
      }
      user_name: fieldData(fieldId: "a_fullname") {
        values
      }
    }
    totalCount
  }
}

Sample response

{
  "data": {
    "feedback": {
      "nodes": [
        {
          "surveyId": "145700",
          "user_email": {
            "values": [
              "jsmith@medallia.com"
            ]
          },
          "user_name": {
            "values": [
              "John, Smith"
            ]
          }
        },

   ...

        {
          "surveyId": "145491",
          "user_email": {
            "values": [
              "jjohnson@medallia.com"
            ]
          },
          "user_name": {
            "values": [
              "Jane, Johnson"
            ]
          }
        }
      ],
      "totalCount": 47
    }
  },
  "errors": null,
  "_links": null,
  "_allowed": [
    "POST",
    "GET"
  ]
}

However, adding one fieldData block for each field you need to retrieve is impractical. In the example below, we improved the query by using the fieldDataList operation, which accepts a list of comma-separated fields as an argument.

Example: fieldDataList operation

query myQueryName
  {
  feedback {
    nodes {
      surveyId: id
      
      requested_field_list: fieldDataList(fieldIds: ["e_email","a_fullname"]) {
        values
      } 
    }
    totalCount
  }
}

Filtered query: Including filters

You can use filter conditions before the feedback block definition starts to narrow down your search results. You need to add a filter statement, where you can group clauses with or and and boolean operators. In the example below, we set a date range based on e_responsedate.

Note: Too see detailed information about complex filters, see Filtering and sorting data, Filtering by date ranges and Filtering by segment.

Example: Setting explicit filter values

query myQueryName {
  feedback(
    filter: {
      and: [
        { fieldIds: "e_responsedate", gte: "2019-09-01 12:00:00" }
        { fieldIds: "e_responsedate", lt: "2019-12-31 13:00:00" }
      ]
    }
  ) {
    nodes {
      surveyId: id
 
      requested_field_list: fieldDataList(fieldIds: ["e_email", "a_fullname"]) {
        values
      }
    }
    totalCount
  }
}

Sample response

{
  "data": {
    "feedback": {
      "nodes": [
        {
          "surveyId": "75058",
          "requested_field_list": [
            {
              "values": [
                "medalliademo_159957@example.com"
              ]
            },
            {
              "values": [
                "Ramos, Katherine"
              ]
            }
          ]
        },

        ...
     
    	{
          "surveyId": "74963",
          "requested_field_list": [
            {
              "values": [
                "medalliademo_155297@example.com"
              ]
            },
            {
              "values": [
                "Rose, Anne"
              ]
            }
          ]
        }
      ],
      "totalCount": 419
    }
  },
  "errors": null,
  "_links": null,
  "_allowed": [
    "POST",
    "GET"
  ]
}

To use an argument several times in a single query, define a variable and reference it as many times as you need along the query. In the example below, we define startDate and endDate as variables, and then use them in the filter.

Example: Using variables

query myQueryName(
  $start_date: String = "2019-09-01 12:00:00"
  $end_date: String = "2019-12-31 13:00:00"
) {
  feedback(
    filter: {
      and: [
        { fieldIds: "e_responsedate", gte: $start_date }
        { fieldIds: "e_responsedate", lt: $end_date }
      ]
    }
  ) {
    nodes {
      surveyId: id
 
      requested_field_list: fieldDataList(fieldIds: ["e_email", "a_fullname"]) {
        values
      }
    }
    totalCount
  }
}

Filtered query: Passing variables as a separate argument

Optionally, you can send variables in a separate POST argument apart from the query definition. Using the same example, we can take startDate and endDate and pass them as arguments in the Variables payload.

Example: Data query

query myQueryName(
  $start_date: String
  $end_date: String
) {
  feedback(
    filter: {
      and: [
        { fieldIds: "e_responsedate", gte: $start_date }
        { fieldIds: "e_responsedate", lt: $end_date }
      ]
    }
  ) {
    nodes {
      surveyId: id
 
      requested_field_list: fieldDataList(fieldIds: ["e_email", "a_fullname"]) {
        values
      }
    }
    totalCount
  }
}

Example: Variables

{
    "start_date": "2019-09-01 12:00:00",
    "end_date": "2019-12-31 13:00:00"
}