Skip to content

Commit 1637b35

Browse files
committed
feat: port microservice example to yoga
1 parent a528978 commit 1637b35

File tree

5 files changed

+71
-73
lines changed

5 files changed

+71
-73
lines changed

examples/accounts-microservice/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "lib/index.js",
66
"license": "MIT",
77
"scripts": {
8-
"start": "NODE_ENV=development yarn run -T nodemon -w src -x ts-node src/accounts-microservice.ts & sleep 2 && yarn run -T nodemon -w src -x ts-node src/app-server.ts",
8+
"start": "NODE_ENV=development yarn run -T nodemon -w src -x ts-node src/accounts-microservice.ts & sleep 15 && yarn run -T nodemon -w src -x ts-node src/app-server.ts",
99
"start-services": "docker-compose up -d",
1010
"prestart": "yarn run start-services",
1111
"build": "yarn run -T tsc",
@@ -18,8 +18,8 @@
1818
"@accounts/mongo": "^0.34.0",
1919
"@accounts/password": "^0.32.1",
2020
"@accounts/server": "^0.33.1",
21-
"@apollo/server": "4.9.3",
22-
"@apollo/server-plugin-landing-page-graphql-playground": "4.0.1",
21+
"@envelop/core": "4.0.1",
22+
"@envelop/graphql-modules": "5.0.1",
2323
"@graphql-tools/delegate": "10.0.3",
2424
"@graphql-tools/merge": "9.0.0",
2525
"@graphql-tools/schema": "10.0.0",
@@ -28,6 +28,7 @@
2828
"@graphql-tools/wrap": "10.0.1",
2929
"graphql": "16.8.1",
3030
"graphql-modules": "2.2.0",
31+
"graphql-yoga": "4.0.4",
3132
"lodash": "4.17.21",
3233
"node-fetch": "2.7.0",
3334
"tslib": "2.6.2"

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

+13-20
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import { createAccountsMongoModule } from '@accounts/module-mongo';
55
import { createApplication, createModule, gql } from 'graphql-modules';
66
import { AuthenticationServicesToken } from '@accounts/server';
77
import { AccountsPassword } from '@accounts/password';
8-
import { ApolloServer } from '@apollo/server';
9-
import { startStandaloneServer } from '@apollo/server/standalone';
10-
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
11-
import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground';
8+
import { createServer } from 'node:http';
9+
import { createYoga } from 'graphql-yoga';
10+
import { useGraphQLModules } from '@envelop/graphql-modules';
1211

1312
(async () => {
1413
const typeDefs = gql`
@@ -24,7 +23,7 @@ import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-p
2423
}
2524
`;
2625

27-
const { createOperationController, createSchemaForApollo } = createApplication({
26+
const app = createApplication({
2827
modules: [
2928
createAccountsCoreModule({ tokenSecret: 'secret' }),
3029
createAccountsPasswordModule({
@@ -55,25 +54,19 @@ import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-p
5554
global: true,
5655
},
5756
],
58-
schemaBuilder: buildSchema({ typeDefs }),
57+
schemaBuilder: buildSchema(),
5958
});
6059

61-
const schema = createSchemaForApollo();
60+
const { createOperationController } = app;
6261

63-
// Create the Apollo Server that takes a schema and configures internal stuff
64-
const server = new ApolloServer({
65-
schema,
66-
plugins: [
67-
process.env.NODE_ENV === 'production'
68-
? ApolloServerPluginLandingPageDisabled()
69-
: ApolloServerPluginLandingPageGraphQLPlayground(),
70-
],
71-
});
72-
73-
const { url } = await startStandaloneServer(server, {
74-
listen: { port: 4003 },
62+
const yoga = createYoga({
63+
plugins: [useGraphQLModules(app)],
7564
context: (ctx) => context(ctx, { createOperationController }),
7665
});
7766

78-
console.log(`🚀 Server ready at ${url}`);
67+
const server = createServer(yoga);
68+
69+
server.listen(4003, () => {
70+
console.info('Server is running on http://localhost:4003/graphql');
71+
});
7972
})();

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

+26-31
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,26 @@ import {
1111
context,
1212
createAccountsCoreModule,
1313
} from '@accounts/module-core';
14-
import { createAccountsPasswordModule } from '@accounts/module-password';
1514
import { delegateToSchema } from '@graphql-tools/delegate';
1615
import { createApplication, createModule, gql } from 'graphql-modules';
1716
import { AuthenticationServicesToken } from '@accounts/server';
18-
import { AccountsPassword } from '@accounts/password';
19-
import { ApolloServer } from '@apollo/server';
20-
import { startStandaloneServer } from '@apollo/server/standalone';
21-
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
22-
import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground';
17+
import { createServer } from 'node:http';
18+
import { createYoga } from 'graphql-yoga';
19+
import { useGraphQLModules } from '@envelop/graphql-modules';
2320

24-
const accountsServerUri = 'http://localhost:4003/';
21+
const accountsServerUri = 'http://localhost:4003/graphql';
2522

2623
(async () => {
27-
const typeDefs = gql`
24+
const myTypeDefs = gql`
2825
type PrivateType @auth {
2926
field: String
3027
}
3128
3229
extend type Query {
33-
# Example of how to get the userId from the context and return the current logged in user or null
30+
# Example of how to delegate to another field of the remote schema. Returns the currently logged in user or null.
3431
me: User
32+
# Returns the currently logged in userId directly from the context without querying the remote schema.
33+
myId: ID
3534
publicField: String
3635
# You can only query this if you are logged in
3736
privateField: String @auth
@@ -45,7 +44,7 @@ const accountsServerUri = 'http://localhost:4003/';
4544
}
4645
`;
4746

48-
const resolvers = {
47+
const myResolvers = {
4948
Query: {
5049
me: {
5150
resolve: (parent, args, context, info) => {
@@ -59,6 +58,7 @@ const accountsServerUri = 'http://localhost:4003/';
5958
});
6059
},
6160
},
61+
myId: (parent, args, context) => context.userId,
6262
publicField: () => 'public',
6363
privateField: () => 'private',
6464
privateFieldWithAuthResolver: authenticated(() => {
@@ -74,8 +74,6 @@ const accountsServerUri = 'http://localhost:4003/';
7474
},
7575
};
7676

77-
// // Note: the following steps are optional and only required if you want to stitch the remote accounts schema with your apps schema.
78-
7977
const remoteExecutor: AsyncExecutor = async ({ document, variables, context }) => {
8078
console.log('context: ', context);
8179
const query = print(document);
@@ -99,25 +97,28 @@ const accountsServerUri = 'http://localhost:4003/';
9997

10098
const { authDirectiveTypeDefs, authDirectiveTransformer } = authDirective('auth');
10199

102-
const { createOperationController, createSchemaForApollo } = createApplication({
100+
const app = createApplication({
103101
modules: [
104102
createAccountsCoreModule({
105103
tokenSecret: 'secret',
106104
// setting micro to true will instruct accounts-js to only
107105
// verify access tokens without any additional session logic
108106
micro: true,
109107
}),
110-
createAccountsPasswordModule(),
111-
createModule({ id: 'app', typeDefs }),
108+
createModule({
109+
id: 'app',
110+
typeDefs: myTypeDefs,
111+
resolvers: myResolvers,
112+
}),
112113
],
113114
providers: [
114115
{
115116
provide: AuthenticationServicesToken,
116-
useValue: { password: AccountsPassword },
117+
useValue: {},
117118
global: true,
118119
},
119120
],
120-
schemaBuilder: () =>
121+
schemaBuilder: ({ typeDefs, resolvers }) =>
121122
authDirectiveTransformer(
122123
stitchSchemas({
123124
subschemas: [remoteSubschema],
@@ -127,22 +128,16 @@ const accountsServerUri = 'http://localhost:4003/';
127128
),
128129
});
129130

130-
const schema = createSchemaForApollo();
131+
const { createOperationController } = app;
131132

132-
// Create the Apollo Server that takes a schema and configures internal stuff
133-
const server = new ApolloServer({
134-
schema,
135-
plugins: [
136-
process.env.NODE_ENV === 'production'
137-
? ApolloServerPluginLandingPageDisabled()
138-
: ApolloServerPluginLandingPageGraphQLPlayground(),
139-
],
140-
});
141-
142-
const { url } = await startStandaloneServer(server, {
143-
listen: { port: 4000 },
133+
const yoga = createYoga({
134+
plugins: [useGraphQLModules(app)],
144135
context: (ctx) => context(ctx, { createOperationController }),
145136
});
146137

147-
console.log(`🚀 Server ready at ${url}`);
138+
const server = createServer(yoga);
139+
140+
server.listen(4000, () => {
141+
console.info('Server is running on http://localhost:4000/graphql');
142+
});
148143
})();

modules/module-core/src/index.ts

+25-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createModule, Provider } from 'graphql-modules';
1+
import { createModule, gql, Provider } from 'graphql-modules';
22
import { mergeTypeDefs } from '@graphql-tools/merge';
33
import { User, IContext } from '@accounts/types';
44
import {
@@ -33,22 +33,30 @@ export type AccountsContextGraphQLModules<IUser extends User = User> = IContext<
3333
export const createAccountsCoreModule = (config: AccountsCoreModuleConfig) =>
3434
createModule({
3535
id: 'accounts-core',
36-
typeDefs: mergeTypeDefs(
37-
[
38-
makeSchema(config),
39-
TypesTypeDefs,
40-
getQueryTypeDefs(config),
41-
getMutationTypeDefs(config),
42-
...getSchemaDef(config),
43-
],
44-
{
45-
useSchemaDefinition: config.withSchemaDefinition,
46-
}
47-
),
48-
resolvers: {
49-
[config.rootQueryName || 'Query']: Query,
50-
[config.rootMutationName || 'Mutation']: Mutation,
51-
},
36+
typeDefs: config.micro
37+
? gql`
38+
extend type Query {
39+
_accounts_core: String
40+
}
41+
`
42+
: mergeTypeDefs(
43+
[
44+
makeSchema(config),
45+
TypesTypeDefs,
46+
getQueryTypeDefs(config),
47+
getMutationTypeDefs(config),
48+
...getSchemaDef(config),
49+
],
50+
{
51+
useSchemaDefinition: config.withSchemaDefinition,
52+
}
53+
),
54+
...(!config.micro && {
55+
resolvers: {
56+
[config.rootQueryName || 'Query']: Query,
57+
[config.rootMutationName || 'Mutation']: Mutation,
58+
},
59+
}),
5260
providers: () => {
5361
const providers: Provider[] = [
5462
{

yarn.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -4080,8 +4080,8 @@ __metadata:
40804080
"@accounts/mongo": "npm:^0.34.0"
40814081
"@accounts/password": "npm:^0.32.1"
40824082
"@accounts/server": "npm:^0.33.1"
4083-
"@apollo/server": "npm:4.9.3"
4084-
"@apollo/server-plugin-landing-page-graphql-playground": "npm:4.0.1"
4083+
"@envelop/core": "npm:4.0.1"
4084+
"@envelop/graphql-modules": "npm:5.0.1"
40854085
"@graphql-tools/delegate": "npm:10.0.3"
40864086
"@graphql-tools/merge": "npm:9.0.0"
40874087
"@graphql-tools/schema": "npm:10.0.0"
@@ -4091,6 +4091,7 @@ __metadata:
40914091
"@types/lodash": "npm:4.14.199"
40924092
graphql: "npm:16.8.1"
40934093
graphql-modules: "npm:2.2.0"
4094+
graphql-yoga: "npm:4.0.4"
40944095
lodash: "npm:4.17.21"
40954096
node-fetch: "npm:2.7.0"
40964097
tslib: "npm:2.6.2"

0 commit comments

Comments
 (0)