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) {
idname
}
}{
users {
idnameinterests {
idtitle
}
}
}{
interests {
idtitle
}
}{
interests (interestIds: [
"4246ec53-6eaf-4744-93ce-e2643263d84b",
"58659c67-2393-41d6-a803-94a8e9aa664f",
]) {
idtitle
}
}mutation {
createUser(createUserData: {
name: "Matt",
}) {
nameinterests {
title
}
}
}mutation {
createUser(createUserData: {
name: "Matt",
interestIds: ["58659c67-2393-41d6-a803-94a8e9aa664f"],
}) {
nameinterests {
title
}
}
}mutation {
createInterest(createInterestData: {
title: "React",
}) {
id
}
}subscription {
userAdded {
idname
}
}subscription {
interestAdded {
idtitle
}
}import{gql}from'@apollo/client';import{useSubscription}from"@apollo/react-hooks";constADDED_USERS_SUBSCRIPTION=gql` subscription { userAdded { id name interests { title } } }`;constLatestUser=()=>{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 !</>;}constinterestList=interests.map(x=>x.title).join(', ');return<>Please welcome {name}, fun fact: they like {interestList} !!</>;}exportdefaultLatestUser;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.