Skip to content

Commit 9e5ec3a

Browse files
authored
Show GraphQL Yoga and move docs to Apollo Server v3 (#2217)
1 parent 0781723 commit 9e5ec3a

File tree

22 files changed

+534
-695
lines changed

22 files changed

+534
-695
lines changed

.changeset/clean-rules-worry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-modules': patch
3+
---
4+
5+
Add a deprecation note for createSchemaForApollo method

.changeset/config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"examples-basic",
1212
"examples-di",
1313
"examples-subscriptions",
14-
"example-graphql-ez"
14+
"graphql-yoga"
1515
]
1616
}

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Just take a look at the build status on Github Actions and find "Publish Canary"
4444
More advanced usage at [graphql-modules.com](https://graphql-modules.com/docs)
4545

4646
```js
47-
import { createModule, createApplication, gql } from 'graphql-modules';
47+
import { createModule, createApplication, gql } from 'graphql-modules'
4848

4949
const module = createModule({
5050
id: 'my-module',
@@ -59,12 +59,12 @@ const module = createModule({
5959
posts: [Post]
6060
}
6161
`,
62-
resolvers: blogResolvers,
63-
});
62+
resolvers: blogResolvers
63+
})
6464

6565
const application = createApplication({
66-
modules: [module],
67-
});
66+
modules: [module]
67+
})
6868
```
6969

7070
Inside the `examples` directory you can find the following examples:

examples/ez/package.json

-24
This file was deleted.

examples/ez/src/app/auth/auth.module.ts

-48
This file was deleted.

examples/ez/src/app/social-network/social-network.module.ts

-15
This file was deleted.

examples/ez/src/app/user/user.module.ts

-17
This file was deleted.

examples/ez/src/index.ts

-34
This file was deleted.

examples/graphql-yoga/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "graphql-yoga",
3+
"version": "0.0.0",
4+
"private": true,
5+
"main": "dist/index.js",
6+
"license": "MIT",
7+
"scripts": {
8+
"start": "ts-node --project ../../tsconfig.app.json ./src/index.ts",
9+
"build": "tsc"
10+
}
11+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createApplication } from 'graphql-modules';
2+
import { PostModule } from './post/post.module';
3+
4+
export const app = createApplication({
5+
modules: [PostModule],
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { createModule, gql } from 'graphql-modules';
2+
import { PostsProvider } from './post.provider';
3+
import { PubSub, PUB_SUB, providePubSub } from './pubsub';
4+
5+
export const PostModule = createModule({
6+
id: 'post',
7+
dirname: __dirname,
8+
providers: [PostsProvider, providePubSub()],
9+
typeDefs: gql`
10+
type Subscription {
11+
postAdded: Post
12+
}
13+
14+
type Query {
15+
posts: [Post]
16+
}
17+
18+
type Mutation {
19+
addPost(author: String, comment: String): Post
20+
}
21+
22+
type Post {
23+
author: String
24+
comment: String
25+
}
26+
`,
27+
resolvers: {
28+
Subscription: {
29+
postAdded: {
30+
// Additional event labels can be passed to asyncIterator creation
31+
subscribe(
32+
_root: any,
33+
_args: any,
34+
{ injector }: GraphQLModules.Context
35+
) {
36+
return injector.get<PubSub>(PUB_SUB).subscribe('POST_ADDED');
37+
},
38+
},
39+
},
40+
Query: {
41+
posts(_root: any, _args: any, { injector }: GraphQLModules.Context) {
42+
return injector.get(PostsProvider).getPosts();
43+
},
44+
},
45+
Mutation: {
46+
addPost(_root: any, args: any, { injector }: GraphQLModules.Context) {
47+
return injector.get(PostsProvider).addPost(args);
48+
},
49+
},
50+
},
51+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Injectable, Inject } from 'graphql-modules';
2+
import { PUB_SUB, PubSub } from './pubsub';
3+
import { Post } from './types';
4+
5+
@Injectable()
6+
export class PostsProvider {
7+
posts: Post[] = [];
8+
9+
constructor(@Inject(PUB_SUB) private pubSub: PubSub) {}
10+
11+
getPosts() {
12+
return this.posts;
13+
}
14+
15+
addPost(post: Post) {
16+
this.posts.push(post);
17+
this.pubSub.publish('POST_ADDED', { postAdded: post });
18+
return post;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { InjectionToken, FactoryProvider, Scope } from 'graphql-modules';
2+
import { createPubSub, PubSub as TPubSub } from '@graphql-yoga/node';
3+
import { Post } from './types';
4+
5+
type PubSub = TPubSub<{
6+
POST_ADDED: [
7+
{
8+
postAdded: Post;
9+
}
10+
];
11+
}>;
12+
13+
export const PUB_SUB = new InjectionToken<PubSub>('PubSub');
14+
15+
export { PubSub };
16+
17+
export function providePubSub(): FactoryProvider<PubSub> {
18+
return {
19+
provide: PUB_SUB,
20+
scope: Scope.Singleton,
21+
useFactory() {
22+
return createPubSub();
23+
},
24+
};
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Post {
2+
author: string;
3+
comment: string;
4+
}

examples/graphql-yoga/src/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'reflect-metadata';
2+
import { createServer } from '@graphql-yoga/node';
3+
import { useGraphQLModules } from '@envelop/graphql-modules';
4+
import { app } from './app';
5+
6+
const server = createServer({
7+
plugins: [useGraphQLModules(app)],
8+
});
9+
10+
server.start().then(() => {
11+
// tslint:disable-next-line: no-console
12+
console.info(`🚀 Server ready at http://localhost:4000/graphql`);
13+
});

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
"@babel/plugin-proposal-class-properties": "7.17.12",
3333
"@changesets/apply-release-plan": "5.0.1",
3434
"@changesets/cli": "2.23.0",
35+
"@envelop/graphql-modules": "3.3.3",
3536
"@graphql-tools/merge": "8.2.14",
37+
"@graphql-yoga/node": "2.9.2",
3638
"@types/benchmark": "2.1.1",
3739
"@types/express": "4.17.13",
3840
"@types/jest": "27.5.2",

0 commit comments

Comments
 (0)