When starting a backend project, two of the biggest concerns will usually be the right structure of the project and authentication. If you could skip thinking and planning about these two, starting a new backend project can be much easier.
If you haven’t checked out our blog post about authentication and authorization in GraphQL Modules, please read that before!
Internally, we use GraphQL-Modules and AccountsJS to help us with those two decisions, GraphQL-Modules helps us solve our architectural problems in modular, schema-first approaches with the power of GraphQL and AccountsJS helps us create our authentication solutions by providing a simple API together with client and server libraries that saves us a lot of the ground work around authentication.
It is really customizable; so you can write any plugins for your own authentication methods or use the already existing email-password or the Facebook and Twitter OAuth integration packages.
AccountsJS has connector libraries for MongoDB and Redis, but you can write your own database handler by implementing a simple interface.
Accounts JS provide a ready to use GraphQL API if you install their GraphQL library and we are happy to announce that the GraphQL library is now internally built using GraphQL-Modules!
It doesn’t affect people who are not using GraphQL Modules, but it helps the maintainers of AccountsJS and simplifies the integration for GraphQL-Modules-based projects.
How To Implement Server-Side using AccountsJS, GraphQL-Modules and Apollo-Server
First install required dependencies from npm or yarn;
yarn add mongodb @accounts/server @accounts/password @accounts/database-manager @accounts/mongo @accounts/graphql-api @graphql-modules/core apollo-server graphql-import-node
Let’s assume that we’re using MongoDB as our database, password-based authentication and ApolloServer;
And we can extend User type with custom fields in our schema, and add a mutation which is restricted to authenticated clients.
Finally let’s define some resolvers for it;
When you print the whole app’s schema, you would see something like above;
How To Implement Client-Side using AccountsJS, React and Apollo-Client
Now we can create a simple frontend app by using Apollo-Client and AccountsJS client for this backend app. The example below shows some example code that works on these two.
As you can see from the example, it can be really easy to create an application that has authentication in modular and future proof approach.
You can learn more about AccountsJS from the docs of this great library for more features such as Two-Factor Authentication and Facebook and Twitter integration using OAuth.
Also you can learn more about GraphQL-Modules on the website and see how you can add GraphQL Modules features into your system in a gradual and selective way.
If you want strict types based on GraphQL Schema, for each module, GraphQL Code Generator has built-in support for GraphQL-Modules based projects. See the docs for more details.
You can check out our example about this integration;
All posts about GraphQL Modules
- GraphQL Modules — Feature based GraphQL Modules at scale
- Why is True Modular Encapsulation So Important in Large-Scale GraphQL Projects?
- Why did we implement our own Dependency Injection library for GraphQL-Modules?
- Scoped Providers in GraphQL-Modules Dependency Injection
- Writing a GraphQL TypeScript project w/ GraphQL-Modules and GraphQL-Code-Generator
- Authentication and Authorization in GraphQL (and how GraphQL-Modules can help)
- Authentication with AccountsJS & GraphQL Modules
- Manage Circular Imports Hell with GraphQL-Modules