{"id":21822689,"url":"https://github.com/unirakun/graphql-directives-middlewares","last_synced_at":"2025-07-02T14:33:48.736Z","repository":{"id":38218659,"uuid":"160669467","full_name":"unirakun/graphql-directives-middlewares","owner":"unirakun","description":"GraphQL directives as middlewares","archived":false,"fork":false,"pushed_at":"2023-01-06T01:59:34.000Z","size":1643,"stargazers_count":18,"open_issues_count":14,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-21T11:48:58.257Z","etag":null,"topics":["directive","graphql","middleware","nantes","order","preserve","sort","sorted"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/unirakun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-06T12:07:45.000Z","updated_at":"2021-12-12T03:03:13.000Z","dependencies_parsed_at":"2023-02-05T02:16:16.149Z","dependency_job_id":null,"html_url":"https://github.com/unirakun/graphql-directives-middlewares","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/unirakun/graphql-directives-middlewares","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unirakun%2Fgraphql-directives-middlewares","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unirakun%2Fgraphql-directives-middlewares/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unirakun%2Fgraphql-directives-middlewares/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unirakun%2Fgraphql-directives-middlewares/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/unirakun","download_url":"https://codeload.github.com/unirakun/graphql-directives-middlewares/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unirakun%2Fgraphql-directives-middlewares/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261162117,"owners_count":23118246,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["directive","graphql","middleware","nantes","order","preserve","sort","sorted"],"created_at":"2024-11-27T17:16:52.988Z","updated_at":"2025-07-02T14:33:48.696Z","avatar_url":"https://github.com/unirakun.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# graphql-directives-middlewares\n\n\u003e GraphQL directives as middlewares\n\n[![npm bundle size](https://img.shields.io/bundlephobia/minzip/graphql-directives-middlewares)](https://bundlephobia.com/result?p=graphql-directives-middlewares)\n[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/unirakun/graphql-directives-middlewares/Quality)](https://github.com/unirakun/graphql-directives-middlewares/actions?query=branch%3Amaster) [![NPM Version](https://badge.fury.io/js/graphql-directives-middlewares.svg)](https://www.npmjs.com/package/graphql-directives-middlewares) [![Coveralls github](https://img.shields.io/coveralls/github/unirakun/graphql-directives-middlewares.svg)](https://coveralls.io/github/unirakun/graphql-directives-middlewares)\n\n## install\n\n`yarn add graphql-directives-middlewares`\n\n\u003e You need to have `graphql` and `graphql-tools` installed on your project.\n\n## why\n\nWe create this library because we needed our directives to be sorted at runtime.\nWe were on the case we needed to execute our `@auth` directive BEFORE our `@api` directive take place.\n\nWith `graphql-directives-middlewares` you just have to declare, in your schema, the directives in order you want them to take place.\n\nWith this declaration:\n\n```gql\ntype Query {\n  users: [User] @auth(requires: ADMIN) @api(name: \"users\")\n}\n```\n\nYour `@auth` directive will be called BEFORE the `@api` one.\n\nIf you invert `@auth` and `@api`, then `@api` directive will be called BEFORE the `@auth`, without changing your implementation!\n\n## API\n\n### createVisitFieldDefinition\n\n`createVisitFieldDefinition(name: string, impl: (params, next) -\u003e (...args))`\n\n- `name`: the directive name you use in your `gql` schema\n- `impl`: is a function that take `params` and `next` and return a function that is your graphql custom resolver\n  - `params` are your directives arguments\n  - `next` is the next resolver to call, like in a middleware engine\n\n### createVisitObject\n\n`createVisitObject(name: string, impl: (params, next) -\u003e (...args))`\n\n- `name`: the directive name you use in your `gql` schema\n- `impl`: is a function that take `params` and `next` and return a function that is your graphql custom resolver\n  - `params` are your directives arguments\n  - `next` is the next resolver to call, like in a middleware engine\n\n## usage\n\nExample with a `@auth` directive\n\n1. define your directive in the schema\n\n```js\nexport default `\n  enum Role {\n    ADMIN\n    USER\n    VIEWER\n  }\n\n  directive @auth(requires: Role = ADMIN) on FIELD_DEFINITION\n`\n```\n\n2. create your directive implementation\n\n```js\nimport { GraphQLList } from 'graphql'\nimport { createVisitFieldDefinition } from 'graphql-directives-middlewares'\n\nexport default createVisitFieldDefinition(\n  'auth',\n  (params, next) =\u003e async (...args) =\u003e {\n    const { requires: requiredRole } = params\n\n    if (!requiredRole) {\n      return next()\n    }\n\n    const [, , context, definition] = args\n    const { role } = context\n\n    if (!role.includes(requiredRole)) {\n      if (definition.returnType instanceof GraphQLList) return []\n      throw new Error('Unauthorized')\n    }\n\n    return next()\n  },\n)\n```\n\n3. bind your directives implementation to your schema\n\n```js\nimport { makeExecutableSchema } from 'graphql-tools'\nimport typeDefs from './types'\nimport auth from './auth'\n\nexport default () =\u003e {\n  const resolvers = {\n    Query: {\n      users: () =\u003e [\n        {\n          id: 'fabien-juif-1',\n          fullName: 'Fabien JUIF',\n        },\n      ],\n    },\n  }\n\n  return makeExecutableSchema({\n    typeDefs,\n    resolvers,\n    schemaDirectives: {\n      auth,\n    },\n  })\n}\n```\n\n4. use your directives\n\n```js\nimport { gql } from 'apollo-server'\nimport user from './user'\n\nexport default gql`\n  ${user}\n\n  type Query {\n    users: [User] @auth(requires: ADMIN)\n  }\n`\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funirakun%2Fgraphql-directives-middlewares","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funirakun%2Fgraphql-directives-middlewares","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funirakun%2Fgraphql-directives-middlewares/lists"}