Title
m

mezie

04/11/2018, 10:04 AM
Say I have two types:
User
and
Event
w

woss

04/11/2018, 11:15 AM
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

mezie

04/11/2018, 9:25 PM
Thanks for the reply. With this approach, how will I add users to
attendingUsers
from a resolver function?
w

woss

04/12/2018, 9:04 AM
there should be two functions
create
and
connect
, create will create a user and connect will connect it with given ID!
...
 tags:{
      create: {name: "23edsaasdas"}
    }
    owner: {
      connect:{auth0id:"chickens-and-sloths" }
    }
...
m

mezie

04/12/2018, 10:01 AM
So I have this:
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

woss

04/12/2018, 10:47 AM
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

mezie

04/12/2018, 12:09 PM
This is the resolver to create a new event:
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

woss

04/12/2018, 12:41 PM
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

mezie

04/12/2018, 1:05 PM
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

woss

04/12/2018, 2:34 PM
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

mezie

04/14/2018, 4:31 AM
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

woss

04/16/2018, 7:57 AM
Im sure you need to respect the types 🙂