I am creating a gql express server and i have created multiple schemas and referenced those schemas into each other. Assume collection USERS
. Each user will have multiple challenges(schema or collection - challenge
). I want to store the document(challenge) id into an array of challenges inside the user document that created it and vice-cersa.How can I acheive this?
My user Schema -
import mongoose from "mongoose";
import { composeWithMongoose } from "graphql-compose-mongoose";
const { Schema } = mongoose;
export const UserSchema = new Schema(
{
name: {
type: String,
trim: true,
required: true,
},
email: {
type: String,
lowercase: true,
trim: true,
unique: true,
required: true,
},
challenge: [
{
type: Schema.Types.ObjectId,
ref: "Challenge",
required: true,
},
],
},
{
timestamps: {
createdAt: true,
updatedAt: true,
},
}
);
export const User = mongoose.model("User", UserSchema);
export const UserTC = composeWithMongoose(User);
My challenge Schema -
import mongoose from "mongoose";
import { composeWithMongoose } from "graphql-compose-mongoose";
const { Schema } = mongoose;
export const ChallengeSchema = new Schema(
{
user: [
{
type: Schema.Types.ObjectId,
ref: "User",
required: true,
},
],
task: {
type: String,
trim: true,
required: true,
},
description: {
type: String,
trim: true,
required: true,
},
},
{
timestamps: {
createdAt: true,
updatedAt: true,
},
}
);
export const Challenge = mongoose.model("Challenge", ChallengeSchema);
export const ChallengeTC = composeWithMongoose(Challenge);
I have to also perform gql query to get the challenges details of the the user and also have to query all the users of a particular challenge.