I’m excited about Redwood and hope to be involved in the community as it continues to evolve.
I’m working on a simple application to explore Redwood. It is a ‘Daily Questions’ app where you can track a handful of questions over time, inspired by the practice Marshall Goldsmith describes in his book ‘Triggers’.
What I want to accomplish is being able to create a new ‘Answer’ that I can connect to an already existing ‘Question’. I’ve been looking through various graphql, apollo, and prisma documentation to try to find the right syntax/patterns to use but am a bit confused.
Questions:
How should the following files be written to successfully create a relation between an Answer and a Question?
AnswerInput input type in sdl file
createAnswer mutation type in the sdl file
createAnswer service in the services file
CREATE_ANSWER_MUTATION gql string in the NewAnswer component
What documentation resources would you recommend if I were trying to answer these questions for myself?
Thanks in advance for your consideration!
Code
Model Definition
// api/prisma/schema.prisma
model Question {
id Int @id @default(autoincrement())
name String
type String
answers Answer[]
}
model Answer {
id Int @id @default(autoincrement())
content String
question Question
createdAt DateTime @default(now())
}
Those docs were really helpful and I’m definitely closer, but haven’t gotten it to work yet. Do you know if there exists yet a Redwood sample app that implements a 1:n relationship (e.g. User:Post, Author:Book, TodoList:Todos, etc.)?
I’m a little bit in a situation of not knowing what I don’t know, and suspect I’m missing something obvious.
Here are condensed snippets of the relevant code. This of course would not be the final implementation, I’m just trying to successfully create an Answer in the db and have it be linked to a Question. Reading all the docs you listed, I don’t see anything obvious that I’m missing.
Hey @alex - I know this is a bit old so you might have figured it out or moved on. But thought I would chime in to help future people that may be struggling with this. In this case - you did almost everything correct, but it should be
input AnswerInput {
content: String
questionId: Int
}
Thank you @twmilli that’s really help me to understand how to create a one-to-many relationship.
It took me some time to understand that you can’t write the “question” object inside the “input” variable because “question” cannot be on your Schema Definition Input and GraphQL give you an error for that.
I’m curious if someone knows a method to not rewrite all the key properties of your “input” inside the data object in the Service? It can be very long if you have a big object!