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