Skip to content

Commit

Permalink
feat: Add support for aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdao committed Nov 23, 2021
1 parent 521f5a7 commit fd6fcdf
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Notice that `enum` definitions use arrays instead of objects.
## Required anonymous types
### Required anonymous Type and Input

Use the `__required` property as follow:
Use the `__required` property or its alias `!` as follow:

```js
const schema = new Schemax(
Expand All @@ -327,6 +327,8 @@ const schema = new Schemax(
console.log(schema.toString())
```

> With the alias: `users: { where: { id:'ID', first_name:'String', '!':true }, ':': [{`
Which outputs:

```js
Expand All @@ -353,7 +355,7 @@ schema {

### Required anonymous Enum

To make an anonymous enum required, use the reserved `__required` string:
To make an anonymous enum required, use the reserved `__required` string, or its alias `!`:

```js
const schema = [
Expand All @@ -365,6 +367,8 @@ const schema = [
console.log(new Schemax(schema).toString())
```

> With the alias: `['car','home','furniture','!']`
Which outputs:

```js
Expand All @@ -389,7 +393,7 @@ schema {

### Required non-empty anonymous array

Use the `__noempty` keyword to create enums similar to `[RoleEnum!]`.
Use the `__noempty` keyword or its alias `!0` to create enums similar to `[RoleEnum!]`.

```js
const schema = [
Expand All @@ -407,6 +411,8 @@ const schema = [
console.log(new Schemax(schema).toString())
```

> With the alias: `roles:[['admin','writer','reader','__required','!0','__name:RoleEnum']]`
Which outputs:

```js
Expand Down Expand Up @@ -438,7 +444,7 @@ schema {
## Naming anonymous types
### Naming anonymous Type and Input

Use the `__name` property as follow:
Use the `__name` property or its alias `#` as follow:

```js
const schema = new Schemax(
Expand All @@ -456,6 +462,8 @@ const schema = new Schemax(
console.log(schema.toString())
```

> With the alias: `users: { where: { id:'ID', first_name:'String', __required:true, '#': 'WhereUserInput' }, ':': [{`
Which outputs:

```js
Expand All @@ -482,7 +490,7 @@ schema {

### Naming anonymous Enum

To use a custom enum, use the reserved `__name:YOUR_NAME` string:
To use a custom enum, use the reserved `__name:YOUR_NAME` string or its alias version `#YOUR_NAME`:

```js
const schema = [
Expand All @@ -494,6 +502,9 @@ const schema = [
console.log(new Schemax(schema).toString())
```

> With the alias: `['car','home','furniture','#ProductTypeEnum']`
> NOTICE: With the `#` alias, the `:` is dropped.
```js
type Query {
products(type: ProductTypeEnum): Type_12078318863
Expand Down
21 changes: 12 additions & 9 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ const _compileBody = (body, options) => {

// ENUM
if (Array.isArray(_body)) {
const enums = _body.filter(x => x && x != '__required' && x != '__noempty' && x.indexOf('__name') != 0)
const enums = _body.filter(x => x && x != '__required' && x != '!' && x != '__noempty' && x != '!0' && x.indexOf('__name') != 0 && x.indexOf('#') != 0)
if (!enums.length)
throw new Error('Invalid \'body\'. Array cannot be empty.')
if (enums.some(x => typeof(x) != 'string'))
throw new Error('Invalid \'body\' item. When \'body\' is an array, all items must be strings.')
required = _body.some(x => x == '__required')
noempty = _body.some(x => x == '__noempty')
const enumsName = _body.find(x => x && x.indexOf('__name') == 0)
required = _body.some(x => x == '__required' || x == '!')
noempty = _body.some(x => x == '__noempty' || x == '!0')
const enumsName = _body.find(x => x && (x.indexOf('__name') == 0 || x.indexOf('#') == 0))
if (enumsName)
name = (enumsName.match(/:(.*?)$/)||[])[1] || ''
name = /^#/.test(enumsName) ? enumsName.replace('#','') : (enumsName.match(/:(.*?)$/)||[])[1] || ''
bodyString = indent + enums.sort((a,b) => a<b?-1:1).join(`\n${indent}`) + '\n'
type = 'enum'
} else { // TYPE OR INPUT
Expand All @@ -91,12 +91,15 @@ const _compileBody = (body, options) => {
const field = fields[i]
const fieldBody = _body[field]
const t = Array.isArray(fieldBody) ? 'array' : typeof(fieldBody)
if (field.indexOf('__') == 0) {
if (field == '__name')
const isRequired = field == '__required' || field == '!'
const isNoEmpty = field == '__noempty' || field == '!0'
const isAlias = field == '__name' || field.indexOf('#') == 0
if (field.indexOf('__') == 0 || isRequired || isNoEmpty || isAlias) {
if (isAlias)
name = fieldBody
else if (field == '__required')
else if (isRequired)
required = fieldBody
else if (field == '__noempty')
else if (isNoEmpty)
noempty = fieldBody
} else if (!fieldBody)
bodyString += `${indent}${field}\n`
Expand Down
45 changes: 45 additions & 0 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,51 @@ describe('Schemax', () => {
}
]

// console.log(new Schemax(schema).toString())
assert.equal(compressString(new Schemax(schema).toString()), expected)
})
it('16 - Should support for shortcuts \'!\', \'!0\' and \'#\' to respectively replace \'__required\', \'__noempty\', and \'__name\'', () => {

const expected = compressString(`
type Mutation {
invite(users: [UserInviteInput!]!): Message
}
enum RoleEnum {
admin
reader
writer
}
input UserInviteInput {
id: ID
email: String
roles: [RoleEnum!]!
}
type Message {
message: String
}
schema {
mutation: Mutation
}`)

const schema = [
'type Mutation', {
invite: {
users:[{
id:'ID',
email:'String',
roles:[['admin','writer','reader','!','!0','#RoleEnum']],
'!': true,
'!0': true,
'#': 'UserInviteInput'
}],
':':{ message:'String', '#':'Message' } }
}
]

// console.log(new Schemax(schema).toString())
assert.equal(compressString(new Schemax(schema).toString()), expected)
})
Expand Down

0 comments on commit fd6fcdf

Please sign in to comment.