Context
- There are three entities, Respondent, Question, and Choice.
- A respondent is a person responding to questions.
- A respondent can be asked multiple questions.
- A question can contain one or multiple choices.
- A choice can belong to one or multiple questions.
- A respondent can select none or multiple choices.
- Unique identifiers:
username
for Respondent,qid
for Question, andcid
for Choice.
Schema:
type Question {
qid: String! @id
text: String
choices: [Choice] @hasInverse(field: questions)
respondents: [Respondent]
}
type Choice {
cid: String! @id
text: String
questions: [Question]
respondents: [Respondent]
}
type Respondent {
username: String! @id
questions: [Question] @hasInverse(field: respondents)
choices: [Choice] @hasInverse(field: respondents)
}
First mutation
Starting fresh, inserting a respondent answering a question with certain choices.
mutation ($input: [AddRespondentInput!]!) {
addRespondent(input: $input) {
numUids
}
}
{
"input": [
{
"username": "arya",
"questions": [
{
"qid": "qid1",
"text": "What cars do you currently own?",
"choices": [
{
"cid": "cid1",
"text": "Toyota"
}
]
}
]
}
]
}
Query respondents
query {
queryRespondent {
username
questions {
qid
text
choices {
cid
text
}
}
choices {
cid
text
questions {
qid
text
}
}
}
}
# result:
{
"queryRespondent": [
{
"username": "arya",
"questions": [
{
"qid": "qid1",
"text": "What cars do you currently own?",
"choices": [
{
"cid": "cid1",
"text": "Toyota"
}
]
}
],
"choices": [] # choices are empty here
}
]
}
Second mutation
Add another respondent answering the same question but with different choices.
{
"input": [
{
"username": "john",
"questions": [
{
"qid": "qid1",
"text": "What cars do you currently own?",
"choices": [
{
"cid": "cid1",
"text": "Toyota"
},
{
"cid": "cid2",
"text": "BMW"
}
]
}
]
}
]
}
Query respondents again
{
"queryRespondent": [
{
"username": "arya",
"questions": [
{
"qid": "qid1",
"text": "What cars do you currently own?",
"choices": [
{
"cid": "cid1",
"text": "Toyota"
}
]
}
],
"choices": []
},
{
"username": "john",
"questions": [
{
"qid": "qid1",
"text": "What cars do you currently own?",
"choices": [
{
"cid": "cid1",
"text": "Toyota"
}
]
}
],
"choices": []
}
]
}
Questions
- In the first mutation, I expect
choices
to be linked torespondents
, but it was not the case. What am I doing wrong here? - In the second mutation,
john
choseToyota
andBMW
, butBMW
wasn’t added. Why is that?