@@ -16,22 +16,23 @@ import { delegateToSchema } from '@graphql-tools/delegate';
1616import { createApplication , createModule , gql } from 'graphql-modules' ;
1717import { AuthenticationServicesToken } from '@accounts/server' ;
1818import { 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} ) ( ) ;
0 commit comments