forked from Urigo/graphql-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add apollo subscriptions example Urigo#450
- Loading branch information
Showing
9 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "apollo-subscriptions", | ||
"version": "0.7.0", | ||
"private": true, | ||
"main": "dist/index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "ts-node ./src/index.ts", | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"@graphql-modules/core": "0.8.0-alpha.66fb3726", | ||
"@graphql-modules/di": "0.8.0-alpha.66fb3726", | ||
"@types/graphql": "14.2.1", | ||
"apollo-server-express": "^2.4.8", | ||
"graphql": "14.2.1", | ||
"reflect-metadata": "0.1.13", | ||
"ts-node": "8.0.2", | ||
"typescript": "3.4.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { GraphQLModule } from '@graphql-modules/core'; | ||
import { CommonModule } from './common/common.module'; | ||
import { PostModule } from './post/post.module'; | ||
|
||
export const AppModule = new GraphQLModule({ | ||
imports: [CommonModule, PostModule] | ||
}); |
6 changes: 6 additions & 0 deletions
6
examples/apollo-subscriptions/src/app/common/common.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { GraphQLModule } from '@graphql-modules/core'; | ||
import { PubSub } from 'graphql-subscriptions'; | ||
|
||
export const CommonModule = new GraphQLModule({ | ||
providers: [PubSub] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { GraphQLModule } from '@graphql-modules/core'; | ||
import { PubSub } from 'graphql-subscriptions'; | ||
import { CommonModule } from '../common/common.module'; | ||
import { PostsProvider } from './post.provider'; | ||
|
||
export const PostModule = new GraphQLModule({ | ||
imports: [CommonModule], | ||
providers: [PostsProvider], | ||
typeDefs: ` | ||
type Subscription { | ||
postAdded: Post | ||
} | ||
type Query { | ||
posts: [Post] | ||
} | ||
type Mutation { | ||
addPost(author: String, comment: String): Post | ||
} | ||
type Post { | ||
author: String | ||
comment: String | ||
} | ||
`, | ||
resolvers: { | ||
Subscription: { | ||
postAdded: { | ||
// Additional event labels can be passed to asyncIterator creation | ||
subscribe: (root, args, { injector }) => injector.get(PubSub).asyncIterator(['POST_ADDED']) | ||
} | ||
}, | ||
Query: { | ||
posts: (root, args, { injector }) => injector.get(PostsProvider).getPosts() | ||
}, | ||
Mutation: { | ||
addPost: (root, args, { injector }) => { | ||
return injector.get(PostsProvider).addPost(args); | ||
} | ||
} | ||
} | ||
}); |
20 changes: 20 additions & 0 deletions
20
examples/apollo-subscriptions/src/app/post/post.provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Injectable } from '@graphql-modules/di'; | ||
import { PubSub } from 'graphql-subscriptions'; | ||
|
||
export interface Post { | ||
author: string; | ||
comment: string; | ||
} | ||
|
||
@Injectable() | ||
export class PostsProvider { | ||
posts: Post[] = []; | ||
constructor(private pubSub: PubSub) {} | ||
getPosts() { | ||
return this.posts; | ||
} | ||
addPost(post: Post) { | ||
this.posts.push(post); | ||
this.pubSub.publish('POST_ADDED', { postAdded: post }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'reflect-metadata'; | ||
import * as express from 'express'; | ||
import { createServer } from 'http'; | ||
import { ApolloServer } from 'apollo-server-express'; | ||
import { AppModule } from './app/app.module'; | ||
|
||
const { schema, subscriptions } = AppModule; | ||
|
||
const server = new ApolloServer({ | ||
schema, | ||
context: session => session, | ||
subscriptions | ||
}); | ||
|
||
const app = express(); | ||
server.applyMiddleware({ app }); | ||
|
||
const httpServer = createServer(app); | ||
server.installSubscriptionHandlers(httpServer); | ||
|
||
httpServer.listen({ port: 4000 }, () => { | ||
// tslint:disable-next-line: no-console | ||
console.info(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`); | ||
// tslint:disable-next-line: no-console | ||
console.info(`🚀 Subsciription ready at ws://localhost:4000${server.subscriptionsPath}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"experimentalDecorators": true, | ||
"module": "commonjs", | ||
"target": "es2015", | ||
"lib": ["es2015", "esnext.asynciterable"], | ||
"noImplicitAny": false, | ||
"suppressImplicitAnyIndexErrors": true, | ||
"moduleResolution": "node", | ||
"emitDecoratorMetadata": true, | ||
"sourceMap": true, | ||
"declaration": true, | ||
"outDir": "./dist", | ||
"rootDir": "./src" | ||
}, | ||
"files": ["src/index.ts"] | ||
} |