@@ -11,27 +11,26 @@ import {
11
11
context ,
12
12
createAccountsCoreModule ,
13
13
} from '@accounts/module-core' ;
14
- import { createAccountsPasswordModule } from '@accounts/module-password' ;
15
14
import { delegateToSchema } from '@graphql-tools/delegate' ;
16
15
import { createApplication , createModule , gql } from 'graphql-modules' ;
17
16
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' ;
23
20
24
- const accountsServerUri = 'http://localhost:4003/' ;
21
+ const accountsServerUri = 'http://localhost:4003/graphql ' ;
25
22
26
23
( async ( ) => {
27
- const typeDefs = gql `
24
+ const myTypeDefs = gql `
28
25
type PrivateType @auth {
29
26
field: String
30
27
}
31
28
32
29
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.
34
31
me: User
32
+ # Returns the currently logged in userId directly from the context without querying the remote schema.
33
+ myId: ID
35
34
publicField: String
36
35
# You can only query this if you are logged in
37
36
privateField: String @auth
@@ -45,7 +44,7 @@ const accountsServerUri = 'http://localhost:4003/';
45
44
}
46
45
` ;
47
46
48
- const resolvers = {
47
+ const myResolvers = {
49
48
Query : {
50
49
me : {
51
50
resolve : ( parent , args , context , info ) => {
@@ -59,6 +58,7 @@ const accountsServerUri = 'http://localhost:4003/';
59
58
} ) ;
60
59
} ,
61
60
} ,
61
+ myId : ( parent , args , context ) => context . userId ,
62
62
publicField : ( ) => 'public' ,
63
63
privateField : ( ) => 'private' ,
64
64
privateFieldWithAuthResolver : authenticated ( ( ) => {
@@ -74,8 +74,6 @@ const accountsServerUri = 'http://localhost:4003/';
74
74
} ,
75
75
} ;
76
76
77
- // // Note: the following steps are optional and only required if you want to stitch the remote accounts schema with your apps schema.
78
-
79
77
const remoteExecutor : AsyncExecutor = async ( { document, variables, context } ) => {
80
78
console . log ( 'context: ' , context ) ;
81
79
const query = print ( document ) ;
@@ -99,25 +97,28 @@ const accountsServerUri = 'http://localhost:4003/';
99
97
100
98
const { authDirectiveTypeDefs, authDirectiveTransformer } = authDirective ( 'auth' ) ;
101
99
102
- const { createOperationController , createSchemaForApollo } = createApplication ( {
100
+ const app = createApplication ( {
103
101
modules : [
104
102
createAccountsCoreModule ( {
105
103
tokenSecret : 'secret' ,
106
104
// setting micro to true will instruct accounts-js to only
107
105
// verify access tokens without any additional session logic
108
106
micro : true ,
109
107
} ) ,
110
- createAccountsPasswordModule ( ) ,
111
- createModule ( { id : 'app' , typeDefs } ) ,
108
+ createModule ( {
109
+ id : 'app' ,
110
+ typeDefs : myTypeDefs ,
111
+ resolvers : myResolvers ,
112
+ } ) ,
112
113
] ,
113
114
providers : [
114
115
{
115
116
provide : AuthenticationServicesToken ,
116
- useValue : { password : AccountsPassword } ,
117
+ useValue : { } ,
117
118
global : true ,
118
119
} ,
119
120
] ,
120
- schemaBuilder : ( ) =>
121
+ schemaBuilder : ( { typeDefs , resolvers } ) =>
121
122
authDirectiveTransformer (
122
123
stitchSchemas ( {
123
124
subschemas : [ remoteSubschema ] ,
@@ -127,22 +128,16 @@ const accountsServerUri = 'http://localhost:4003/';
127
128
) ,
128
129
} ) ;
129
130
130
- const schema = createSchemaForApollo ( ) ;
131
+ const { createOperationController } = app ;
131
132
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 ) ] ,
144
135
context : ( ctx ) => context ( ctx , { createOperationController } ) ,
145
136
} ) ;
146
137
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
+ } ) ;
148
143
} ) ( ) ;
0 commit comments