-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
278 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
declare global { | ||
// tslint:disable-next-line:class-name | ||
export interface SELECTOR_META { | ||
gqlFields?: Array<string>; | ||
} | ||
} | ||
|
||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
160 changes: 160 additions & 0 deletions
160
libs/ngrx-entity-relationship/graphql/src/lib/toGraphQL.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import {ENTITY_SELECTOR} from 'ngrx-entity-relationship'; | ||
import 'ngrx-entity-relationship/augments'; | ||
|
||
const resolveGraphQL = ( | ||
selector: ENTITY_SELECTOR, | ||
options: { | ||
include: Array<keyof any>; | ||
prefix: string; | ||
level: number; | ||
} = { | ||
include: [], | ||
prefix: '', | ||
level: 0, | ||
}, | ||
) => { | ||
const prefix = options.prefix ? options.prefix.repeat(options.level) : ''; | ||
let query = ''; | ||
const included: Array<string> = []; | ||
for (const relationship of selector.relationships) { | ||
if (typeof relationship.keyId !== 'string') { | ||
continue; | ||
} | ||
if (typeof relationship.keyValue !== 'string') { | ||
continue; | ||
} | ||
if (relationship.ngrxEntityRelationship === 'childrenEntities') { | ||
included.push(relationship.keyValue); | ||
query += `${prefix}${relationship.keyValue}`; | ||
query += `${prefix ? ' ' : ''}{${prefix ? '\n' : ''}${resolveGraphQL(relationship, { | ||
include: [relationship.keyId], | ||
prefix: options.prefix, | ||
level: options.level + 1, | ||
})}${prefix}}${prefix ? '\n' : ''}`; | ||
} else if (relationship.ngrxEntityRelationship === 'childEntity') { | ||
included.push(relationship.keyValue); | ||
query += `${prefix}${relationship.keyValue}`; | ||
query += `${prefix ? ' ' : ''}{${prefix ? '\n' : ''}${resolveGraphQL(relationship, { | ||
include: [relationship.keyId], | ||
prefix: options.prefix, | ||
level: options.level + 1, | ||
})}${prefix}}${prefix ? '\n' : ''}`; | ||
} else { | ||
included.push(relationship.keyId); | ||
included.push(relationship.keyValue); | ||
query += `${prefix}${relationship.keyId}`; | ||
query += `\n${prefix}${relationship.keyValue}`; | ||
query += `${prefix ? ' ' : ''}{${prefix ? '\n' : ''}${resolveGraphQL(relationship, { | ||
include: [], | ||
prefix: options.prefix, | ||
level: options.level + 1, | ||
})}${prefix}}${prefix ? '\n' : ''}`; | ||
} | ||
} | ||
for (const field of options.include) { | ||
if (typeof field !== 'string') { | ||
continue; | ||
} | ||
if (included.indexOf(field) !== -1) { | ||
continue; | ||
} | ||
query += `${prefix}${field}\n`; | ||
} | ||
const fields = selector.meta.gqlFields || []; | ||
for (const field of fields) { | ||
if (included.indexOf(field) !== -1) { | ||
continue; | ||
} | ||
query += `${prefix}${field}\n`; | ||
} | ||
return query; | ||
}; | ||
|
||
function encodeValue(data: any): string | undefined { | ||
if (typeof data === 'number') { | ||
return JSON.stringify(data); | ||
} | ||
if (typeof data === 'string') { | ||
return JSON.stringify(data); | ||
} | ||
if (data === null || data === undefined) { | ||
return JSON.stringify(null); | ||
} | ||
if (typeof data === 'function') { | ||
return undefined; | ||
} | ||
if (Array.isArray(data)) { | ||
return JSON.stringify(data.map(encodeValue).filter(v => v !== undefined)); | ||
} | ||
const fields: Array<string> = []; | ||
for (const key of Object.keys(data)) { | ||
const value = encodeValue(data[key]); | ||
if (value === undefined) { | ||
continue; | ||
} | ||
fields.push(`${key}:${value}`); | ||
} | ||
return `{${fields.join(',')}}`; | ||
} | ||
|
||
export function toGraphQL(...queries: Array<string>): string; | ||
export function toGraphQL(query: string, params: object, selector: ENTITY_SELECTOR): string; | ||
export function toGraphQL(query: string, selector: ENTITY_SELECTOR): string; | ||
|
||
export function toGraphQL(...queries: Array<any>): string { | ||
const prefix = ''; | ||
let query = ''; | ||
let selector: ENTITY_SELECTOR | undefined; | ||
let params: Record<string, any> | null | string | undefined; | ||
if (queries.length >= 2 && typeof queries[1] !== 'string') { | ||
if (queries[2] && typeof queries[2] !== 'string') { | ||
[query, params, selector] = queries; | ||
} else { | ||
[query, selector] = queries; | ||
} | ||
} | ||
|
||
const stringParams: Array<string> = []; | ||
if (typeof params === 'string') { | ||
stringParams.push(params); | ||
} | ||
if (params && typeof params === 'object') { | ||
for (const key of Object.keys(params)) { | ||
if (key === '$') { | ||
continue; | ||
} | ||
stringParams.push(`${key}:${prefix ? ' ' : ''}${encodeValue(params[key])}`); | ||
} | ||
} | ||
if (params && typeof params === 'object' && params.$ && typeof params.$ === 'object') { | ||
const params$ = params.$; | ||
for (const key of Object.keys(params$)) { | ||
if (typeof params$[key] !== 'string' || params$[key][0] !== '$') { | ||
throw new Error(`${key} should be a variable that starts with $ symbol`); | ||
} | ||
stringParams.push(`${key}:${prefix ? ' ' : ''}${params$[key]}`); | ||
} | ||
} | ||
params = stringParams.length ? `(${stringParams.join(`,${prefix ? ' ' : ''}`)})` : ''; | ||
|
||
if (selector) { | ||
return `{\n${prefix}${query}${params}${prefix ? ' ' : ''}{\n${resolveGraphQL(selector, { | ||
include: [], | ||
prefix: `${prefix}`, | ||
level: 2, | ||
})}${prefix}}\n}`; | ||
} | ||
const parts: Array<string> = []; | ||
for (let part of queries) { | ||
if (typeof part !== 'string') { | ||
continue; | ||
} | ||
part = part.trim(); | ||
if (part.substr(0, 1) === '{' && part.substr(part.length - 1, 1) === '}') { | ||
part = part.substr(1, part.length - 2); | ||
} | ||
parts.push(part.trim()); | ||
} | ||
|
||
return `{${parts.join('\n')}}`; | ||
} |
12 changes: 12 additions & 0 deletions
12
libs/ngrx-entity-relationship/graphql/src/lib/toMutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {toTerm} from './toTerm'; | ||
|
||
export function toMutation(toGraphQL: string): string; | ||
export function toMutation( | ||
variables: { | ||
[key: string]: 'ID' | 'ID!' | 'String!' | 'String' | 'Boolean!' | 'Boolean' | string; | ||
}, | ||
toGraphQL: string, | ||
): string; | ||
export function toMutation(...args: Array<any>): string { | ||
return toTerm('mutation', ...args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {toTerm} from './toTerm'; | ||
|
||
export function toQuery(toGraphQL: string): string; | ||
export function toQuery( | ||
variables: { | ||
[key: string]: 'ID' | 'ID!' | 'String!' | 'String' | 'Boolean!' | 'Boolean' | string; | ||
}, | ||
toGraphQL: string, | ||
): string; | ||
export function toQuery(...args: Array<any>): string { | ||
return toTerm('query', ...args); | ||
} |
12 changes: 12 additions & 0 deletions
12
libs/ngrx-entity-relationship/graphql/src/lib/toSubscription.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {toTerm} from './toTerm'; | ||
|
||
export function toSubscription(toGraphQL: string): string; | ||
export function toSubscription( | ||
variables: { | ||
[key: string]: 'ID' | 'ID!' | 'String!' | 'String' | 'Boolean!' | 'Boolean' | string; | ||
}, | ||
toGraphQL: string, | ||
): string; | ||
export function toSubscription(...args: Array<any>): string { | ||
return toTerm('subscription', ...args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function toTerm(term: 'query' | 'subscription' | 'mutation', ...args: Array<any>): string { | ||
const params: Array<string> = []; | ||
let toGraphQL = ''; | ||
|
||
if (typeof args[0] === 'object') { | ||
for (const key of Object.keys(args[0] || {})) { | ||
params.push(`${key}:${args[0][key]}`); | ||
} | ||
toGraphQL = args[1]; | ||
} else { | ||
toGraphQL = args[0]; | ||
} | ||
|
||
return `${term}${params.length ? `(${params.join(',')})` : ''}${toGraphQL}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'ngrx-entity-relationship/augments'; | ||
|
||
export * from './lib/toGraphQL'; | ||
export * from './lib/toMutation'; | ||
export * from './lib/toQuery'; | ||
export * from './lib/toSubscription'; |
8 changes: 8 additions & 0 deletions
8
libs/ngrx-entity-relationship/test/graphql/src/lib/toGraphQL.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {toGraphQL} from '../../../../graphql/src/lib/toGraphQL'; | ||
|
||
describe('toGraphQL', () => { | ||
it('combines strings', () => { | ||
const actual = toGraphQL('1', '2', '3'); | ||
expect(actual).toEqual('{1\n2\n3}'); | ||
}); | ||
}); |
4 changes: 2 additions & 2 deletions
4
...ship/test/operators/relationships.spec.ts → .../rxjs/src/operators/relationships.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...ity-relationship/test/childEntity.spec.ts → ...tionship/test/src/lib/childEntity.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...tionship/test/childEntitySelector.spec.ts → .../test/src/lib/childEntitySelector.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...elationship/test/childrenEntities.spec.ts → ...hip/test/src/lib/childrenEntities.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...hip/test/childrenEntitiesSelector.spec.ts → .../src/lib/childrenEntitiesSelector.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...y-relationship/test/relatedEntity.spec.ts → ...onship/test/src/lib/relatedEntity.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...onship/test/relatedEntitySelector.spec.ts → ...est/src/lib/relatedEntitySelector.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 4 additions & 9 deletions
13
...ty-relationship/test/rootEntities.spec.ts → ...ionship/test/src/lib/rootEntities.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...tity-relationship/test/rootEntity.spec.ts → ...ationship/test/src/lib/rootEntity.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ationship/test/rootEntitySelector.spec.ts → ...p/test/src/lib/rootEntitySelector.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...y-relationship/test/store/actions.spec.ts → ...onship/test/src/lib/store/actions.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...-relationship/test/store/fromFlat.spec.ts → ...nship/test/src/lib/store/fromFlat.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...relationship/test/store/fromGraph.spec.ts → ...ship/test/src/lib/store/fromGraph.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...ationship/test/store/injectEntity.spec.ts → ...p/test/src/lib/store/injectEntity.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...ore/ngrxEntityRelationshipReducer.spec.ts → ...ore/ngrxEntityRelationshipReducer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...elationship/test/store/patchState.spec.ts → ...hip/test/src/lib/store/patchState.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rx-entity-relationship/test/utils.spec.ts → ...y-relationship/test/src/lib/utils.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.