LogoLogo
CS CONSOLECOMMUNITYDOCS
  • What is code.store?
  • Our vision
  • How it works?
  • Getting started
    • Quick Start
      • Core concepts
      • Quick Start with CLI
      • Quick Start with Web Interface
    • FUNDAMENTALS
      • Services
        • GraphQL
        • REST
        • Configuration
        • Environment Variables
      • Environments
      • Projects
      • Versioning
      • Access and Authorization
      • Secret management
      • Data management
        • Import and Export
      • Generation
        • Models
        • Handlers
        • Resolvers
        • REST
        • Middlewares
      • Logging
      • Microservices
        • Communication
    • RECIPES
      • TypeScript
        • Custom .tslint
      • Basic authentication
      • Setup a local database with your service
      • GraphQL
    • Tutorials
      • Auth0 authentication
      • Metabase integration
      • Database manipulations
        • External database: MongoDB
        • External database: DynamoDB
      • Wrapping an NPM package with GraphQL: a chess server
    • FAQ
  • Command Line Interface
    • Getting started
    • Commands
Powered by GitBook
On this page
  • Overview
  • Generation
  • Auth handler
  • Context handler

Was this helpful?

Export as PDF
  1. Getting started
  2. FUNDAMENTALS
  3. Generation

Handlers

Overview

To simplify authorization and authentication process code.store platform provide an opportunity to create an authorization handler for REST and context handler for your GraphQL API.

Authorization handlers released as middlewares and allows to implement custom authorization logic.

Generation

Auth handler

To generate auth handler execute cs generate:resolver CLI command, select auth handler type:

> cs generate:handler

? Select the type of resolver (Use arrow keys)
❯ auth 
  context 

Also, you can use flags:

-t handler type, one of: 'context' or 'auth'

After command execution will be generated file auth.handler.ts in src/resolvers directory:

import { AuthHandler } from 'codestore-utils';

const authHandler: AuthHandler = async (context) => {
  // your code goes here
  return {};
};

export default authHandler;

Usage

You can specify your authentication strategy here. This handler works as an authentication middleware which will be called for all GraphQL queries/mutations marked with directive @auth.

In order to use that directive in your GraphQL schema, add a following directive declaration at the top of your GraphQL file:

directive @auth on FIELD_DEFINITION

Then use it in query/mutation declarations, for example:

type Query {
    helloWorld: String! @auth
}

Context handler

Use context handler as a handler, which will be execute before each GraphQL resolver. To generate context handler execute cs generate:resolver CLI command, select context handler type:

> cs generate:handler

? Select the type of resolver (Use arrow keys)
  auth 
❯ context 

Also, you can use flags:

-t handler type, one of: 'context' or 'auth'

After command execution will be generated file context.handler.ts in src/resolvers directory:

import { ContextHandler } from 'codestore-utils';

const contextHandler: ContextHandler = async (context) => ({
  //  your code goes here
});

export default contextHandler;

PreviousModelsNextResolvers

Last updated 4 years ago

Was this helpful?