Skip to content

Commit 13041d7

Browse files
committed
feat: start using yoga in some examples
1 parent 8c05452 commit 13041d7

File tree

18 files changed

+496
-105
lines changed

18 files changed

+496
-105
lines changed

.changeset/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"@examples/graphql-mikro-orm-typescript",
2020
"@examples/graphql-typeorm-typescript",
2121
"@examples/graphql-server-typescript",
22+
"@examples/graphql-server-typescript-apollo",
2223
"@examples/magic-link-server-typescript",
2324
"@examples/react-graphql-typescript",
2425
"@examples/react-rest-typescript",

examples/accounts-microservice/src/accounts-microservice.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-p
2424
}
2525
`;
2626

27-
const app = createApplication({
27+
const { createOperationController, createSchemaForApollo } = createApplication({
2828
modules: [
2929
createAccountsCoreModule({ tokenSecret: 'secret' }),
3030
createAccountsPasswordModule({
@@ -58,7 +58,7 @@ import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-p
5858
schemaBuilder: buildSchema({ typeDefs }),
5959
});
6060

61-
const schema = app.createSchemaForApollo();
61+
const schema = createSchemaForApollo();
6262

6363
// Create the Apollo Server that takes a schema and configures internal stuff
6464
const server = new ApolloServer({
@@ -72,7 +72,7 @@ import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-p
7272

7373
const { url } = await startStandaloneServer(server, {
7474
listen: { port: 4003 },
75-
context: (ctx) => context(ctx, { app }),
75+
context: (ctx) => context(ctx, { createOperationController }),
7676
});
7777

7878
console.log(`🚀 Server ready at ${url}`);

examples/accounts-microservice/src/app-server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const accountsServerUri = 'http://localhost:4003/';
9999

100100
const { authDirectiveTypeDefs, authDirectiveTransformer } = authDirective('auth');
101101

102-
const app = createApplication({
102+
const { createOperationController, createSchemaForApollo } = createApplication({
103103
modules: [
104104
createAccountsCoreModule({
105105
tokenSecret: 'secret',
@@ -127,7 +127,7 @@ const accountsServerUri = 'http://localhost:4003/';
127127
),
128128
});
129129

130-
const schema = app.createSchemaForApollo();
130+
const schema = createSchemaForApollo();
131131

132132
// Create the Apollo Server that takes a schema and configures internal stuff
133133
const server = new ApolloServer({
@@ -141,7 +141,7 @@ const accountsServerUri = 'http://localhost:4003/';
141141

142142
const { url } = await startStandaloneServer(server, {
143143
listen: { port: 4000 },
144-
context: (ctx) => context(ctx, { app }),
144+
context: (ctx) => context(ctx, { createOperationController }),
145145
});
146146

147147
console.log(`🚀 Server ready at ${url}`);

examples/graphql-server-mikro-orm-postgres/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"@accounts/module-password": "^0.34.0",
2626
"@accounts/password": "^0.32.1",
2727
"@accounts/server": "^0.33.1",
28-
"@apollo/server": "4.9.3",
29-
"@apollo/server-plugin-landing-page-graphql-playground": "4.0.1",
28+
"@envelop/core": "4.0.1",
29+
"@envelop/graphql-modules": "5.0.1",
3030
"@graphql-tools/merge": "9.0.0",
3131
"@graphql-tools/schema": "10.0.0",
3232
"@mikro-orm/cli": "5.8.1",
@@ -38,6 +38,7 @@
3838
"@mikro-orm/reflection": "5.8.1",
3939
"graphql": "16.8.1",
4040
"graphql-modules": "2.2.0",
41+
"graphql-yoga": "4.0.4",
4142
"tslib": "2.6.2"
4243
},
4344
"devDependencies": {

examples/graphql-server-mikro-orm-postgres/src/index.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import { Email } from './entities/email';
99
import { createApplication, gql } from 'graphql-modules';
1010
import AccountsServer, { AuthenticationServicesToken, ServerHooks } from '@accounts/server';
1111
import { context, createAccountsMikroORMModule } from '@accounts/module-mikro-orm';
12-
import { ApolloServer } from '@apollo/server';
13-
import { startStandaloneServer } from '@apollo/server/standalone';
14-
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
15-
import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground';
12+
import { createServer } from 'node:http';
13+
import { createYoga } from 'graphql-yoga';
14+
import { useGraphQLModules } from '@envelop/graphql-modules';
1615

17-
export const createAccounts = async () => {
16+
void (async () => {
1817
const orm = await MikroORM.init(config);
1918

2019
const typeDefs = gql`
@@ -99,8 +98,8 @@ export const createAccounts = async () => {
9998
],
10099
schemaBuilder: buildSchema({ typeDefs, resolvers }),
101100
});
102-
const { injector } = app;
103-
const schema = app.createSchemaForApollo();
101+
102+
const { injector, createOperationController } = app;
104103

105104
injector.get(AccountsServer).on(ServerHooks.ValidateLogin, ({ user }) => {
106105
// This hook is called every time a user try to login.
@@ -109,33 +108,29 @@ export const createAccounts = async () => {
109108
console.log(`${user.firstName} ${user.lastName} logged in`);
110109
});
111110

112-
// Create the Apollo Server that takes a schema and configures internal stuff
113-
const server = new ApolloServer({
114-
schema,
115-
plugins: [
116-
process.env.NODE_ENV === 'production'
117-
? ApolloServerPluginLandingPageDisabled()
118-
: ApolloServerPluginLandingPageGraphQLPlayground(),
119-
],
120-
});
121-
122-
const { url } = await startStandaloneServer(server, {
123-
listen: { port: 4000 },
111+
// Create a Yoga instance with a GraphQL schema.
112+
const yoga = createYoga({
113+
plugins: [useGraphQLModules(app)],
124114
context: (ctx) =>
125115
context(ctx, {
126-
app,
116+
createOperationController,
127117
// Provide EntityManager either via context or via Providers
128118
// ctx: { em: orm.em.fork() }
129119
}),
130120
});
131121

132-
console.log(`🚀 Server ready at ${url}`);
122+
// Pass it into a server to hook into request handlers.
123+
const server = createServer(yoga);
124+
125+
// Start the server and you're done!
126+
server.listen(4000, () => {
127+
console.info('Server is running on http://localhost:4000/graphql');
128+
});
133129

134130
try {
135131
const generator = orm.getSchemaGenerator();
136132
await generator.createSchema({ wrap: true });
137133
} catch {
138134
// Schema has already been created
139135
}
140-
};
141-
createAccounts();
136+
})();

examples/graphql-server-typeorm-postgres/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const createAccounts = async () => {
6868
},
6969
};
7070

71-
const app = createApplication({
71+
const { createOperationController, createSchemaForApollo } = createApplication({
7272
modules: [
7373
createAccountsCoreModule({ tokenSecret }),
7474
createAccountsPasswordModule(),
@@ -86,7 +86,7 @@ export const createAccounts = async () => {
8686
],
8787
schemaBuilder: buildSchema({ typeDefs, resolvers }),
8888
});
89-
const schema = app.createSchemaForApollo();
89+
const schema = createSchemaForApollo();
9090

9191
// Create the Apollo Server that takes a schema and configures internal stuff
9292
const server = new ApolloServer({
@@ -100,7 +100,7 @@ export const createAccounts = async () => {
100100

101101
const { url } = await startStandaloneServer(server, {
102102
listen: { port: 4000 },
103-
context: (ctx) => context(ctx, { app }),
103+
context: (ctx) => context(ctx, { createOperationController }),
104104
});
105105

106106
console.log(`🚀 Server ready at ${url}`);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# graphql-server-typescript
2+
3+
This example demonstrate how to use [accounts-js](https://github.com/accounts-js/accounts).
4+
5+
## Setup example
6+
7+
In order to be able to run this example on your machine you first need to do the following steps:
8+
9+
- Clone the repository `git clone [email protected]:accounts-js/accounts.git`
10+
- Install project dependencies: `yarn install`
11+
- Compile the packages `yarn run compile`
12+
- Go to the example folder `cd examples/graphql-server-typescript`
13+
14+
## Prerequisites
15+
16+
You will need a MongoDB server to run this server. If you don't have a MongoDB server running already, and you have Docker & Docker Compose, you can do
17+
18+
```bash
19+
docker-compose up -d
20+
```
21+
22+
to start a new one.
23+
24+
## Getting Started
25+
26+
Start the app.
27+
28+
Visit http://localhost:4000/
29+
30+
```bash
31+
yarn run start
32+
```
33+
34+
-> [Start the client side](../react-graphql-typescript).
35+
36+
```graphql
37+
mutation CreateUser {
38+
createUser(
39+
user: { email: "[email protected]", password: "1234567", firstName: "John", lastName: "Doe" }
40+
)
41+
}
42+
43+
mutation Auth {
44+
authenticate(
45+
serviceName: "password"
46+
params: { password: "1234567", user: { email: "[email protected]" } }
47+
) {
48+
tokens {
49+
accessToken
50+
}
51+
}
52+
}
53+
54+
query Test {
55+
privateField
56+
}
57+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '3.6'
2+
services:
3+
db-mongo-accounts:
4+
image: mongo:3.6.5-jessie
5+
ports:
6+
- '27017:27017'
7+
restart: always
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@examples/graphql-server-typescript-apollo",
3+
"private": true,
4+
"version": "0.32.0",
5+
"main": "lib/index.js",
6+
"license": "MIT",
7+
"scripts": {
8+
"start": "NODE_ENV=development yarn run -T nodemon -w src -x ts-node src/index.ts",
9+
"build": "yarn run -T tsc",
10+
"test": "yarn run build"
11+
},
12+
"dependencies": {
13+
"@accounts/module-core": "^0.34.0",
14+
"@accounts/module-mongo": "^0.34.0",
15+
"@accounts/module-password": "^0.34.0",
16+
"@accounts/password": "^0.32.2",
17+
"@accounts/rest-express": "^0.33.1",
18+
"@accounts/server": "^0.33.1",
19+
"@apollo/server": "4.9.3",
20+
"@apollo/server-plugin-landing-page-graphql-playground": "4.0.1",
21+
"@graphql-tools/merge": "9.0.0",
22+
"@graphql-tools/schema": "10.0.0",
23+
"graphql": "16.8.1",
24+
"graphql-modules": "2.2.0",
25+
"graphql-tag": "2.12.6",
26+
"mongoose": "7.5.2",
27+
"tslib": "2.6.2"
28+
}
29+
}

0 commit comments

Comments
 (0)