Q: How does the subscription get triggered in the example?
A: In the example, a createUser mutation is implemented. Whenever this mutation is called, the code publishes a “newUser” event via the PubSub, so any client subscribed to that event receives the new user data in real time.
Q: What database is used in the example, and why?
A: The tutorial uses MongoDB (via Mongoose) as the database to store user data. It’s chosen likely for simplicity and because NestJS integrates well with it.
Q: What setup is needed to enable GraphQL subscriptions in a NestJS app?
A: You need to install the GraphQL + subscription libraries (@nestjs/graphql, @nestjs/apollo, plus subscriptions-transport-ws or graphql-ws), configure your GraphQL module to enable WebSocket transport, define resolvers for subscription events in addition to queries/mutations, and use a PubSub mechanism to publish events.
Q: Why choose NestJS for real-time GraphQL subscriptions?
A: NestJS provides a modular architecture out of the box, TypeScript support, dependency injection, and built-in support for GraphQL via packages like @nestjs/graphql. It’s well-suited for scalable server applications needing structured code.
Q: What are some likely pitfalls when using subscriptions in React + Apollo?
Ensuring you unsubscribe properly when components unmount. Handling WebSocket reconnection or dropped connections. Keeping the client cache in sync when subscription events occur. Making sure backend supports subscriptions and has a matching schema.
Q: What’s the folder or component structure in the example app?
A: There’s a React app with components like a form to create new users, a list component showing users, and a table UI. The app uses a React Router for navigating between list view and form view. Subscription logic is placed in a component listening for new user registration events.
Q: What libraries/packages are needed for subscriptions in this setup?
A: According to the tutorial: @apollo/client, graphql, and a WebSocket transport library such as subscriptions-transport-ws. Also tools like Formik/Yup for form validation are used in the example.
Q: How do I set up Apollo Client to handle both queries/mutations and subscriptions in React?
A: You use two different links: an HTTP link for queries/mutations and a WebSocket link for subscriptions. Then wrap your React app in ApolloProvider, use hooks like useQuery for queries and useSubscription for real-time updates.
Q: What problems do GraphQL Subscriptions solve compared to polling or plain HTTP?
A: Subscriptions allow clients to receive real-time updates over WebSockets, so you don’t need to make repeated HTTP requests at intervals. This reduces server load, latency, and improves user experience.
Q: What are the security considerations in this deployment?
A: Some include: using SSH via keys rather than weak password auth, ensuring only necessary ports are open, handling secrets (e.g. in GitHub Actions), and likely using least privilege access. The blog mentions they used password auth but plan to look at SSH key auth for better security.