
Security News
Socket Integrates With Bun 1.3’s Security Scanner API
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
graphql-annotations
Advanced tools
Annotate a GraphQL schema
npm i graphql-annotations
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 }
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
Annotate a GraphQL Schema
We found that graphql-annotations demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
Research
The Socket Threat Research Team is tracking weekly intrusions into the npm registry that follow a repeatable adversarial playbook used by North Korean state-sponsored actors.
Security News
A data handling bug in OSV.dev caused disputed CVEs to disappear from vulnerability feeds until a recent fix restored over 500 advisories.