Say I have two types: `User` and `Event`
# orm-help
m
Say I have two types:
User
and
Event
w
why not having and array of user IDs in Event type called
attendingUsers
or something like that
in that case you can have 2 arrays
maybeGoing
goingForSure
m
Thanks for the reply. With this approach, how will I add users to
attendingUsers
from a resolver function?
w
there should be two functions
create
and
connect
, create will create a user and connect will connect it with given ID!
Copy code
...
 tags:{
      create: {name: "23edsaasdas"}
    }
    owner: {
      connect:{auth0id:"chickens-and-sloths" }
    }
...
m
So I have this:
Copy code
type Event {
  id: ID! @unique
  organizer: User!
  title: String!
  description: String!
  location: String!
  date: DateTime!
  participants: [User!]!
}

type User {
  id: ID! @unique
  email: String! @unique
  password: String!
  name: String!
  events: [Event!]!
}
A user can be the organizer of an event and other users can attend the event. With these definitions, how do add a user as a participant to an event from a resolver function?
w
can you share your resolver? are you forwarding it to the DB or not. it’s quite straight forward with prisma, schema should tell you that there is connect and create method on that relation since
[String!]!
is a relation (prisma) and in ‘normal’ graphql is a larray of some strings
m
This is the resolver to create a new event:
Copy code
const event = {
  async createEvent (
    parent,
    { title, description, date, location },
    ctx,
    info
  ) {
    const userId = getUserId(ctx)
    return ctx.db.mutation.createEvent(
      {
        data: {
          title,
          description,
          date,
          location,
          organizer: {
            connect: { id: userId }
          }
        }
      },
      info
    )
  }
}
w
Copy code
const event = {
  async createEvent(
    parent,
    {title, description, date, location, participants},
    ctx,
    info,
  ) {
    // participants are in format of ["ID", "another-ID", "yeti-another-ID"]
    const userId = getUserId(ctx)
    return ctx.db.mutation.createEvent(
      {
        data: {
          title,
          description,
          date,
          location,
          organizer: {
            connect: {id: userId},
          },
          participants: {
            // case you have the IDS
            connect: participants.map(id => {
              return {id: id}
            }),
          },
        },
      },
      info,
    )
  },
}
something like this? maybe
m
This will work if I want to create participants at the point of creating an event
I want to be able to create event separately and add participants separately
w
my example will work if you want to
connect
them. that means you have the IDs already.
in your mutation request add
participants: {}
and that should create empty array
in the DB. after that you can use your
update
method with
connect
m
It worked, thanks buddy. But now I have an error
I'm trying to return a string instead of returning the whole event after performing an update
I already set the return type to
String
but I'm getting this error: Field updateEvent of type Event must have a selection of subfields
w
Im sure you need to respect the types 🙂