Skip to main content

Working with the Quiz Object

There’s more moving pieces with a quiz then compared to fetching a text page for example. With a quiz, you have to both fetch data and then allow the user to send data back in the form of a quiz submission. We’ll walk you through the various API calls you’ll need to use do that here.

Fetching the Quiz Questions

The first order of business is actually getting the quiz questions to show the users. We’ll do that with the Pages query.

query Pages($identifiers: [String!]!) {
Pages(identifiers: $identifiers) {
... on QuizPage {
title
questions {
body
choices {
value
correct
}
}
}
}
}

Creating an Assessment Attempt

Next, we need to let the user create a quiz attempt. We’ll do that with the LoadAssessmentAttemptWithQuestions query. Given the headless nature of Helium, the choice is completely up to you when you want to call this query. Note: most users that land on a quiz page will usually submit a quiz, so it might make sense to create an assessment attempt as soon as the user lands on a quiz page. You could also do it when the user answers the first question.

query {
LoadAssessmentAttemptWithQuestions(
id: $id
courseId: $courseId
topicType: $topicType
) {
id
status
grade
}
}

Updating the Assessment Attempt

Now that we have the assessment attempt, we need to update it each time a user answers a question. We’ll use the UpdateAssessment mutation for this.

mutation UpdateAssessmentAttempt(
$activeQuestion: QuestionInput,
$assessmentAttempt: AssessmentAttemptInput
) {
UpdateAssessmentAttempt(
activeQuestion: $activeQuestion,
assessmentAttempt: $assessmentAttempt
) {
id
grade
}
}

// SAMPLE VARIABLES
{
"assessmentAttempt": {
"id": "{{ id from LoadAssessmentAttemptWithQuestions }}",
"status": "started"
},
"activeQuestion": {
"body": "<p>Who is the current quarterback for the Eagles?</p>",
"mustSelectAllCorrectChoices": true,
"selectedChoice": {
"value": "Jalen Hurts",
"correct": true
}
}
}

Submitting the Assessment Attempt

Awesome, the user has finished the quiz and is ready to click the submit button. To submit the quiz submission, we’ll call the UpdateAssessment mutation one last time

mutation UpdateAssessmentAttempt(
$assessmentAttempt: AssessmentAttemptInput
) {
UpdateAssessmentAttempt(
assessmentAttempt: $assessmentAttempt
) {
id
grade
}
}

// SAMPLE VARIABLES
{
"assessmentAttempt": {
"id": "{{ id from LoadAssessmentAttemptWithQuestions }}",
"status": "finished"
}
}

Still have a question?

Get your questions answered in our Developer Discord.