-
My old code works like this: import { AccountsModule } from '@accounts/graphql-api';
export const accountsGraphQL = AccountsModule.forRoot({ accountsServer }); This is no more possible with the new changes: const app = createApplication({
modules: [
createAccountsCoreModule({ tokenSecret: 'secret' }),
createAccountsPasswordModule({
// This option is called when a new user create an account
// Inside we can apply our logic to validate the user fields
validateNewUser: (user) => {
if (!user.firstName) {
throw new Error('First name required');
}
if (!user.lastName) {
throw new Error('Last name required');
}
// For example we can allow only some kind of emails
if (user.email.endsWith('.xyz')) {
throw new Error('Invalid email');
}
return user;
},
}),
createAccountsMongoModule({ dbConn }),
],
providers: [
{
provide: AuthenticationServicesToken,
useValue: { password: AccountsPassword },
global: true,
},
],
schemaBuilder: buildSchema({ typeDefs, resolvers }),
});
const { injector, createOperationController } = app; This is a breaking change. As my app is already structured to work with const module = createAccountsCoreModule({ tokenSecret: 'token_secret', });
const typeDefs = module.typeDefs. But it doesn't provide resolvers. import { context } from '@accounts/module-core';
context({ request }, { createOperationController }) Requires On the other hand, I can't continue using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
const { typeDefs, resolvers } = app; Depending on the graphql server of your choice you might be able to use them directly or not. For example with import { createHandler } from 'graphql-http/lib/use/http';
const app = createApplication([...]);
const { injector, createOperationController } = app;
// Create the GraphQL over HTTP Node request handler
const handler = createHandler<Pick<IContext, keyof IContext>>({
schema: app.schema, // schema is basically typedefs + resolvers
execute: app.createExecution(),
context: (request) => context({ request }, { createOperationController }),
}); As you can see you can use typedefs/resolvers from app if you also provide the custom executor. Other graphql servers don't support custom executors in the same way, for example apollo ( const { injector, createOperationController, createApolloGateway } = app;
const gateway = createApolloGateway();
const server = new ApolloServer({gateway}); but you can still customize the typedefs/resolvers via the const app = createApplication({
[...]
// typedefs and resolvers are the additional ones that you want to merge along with the accounts.js ones
schemaBuilder: buildSchema({ typeDefs, resolvers }),
});
Context is a different matter. Since v1 graphql-modules is less strict about inter-module dependencies and no longer provides per-module context. That means that the context is global and you are in charge to provide it. That being said we provide another helper function called import { context } from '@accounts/module-core';
const { createOperationController } = app;
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
context: (ctx) => context(ctx, { createOperationController }),
}); It's a breaking change for sure and a necessary one, but I honestly have an hard time understanding why it would be difficult to refactor your app: just put your custom schema inside |
Beta Was this translation helpful? Give feedback.
app
(meaningcreateApplication
's return value) provides bothtypeDefs
andresolvers
:Depending on the graphql server of your choice you might be able to use them directly or not. For example with
graphql-http
(examples/graphql-server-typescript-basic
):