Skip to content

Commit

Permalink
Add apollo subscriptions example Urigo#450
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jul 16, 2019
1 parent 9b51481 commit cc3473c
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/introduction/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const AnotherModule = new GraphQLModule({
});
```

To get access to your configuration in your `Provider`s, inject `MyModuleConfig` and pass your module's name as `string`:
To get access to your configuration in your `Provider`s, inject `MyModuleConfig`:

```typescript
import { ModuleConfig } from '@graphql-modules/core';
Expand All @@ -55,7 +55,7 @@ import { MyModule } from './my-module.ts';

@Injectable()
export class MyProvider {
constructor(@Inject(ModuleConfig(module => MyModule)) private config: MyModuleConfig) {
constructor(@Inject(ModuleConfig) private config: MyModuleConfig) {

}

Expand Down
2 changes: 1 addition & 1 deletion docs/recipes/development-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Take care to add the polyfill `reflect-metadata` & require it (once)
```js
require('reflect-metadata');
Inject(AProvider)(MyProvider, undefined, 0); // inject AProvider to first MyProvider constructor argument
Inject(ModuleConfig(_ => 'SimpleModule'))(MyProvider, undefined, 0); // inject SimpleModule Config to second MyProvider constructor argument
Inject(ModuleConfig)(MyProvider, undefined, 0); // inject SimpleModule Config to second MyProvider constructor argument
module.exports = Injectable({...})(MyProvider);

```
Expand Down
21 changes: 21 additions & 0 deletions examples/apollo-subscriptions/package.json
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"
}
}
7 changes: 7 additions & 0 deletions examples/apollo-subscriptions/src/app/app.module.ts
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 examples/apollo-subscriptions/src/app/common/common.module.ts
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]
});
43 changes: 43 additions & 0 deletions examples/apollo-subscriptions/src/app/post/post.module.ts
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 examples/apollo-subscriptions/src/app/post/post.provider.ts
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 });
}
}
26 changes: 26 additions & 0 deletions examples/apollo-subscriptions/src/index.ts
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}`);
});
17 changes: 17 additions & 0 deletions examples/apollo-subscriptions/tsconfig.json
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"]
}

0 comments on commit cc3473c

Please sign in to comment.