Skip to content

Commit

Permalink
feat(gql): a way to get a block and a shape for gql
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jul 12, 2021
1 parent e9cb5e7 commit c322e23
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
34 changes: 34 additions & 0 deletions docs/articles/guide/graphql/quick.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,37 @@ It will generate:
}
}
```

It is also possible to generate a block with fields if to pass an empty string as the query parameter.

```ts
const shape = toGraphQL('', selectUser);
```

It will generate:

```graphql
{
id
someRel {
id
}
# ...
}
```

Or to get a shape if to pass a selector only.

```ts
const shape = toGraphQL(selectUser);
```

It will generate:

```graphql
id
someRel {
id
}
# ...
```
18 changes: 15 additions & 3 deletions libs/ngrx-entity-relationship/graphql/src/lib/toGraphQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ function encodeValue(data: any): string | undefined {
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(selector: ENTITY_SELECTOR): string;

export function toGraphQL(...queries: Array<any>): string {
const prefix = (window as any).ngrxGraphqlPrefix || '';
let query = '';
let query: string | undefined = '';
let selector: ENTITY_SELECTOR | undefined;
let params: Record<string, any> | null | string | undefined;
if (queries.length >= 2 && typeof queries[1] !== 'string') {
Expand All @@ -138,6 +139,9 @@ export function toGraphQL(...queries: Array<any>): string {
} else {
[query, selector] = queries;
}
} else if (queries.length === 1 && typeof queries[0] !== 'string') {
[selector] = queries;
query = undefined;
}

const stringParams: Array<string> = [];
Expand Down Expand Up @@ -165,11 +169,19 @@ export function toGraphQL(...queries: Array<any>): string {
params = stringParams.length ? `(${stringParams.join(`,${prefix ? ' ' : ''}`)})` : '';

if (selector) {
return `{\n${prefix}${query}${params}${prefix ? ' ' : ''}{\n${resolveGraphQL(selector, {
let gql = resolveGraphQL(selector, {
include: [],
prefix: `${prefix}`,
level: 2,
})}${prefix}}\n}`;
});

if (query === undefined) {
return gql;
}

gql = `{\n${gql}${prefix}}`;

return query ? `{\n${prefix}${query}${params}${prefix ? ' ' : ''}${gql}\n}` : gql;
}
const parts: Array<string> = [];
for (let part of queries) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {childEntitySelector, rootEntitySelector} from 'ngrx-entity-relationship';
import {toGraphQL} from 'ngrx-entity-relationship/graphql';

describe('toGraphQL', () => {
const root = rootEntitySelector(jasmine.createSpy(), {
gqlFields: ['id', 'name'],
});
const rootChild = childEntitySelector<any, any, any>(jasmine.createSpy(), 'childId', 'child', {
gqlFields: ['subId', 'subName'],
});
const selector = root(rootChild());

const normalize = (query: string) =>
query
.replace(/\s+/gm, ' ')
.replace(/\s*([{}])\s*/gm, '$1')
.trim();

it('generates a wrapped block with a query', () => {
expect(normalize(toGraphQL('data', selector))).toEqual(
normalize(`
{
data {
child {
childId
subId
subName
}
id
name
}
}`),
);
});

it('generates a block with an empty query', () => {
expect(normalize(toGraphQL('', selector))).toEqual(
normalize(`
{
child {
childId
subId
subName
}
id
name
}
`),
);
});

it('generates fields without a query', () => {
expect(normalize(toGraphQL(selector))).toEqual(
normalize(`
child {
childId
subId
subName
}
id
name
`),
);
});
});

0 comments on commit c322e23

Please sign in to comment.