Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a GraphQL subscription example #4

Closed
2 tasks done
ZenSoftware opened this issue Jan 4, 2021 · 1 comment
Closed
2 tasks done

Create a GraphQL subscription example #4

ZenSoftware opened this issue Jan 4, 2021 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@ZenSoftware
Copy link
Owner

ZenSoftware commented Jan 4, 2021

In progress...
Apollo Client connects and everything works, but this needs a publicly available example

  • WebSocketLink connects and Nest @GqlGuard and @Roles decorators work with Subscriptions.
  • Create an example
@ZenSoftware ZenSoftware created this issue from a note in Zen Stable Release (In progress) Jan 4, 2021
@ZenSoftware ZenSoftware self-assigned this Jan 4, 2021
@ZenSoftware ZenSoftware added the enhancement New feature or request label Jan 4, 2021
@ZenSoftware ZenSoftware moved this from In progress to To do in Zen Stable Release Jan 4, 2021
@ZenSoftware
Copy link
Owner Author

  • Implemented client side GraphQL Subscription over WebSockets
  • Implemented server side sample of a pub/sub Nest GraphQL Subscription service
  • Works with the @Roles(roles: string | string[]) authorization to be used in conjunction with the JWT @UseGuards(GqlGuard) decorator.

constructor(private sampleSubscriptionGQL: SampleSubscriptionGQL) {
this.#sub = this.sampleSubscriptionGQL.subscribe().subscribe(({ data }) => {
this.recentValue = data?.sampleSubscription.message;
});
}

export const typeDefs = gql`
extend type Mutation {
sampleUpload(file: Upload!): Boolean!
}
type SampleSubscriptionResult {
message: String!
}
type Subscription {
sampleSubscription: SampleSubscriptionResult!
}
`;
const pubSub = new PubSub();
interval(1000).subscribe(i =>
pubSub.publish('sampleSubscription', {
sampleSubscription: {
message: `Server ticker ${i}`,
},
})
);
@Resolver()
@UseGuards(GqlGuard)
@Roles('Super')
export class SampleResolver {
@Mutation()
async sampleUpload(@Args('file', { type: () => GraphQLUpload }) file: FileInfo) {
const readStream = file.file.createReadStream();
const chunks = [];
for await (const chunk of readStream) {
chunks.push(chunk);
}
const buffer = Buffer.concat(chunks);
Logger.log(`Recieved '${file.file.filename}' ${buffer.byteLength} bytes`);
return true;
}
@Subscription()
async sampleSubscription(@GqlUser() user: RequestUser) {
Logger.log(`sampleSubscription subscribed to by user with id ${user.id}`);
return pubSub.asyncIterator('sampleSubscription');
}
}

Zen Stable Release automation moved this from To do to Done Jan 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

1 participant