1
votes

I'll set this up.

  • Backend: Node, GraphQL, Sequelize with a PostGRES DB.
  • Frontend: React, GraphQL.
  • I have models of a user, event and member.
  • A user can belong to many different events.
  • An event can have many users.
  • A user will have a specific role for each event.
  • I'm dynamically creating a member join table via Sequelize, like this:

    `Event.belongsToMany(models.User, {
       through: models.Member,
       foreignKey: {
         name: "eventId",
         field: "event_id"
       }
    });`
    
    
    `User.belongsToMany(models.Event, {
        through: models.Member,
        foreignKey: {
           name: "email",
           field: "email"
        }
    });`
    

My member model code does have one unique field, role. That file looks like this:

`export default (sequelize, DataTypes) => {
  const Member = sequelize.define("member", {
    role: DataTypes.STRING
  });
  return Member;
};`

In the end, I have a member table with a role field, and also fields tied to user (email) as well as event (eventId).


My two questions

  1. Front-end: I'm able to query the member table (allMembers), and map through each row's role, email and eventId. But how can I display the event name? (yes, it's part of my event model). For display purposes, the eventId is obviously meaningless to the end user.
  2. Front-end: Once a user sees their list of events they belong to, I'd like for them to be able to click on an event — placing that event's id into state. That way, I can use that eventId for other queries. Any suggestions on the best approach to achieve this?
1

1 Answers

0
votes

I would point you to GraphQL Relay. In GraphQL, you'd have:

interface Node {
  id: ID!
}

type Member implements Node {
  id: ID!
  role: Role!
  email: String!
  event: Event!
}

type Event implements Node {
  id
  ...whatever
}

type Role {
  ...whatever this has
}

type Query {
  node: Node
  member: Member
}

and you'd query for:

query GetPerson {
  member {
    id
    email
    role {
      ...whatever fields on role
    }
    event {
      id
      name
    }
  }
}

The "id" in all of these cases is the "NodeId", which is the base64encoded value of the :. e.g. RXZlbnQ6MTIzNDU2Nzg5MA== would be the ID for the event with the id 1234567890. That would give you enough information to show your first view. Then, to show your second view, you would make the query:

query GetEvent {
  node(id:"RXZlbnQ6MTIzNDU2Nzg5MA==") {
    ... on Event {
      name
      address
      whatever
    }
  }
}

This gives you enough information to fetch the data for this second view. On the server, the query.node resolver base64decodes the id you passed in back into Event:1234567890, then would make some getEventById("1234567890") and return that new object's stuff.

Remember: the value you get from GraphQL comes from the relationships your data points have with each other. You nest them as far as you need to, and you ask for everything you need to build your view, and the IDs you need to paint your next view. If you wanted to fetch things one object at a time, REST would have been good enough.