Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

graphql-annotations

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-annotations

Annotate a GraphQL Schema

latest
Source
npmnpm
Version
0.0.3
Version published
Maintainers
1
Created
Source

graphql-annotations

circleci

Annotate a GraphQL schema

Become a Patreon

Sponsors

Silver

VueSchool logo

Bronze

Installation

npm i graphql-annotations

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-annotations')

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-annotations')
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 }

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-annotations')

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
`

FAQs

Package last updated on 02 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts