Skip to content
Annotate your graphql schema with lightweight directives
JavaScript
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
.vscode
src
tests/specs
typings
.eslintignore
.eslintrc.js
.gitignore
LICENSE
README.md
jest.config.js
package.json
tsconfig.json
yarn.lock

README.md

graphql-metadata

Attach metadata to your GraphQL schema using directive like syntax.

Library supoports following formats:

  • Annotations: Group of elements with common namespace. For example @db.length: 200
  • Marker: Single instance (key) with multiple values. For example @db length:200

Installation

npm i graphql-metadata

Usage

Annotations parsing

Here is a very basic example with a namespace (here 'db') and a description that needs to be parsed:

const { parseAnnotations } = require('graphql-metadata')

const result = parseAnnotations('db', `
  This is a description
  @db.length: 200
  @db.foo: 'bar'
  @db.unique
  @db.index: { name: 'foo', type: 'string' }
`)

console.log(result)

This will output an object containing the annotations:

{
  length: 200,
  foo: 'bar',
  unique: true,
  index: { name: 'foo', type: 'string' }
}

In a GraphQL schema, you can use the description property on GraphQLObjectType, GraphQLField...

const { parseAnnotations } = require('graphql-metadata')
const { buildSchema, isObjectType } = require('graphql')

const schema = buildSchema(`
  """
  @db.table: 'users'
  """
  type User {
    """
    @db.primary
    """
    id: ID!
  }
`)

const typeMap = schema.getTypeMap()
for (const key in typeMap) {
  const type = typeMap[key]
  // Tables
  if (isObjectType(type)) {
    const typeAnnotations = parseAnnotations('db', type.description)
    console.log(type.name, typeAnnotations)
    const fields = type.getFields()
    for (const key in fields) {
      const field = fields[key]
      const fieldAnnotations = parseAnnotations('db', field.description)
      console.log(field.name, fieldAnnotations)
    }
  }
}

Which will output:

User { table: 'users' }
id { primary: true }

Marker parsing

Markers using different syntax for elements that do not support grouping. For example @marker true etc.

Usage:

const result = parseMarker('db', `
  This is a description
  @db length:200, unique: true 
`)

No value usage:

const result = parseMarker('db', `
  This is a description
  @db
`)

Strip annotations

Sometimes it will be helpful to strip the annotations from the description. For example, you may not want to display them in a GraphQL schema explorer.

const { stripAnnotations } = require('graphql-metadata')

const result = stripAnnotations('db', `
  This is a description
  @db.length: 200
  @db.foo: 'bar'
  @db.unique
  @db.index: { name: 'foo', type: 'string' }
`)

console.log(result)

The result will be:

`
  This is a description
`

Relation to GraphQL-Annotations

NOTE: This package is an customized version of https://github.com/Akryum/graphql-annotations If you like it please consider donating to Akryum patreon

Become a Patreon

You can’t perform that action at this time.