Skip to content

Commit

Permalink
feat: Add constructor support for array of inline schema definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdao committed Oct 21, 2021
1 parent 6866bbf commit 23976ce
Show file tree
Hide file tree
Showing 3 changed files with 471 additions and 6 deletions.
106 changes: 103 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Creates GraphQL string schema from plain JSON objects. Managing GraphQL schemas
- Composition: Reuse the same base object in inputs or types.
- Separation of concerns: Isolate your domain in modules and merge them later.
- Inspection: Easily traverse the schema tree.
- Leaner: Define GraphQL types, enums and inputs on-the-fly instead of having to define them explicitly.
- Leaner: Define GraphQL types, enums and inputs on-the-fly (_anonymous types_) instead of having to define them explicitly.

This package works with both ES6 modules and CommonJS and contains no dependencies.

Expand Down Expand Up @@ -429,13 +429,113 @@ schema {

Notice how the directive must be followed by `null`.

# Manipulating schemas
## Merging schemas
# APIs
## `constructor`

The `Schemax` class supports an undefined amount of arguments. It supports an inline schema definitions as well as arrays of inline schema definitions.

__*Inline schema definitions*__

```js
import { Schemax } from 'graphql-schemax'

const schema = new Schemax(
'type Project', {
id: 'ID',
name: 'String'
},
'type User', {
id: 'ID',
first_name: 'String',
last_name: 'String'
},
'type Query', {
projects: '[Project]',
users: '[User]'
}
)

console.log(schema.toString())
```

Which can also be written as follow:

```js
import { Schemax } from 'graphql-schemax'

const inlineSchema = [
'type Project', {
id: 'ID',
name: 'String'
},
'type User', {
id: 'ID',
first_name: 'String',
last_name: 'String'
},
'type Query', {
projects: '[Project]',
users: '[User]'
}]

const schema = new Schemax(...inlineSchema)

console.log(schema.toString())
```

__*Array schema definitions*__

```js
import { Schemax } from 'graphql-schemax'

const inlineSchema = [
'type Project', {
id: 'ID',
name: 'String'
},
'type User', {
id: 'ID',
first_name: 'String',
last_name: 'String'
},
'type Query', {
projects: '[Project]',
users: '[User]'
}]

const schema = new Schemax(inlineSchema)

console.log(schema.toString())
```

or

```js
import { Schemax } from 'graphql-schemax'

const inlineSchema = [
'type Project', {
id: 'ID',
name: 'String'
},
'type User', {
id: 'ID',
first_name: 'String',
last_name: 'String'
},
'type Query', {
projects: '[Project]',
users: '[User]'
}]

const schema = new Schemax(inlineSchema)

console.log(schema.toString())
```

# FAQ
## How to merge schemas?

# Dev
## About this project

Expand Down
16 changes: 14 additions & 2 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@


export function Schemax(...items) {
const _items = [...items]
const _items = items.reduce((acc,item) => {
if (Array.isArray(item) && item.some(x => typeof(x) != 'string'))
acc.push(...item)
else
acc.push(item)
return acc
},[])

this.toString = () => {
return _transpileSchema(_items)
}

this.add = (...args) => {
_items.push(...args)
for (let i=0;i<args.length;i++) {
const item = args[i]
if (Array.isArray(item) && item.some(x => typeof(x) != 'string'))
_items.push(...item)
else
_items.push(item)
}
}

return this
Expand Down
Loading

0 comments on commit 23976ce

Please sign in to comment.