Project using GraphQL (GQL) and Typescript utilising code-first generated schemas - bootstrapped with @nestjs/cli.
For first time running, or when new packages have been added or editted
npm inpm start
# or
npm run devOpen at http://localhost:53373/graphql
npm run build
npm run serve{
users {
name
}
}{
users (skip: 0, take: 1) {
id
name
}
}{
users {
id
name
interests {
id
title
}
}
}{
interests {
id
title
}
}{
interests (interestIds: [
"4246ec53-6eaf-4744-93ce-e2643263d84b",
"58659c67-2393-41d6-a803-94a8e9aa664f",
]) {
id
title
}
}mutation {
createUser(createUserData: {
name: "Matt",
}) {
name
interests {
title
}
}
}mutation {
createUser(createUserData: {
name: "Matt",
interestIds: ["58659c67-2393-41d6-a803-94a8e9aa664f"],
}) {
name
interests {
title
}
}
}mutation {
createInterest(createInterestData: {
title: "React",
}) {
id
}
} subscription {
userAdded {
id
name
}
} subscription {
interestAdded {
id
title
}
}import { gql } from '@apollo/client';
import { useSubscription } from "@apollo/react-hooks";
const ADDED_USERS_SUBSCRIPTION = gql`
subscription {
userAdded {
id
name
interests {
title
}
}
}
`;
const LatestUser = () => {
const { data, error, loading } = useSubscription(ADDED_USERS_SUBSCRIPTION);
if (loading) {
return <></>;
}
if (error) {
return <>Something went wrong.</>;
}
if (!data) {
return <>No new users</>;
}
const { userAdded } = data;
const { name, interests } = userAdded;
if (!interests || !interests.length) {
return <>Say coucou to {name}, they don't have interests yet, let's help them !</>;
}
const interestList = interests.map(x => x.title).join(', ');
return <>Please welcome {name}, fun fact: they like {interestList} !!</>;
}
export default LatestUser;When running GraphQL Server over multiple instances or in a production environment (where in-memory pubsub isn't recommended) consider using an external solution like graphql-redis-subscriptions so all connections are notified of events consistently.