graphql-metadata
Attach metadata to your GraphQL schema using directive like syntax.
Library supoports following formats:
- (DEPRECATED) Annotations: Group of elements with common namespace. For example
@db.length: 200
- Marker: Single instance (key) with multiple values. For example
@db length:200
- Metadata: Directive-like config. For example
@db(length: 200, columns: ['id', 'name'])
Installation
npm i graphql-metadata
Usage
Marker parsing
Markers using different syntax for elements that do not support grouping.
For example @marker true
etc.
Usage:
const result =
No value usage:
const result =
Metadata parsing
Metadata uses the same syntax as GraphQL directives.
Usage:
const field: GraphQLField<anyany> = ... description: `@db(length:200, unique: true, columns: ['id', 'name'] description: 'Some description' )`const result = // Returns: length:200 unique: true columns: 'id' 'name' description: 'Some description'
No value usage:
const field: GraphQLField<anyany> = ... description: '@db'const result = // Returns true
Or with a string:
const result =
[DEPRECATED] Annotations parsing
Here is a very basic example with a namespace
(here 'db'
) and a description
that needs to be parsed:
const parseAnnotations = const result = console
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 = const buildSchema isObjectType = const schema = const typeMap = schemafor const key in typeMap const type = typeMapkey // Tables if const typeAnnotations = console const fields = type for const key in fields const field = fieldskey const fieldAnnotations = console
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 = const result = console
The result will be:
` This is a description`