@@ -11,27 +11,26 @@ import {
1111 context ,
1212 createAccountsCoreModule ,
1313} from '@accounts/module-core' ;
14- import { createAccountsPasswordModule } from '@accounts/module-password' ;
1514import { delegateToSchema } from '@graphql-tools/delegate' ;
1615import { createApplication , createModule , gql } from 'graphql-modules' ;
1716import { 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} ) ( ) ;
0 commit comments