graphql-schemax
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -5,2 +5,9 @@ # Changelog | ||
### [0.0.8](https://github.com/nicolasdao/graphql-schemax/compare/v0.0.7...v0.0.8) (2021-11-01) | ||
### Features | ||
* Add support for a new API called 'addTypeResolutions' which can resolved advanced type conflicts ([e372dcd](https://github.com/nicolasdao/graphql-schemax/commit/e372dcd19d478af431d9f4a9cfd556bbc3d99e83)) | ||
### [0.0.7](https://github.com/nicolasdao/graphql-schemax/compare/v0.0.6...v0.0.7) (2021-10-21) | ||
@@ -7,0 +14,0 @@ |
{ | ||
"name": "graphql-schemax", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Creates GraphQL string schema from plain JSON objects.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
225
README.md
@@ -104,2 +104,8 @@ # GRAPHQL-SCHEMAX | ||
> - [`add`](#add) | ||
> - [`addTypeResolutions`](#addtyperesolutions) | ||
> - [Renaming a type](#renaming-a-type) | ||
> - [Merging types](#merging-types) | ||
> - [Keeping the longest type](#keeping-the-longest-type) | ||
> - [Keeping the shortest type](#keeping-the-shortest-type) | ||
> - [Custom merge](#custom-merge) | ||
> * [Dev](#dev) | ||
@@ -709,2 +715,217 @@ > - [About this project](#about-this-project) | ||
## `addTypeResolutions` | ||
Helps renaming or merging types by explicitly controlling how type names are resolved. | ||
### Renaming a type | ||
```js | ||
import { Schemax } from 'graphql-schemax' | ||
const inlineSchema01 = [ | ||
'type Project', { | ||
id: 'ID', | ||
name: 'String' | ||
}, | ||
'type Query', { | ||
projects: '[Project]' | ||
}] | ||
const inlineSchema02 = [ | ||
'type User', { | ||
id: 'ID', | ||
first_name: 'String', | ||
last_name: 'String' | ||
}, | ||
'type Query', { | ||
users: '[User]' | ||
}] | ||
const schema = new Schemax(...inlineSchema01, ...inlineSchema02) | ||
schema.addTypeResolutions([{ | ||
def: 'type User', to: 'type User @aws_api_key' | ||
}]) | ||
console.log(schema.toString()) | ||
``` | ||
### Merging types | ||
#### Keeping the longest type | ||
```js | ||
import { Schemax } from 'graphql-schemax' | ||
const inlineSchema01 = [ | ||
'type Project', { | ||
id: 'ID', | ||
name: 'String' | ||
}, | ||
'type Query', { | ||
projects: '[Project]' | ||
}] | ||
const inlineSchema02 = [ | ||
'type User', { | ||
id: 'ID', | ||
first_name: 'String', | ||
last_name: 'String' | ||
}, | ||
'type Query @aws_api_key', { | ||
users: '[User]' | ||
}] | ||
const schema = new Schemax(...inlineSchema01, ...inlineSchema02) | ||
schema.addTypeResolutions([{ | ||
def: /^type Query(\s|$)/, keepLongest:true | ||
}]) | ||
console.log(schema.toString()) | ||
``` | ||
Which returns: | ||
```js | ||
type Project { | ||
id: ID | ||
name: String | ||
} | ||
type User { | ||
id: ID | ||
first_name: String | ||
last_name: String | ||
} | ||
type Query @aws_api_key { | ||
projects: [Project] | ||
users: [User] | ||
} | ||
schema { | ||
query: Query | ||
} | ||
``` | ||
#### Keeping the shortest type | ||
```js | ||
import { Schemax } from 'graphql-schemax' | ||
const inlineSchema01 = [ | ||
'type Project', { | ||
id: 'ID', | ||
name: 'String' | ||
}, | ||
'type Query', { | ||
projects: '[Project]' | ||
}] | ||
const inlineSchema02 = [ | ||
'type User', { | ||
id: 'ID', | ||
first_name: 'String', | ||
last_name: 'String' | ||
}, | ||
'type Query @aws_api_key', { | ||
users: '[User]' | ||
}] | ||
const schema = new Schemax(...inlineSchema01, ...inlineSchema02) | ||
schema.addTypeResolutions([{ | ||
def: /^type Query(\s|$)/, keepShortest:true | ||
}]) | ||
console.log(schema.toString()) | ||
``` | ||
Which returns: | ||
```js | ||
type Project { | ||
id: ID | ||
name: String | ||
} | ||
type User { | ||
id: ID | ||
first_name: String | ||
last_name: String | ||
} | ||
type Query { | ||
projects: [Project] | ||
users: [User] | ||
} | ||
schema { | ||
query: Query | ||
} | ||
``` | ||
#### Custom merge | ||
```js | ||
import { Schemax } from 'graphql-schemax' | ||
const inlineSchema01 = [ | ||
'type Project', { | ||
id: 'ID', | ||
name: 'String' | ||
}, | ||
'type Query @aws_cognito_user_pools', { | ||
projects: '[Project]' | ||
}] | ||
const inlineSchema02 = [ | ||
'type User', { | ||
id: 'ID', | ||
first_name: 'String', | ||
last_name: 'String' | ||
}, | ||
'type Query @aws_api_key', { | ||
users: '[User]' | ||
}] | ||
const schema = new Schemax(...inlineSchema01, ...inlineSchema02) | ||
schema.addTypeResolutions([{ | ||
def: /^type Query(\s|$)/, | ||
reduce:(oldType, newType, context) => { // fired for each type that matches the regex /^type Query(\s|$)/ | ||
const attributes = newType.replace('type Query', '').split(' ').filter(x => x) | ||
if (!context.attributes) | ||
context.attributes = new Set(attributes) | ||
else | ||
attributes.forEach(a => context.attributes.add(a)) | ||
const attrs = Array.from(context.attributes) | ||
return attrs.length ? `type Query ${attrs.join(' ')}` : 'type Query' | ||
} | ||
}]) | ||
console.log(schema.toString()) | ||
``` | ||
Which returns: | ||
```js | ||
type Project { | ||
id: ID | ||
name: String | ||
} | ||
type User { | ||
id: ID | ||
first_name: String | ||
last_name: String | ||
} | ||
type Query @aws_cognito_user_pools @aws_api_key { | ||
projects: [Project] | ||
users: [User] | ||
} | ||
schema { | ||
query: Query | ||
} | ||
``` | ||
# Dev | ||
@@ -738,3 +959,3 @@ ## About this project | ||
They are merged in a single type definition. This is how the `type Query`, `type Mutation` and `type Subscription` can be defined in multiple microservice schemas and merged into a single GraphQL schema. For example: | ||
They are merged into a single type definition. This is how the `type Query`, `type Mutation` and `type Subscription` can be defined in multiple microservice schemas and merged into a single GraphQL schema. For example: | ||
@@ -798,2 +1019,4 @@ ```js | ||
> __WARNING:__ If the type definitions are not exactly identical, the type are considered different and won't be merged. This means that identical types with different directive won't be merged automatically. To deal with this advanced scenario, use the [`addTypeResolutions`](#addtyperesolutions) API. | ||
# License | ||
@@ -800,0 +1023,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45060
590
1051