Skip to content

Commit e4b593a

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

File tree

6 files changed

+86
-76
lines changed

6 files changed

+86
-76
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-28
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,23 @@ import { delegateToSchema } from '@graphql-tools/delegate';
1616
import { createApplication, createModule, gql } from 'graphql-modules';
1717
import { AuthenticationServicesToken } from '@accounts/server';
1818
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';
19+
import { createServer } from 'node:http';
20+
import { createYoga } from 'graphql-yoga';
21+
import { useGraphQLModules } from '@envelop/graphql-modules';
2322

24-
const accountsServerUri = 'http://localhost:4003/';
23+
const accountsServerUri = 'http://localhost:4003/graphql';
2524

2625
(async () => {
27-
const typeDefs = gql`
26+
const myTypeDefs = gql`
2827
type PrivateType @auth {
2928
field: String
3029
}
3130
3231
extend type Query {
33-
# Example of how to get the userId from the context and return the current logged in user or null
32+
# Example of how to delegate to another field of the remote schema. Returns the currently logged in user or null.
3433
me: User
34+
# Returns the currently logged in userId directly from the context without querying the remote schema.
35+
myId: ID
3536
publicField: String
3637
# You can only query this if you are logged in
3738
privateField: String @auth
@@ -45,7 +46,7 @@ const accountsServerUri = 'http://localhost:4003/';
4546
}
4647
`;
4748

48-
const resolvers = {
49+
const myResolvers = {
4950
Query: {
5051
me: {
5152
resolve: (parent, args, context, info) => {
@@ -59,6 +60,7 @@ const accountsServerUri = 'http://localhost:4003/';
5960
});
6061
},
6162
},
63+
myId: (parent, args, context) => context.userId,
6264
publicField: () => 'public',
6365
privateField: () => 'private',
6466
privateFieldWithAuthResolver: authenticated(() => {
@@ -74,8 +76,6 @@ const accountsServerUri = 'http://localhost:4003/';
7476
},
7577
};
7678

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

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

102-
const { createOperationController, createSchemaForApollo } = createApplication({
102+
const app = createApplication({
103103
modules: [
104104
createAccountsCoreModule({
105105
tokenSecret: 'secret',
106106
// setting micro to true will instruct accounts-js to only
107107
// verify access tokens without any additional session logic
108108
micro: true,
109109
}),
110-
createAccountsPasswordModule(),
111-
createModule({ id: 'app', typeDefs }),
110+
createAccountsPasswordModule({ micro: true }),
111+
createModule({
112+
id: 'app',
113+
typeDefs: myTypeDefs,
114+
resolvers: myResolvers,
115+
}),
112116
],
113117
providers: [
114118
{
@@ -117,7 +121,7 @@ const accountsServerUri = 'http://localhost:4003/';
117121
global: true,
118122
},
119123
],
120-
schemaBuilder: () =>
124+
schemaBuilder: ({ typeDefs, resolvers }) =>
121125
authDirectiveTransformer(
122126
stitchSchemas({
123127
subschemas: [remoteSubschema],
@@ -127,22 +131,16 @@ const accountsServerUri = 'http://localhost:4003/';
127131
),
128132
});
129133

130-
const schema = createSchemaForApollo();
134+
const { createOperationController } = app;
131135

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 },
136+
const yoga = createYoga({
137+
plugins: [useGraphQLModules(app)],
144138
context: (ctx) => context(ctx, { createOperationController }),
145139
});
146140

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

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
{

modules/module-password/src/index.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createModule } from 'graphql-modules';
1+
import { createModule, gql } from 'graphql-modules';
22
import TypesTypeDefs from './schema/types';
33
import getQueryTypeDefs from './schema/query';
44
import getMutationTypeDefs from './schema/mutation';
@@ -15,16 +15,25 @@ export interface AccountsPasswordModuleConfig extends AccountsPasswordOptions {
1515
rootQueryName?: string;
1616
rootMutationName?: string;
1717
extendTypeDefs?: boolean;
18+
micro?: boolean;
1819
}
1920

2021
export const createAccountsPasswordModule = (config: AccountsPasswordModuleConfig = {}) =>
2122
createModule({
2223
id: 'accounts-password',
23-
typeDefs: [TypesTypeDefs, getQueryTypeDefs(config), getMutationTypeDefs(config)],
24-
resolvers: {
25-
[config.rootQueryName || 'Query']: Query,
26-
[config.rootMutationName || 'Mutation']: Mutation,
27-
},
24+
typeDefs: config.micro
25+
? gql`
26+
extend type Query {
27+
_accounts_password: String
28+
}
29+
`
30+
: [TypesTypeDefs, getQueryTypeDefs(config), getMutationTypeDefs(config)],
31+
...(!config.micro && {
32+
resolvers: {
33+
[config.rootQueryName || 'Query']: Query,
34+
[config.rootMutationName || 'Mutation']: Mutation,
35+
},
36+
}),
2837
providers: [
2938
{
3039
provide: AccountsPasswordConfigToken,

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)