Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Migration & History
-------------------
- [ ] Get this PR from launchql-gen: https://github.com/launchql/launchql-gen/pull/19
- [ ] Move postgraphile-* plugins over (preserve import history)
- [ ] Import original LaunchQL history (preserve git log)
- [x] Import original LaunchQL history (preserve git log) (3c31d9b64d266a7dca2595f5aeddc22954bc76a6)

Misc
----
Expand Down
26 changes: 13 additions & 13 deletions packages/launchql-gen/__tests__/__snapshots__/gql.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3539,7 +3539,7 @@ exports[`GraphQL Code Generation getMany(): works with nested selection: getMany
createdAt
updatedAt
actionId
userActionItems(first: 3) {
userActionItems(first: 3, offset: 0) {
nodes {
id
value
Expand Down Expand Up @@ -3596,7 +3596,7 @@ exports[`GraphQL Code Generation getManyPaginatedNodes(): works with nested sele
createdAt
updatedAt
actionId
userActionItems(first: 3) {
userActionItems(first: 3, offset: 0) {
nodes {
id
value
Expand Down Expand Up @@ -3645,7 +3645,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
createdAt
updatedAt
ownerId
actionGoals(first: 3) {
actionGoals(first: 3, offset: 0) {
nodes {
createdBy
updatedBy
Expand All @@ -3656,7 +3656,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
ownerId
}
}
actionResults(first: 3) {
actionResults(first: 3, offset: 0) {
nodes {
id
createdBy
Expand All @@ -3667,7 +3667,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
ownerId
}
}
actionItems(first: 3) {
actionItems(first: 3, offset: 0) {
nodes {
id
name
Expand All @@ -3687,7 +3687,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
actionId
}
}
userActions(first: 3) {
userActions(first: 3, offset: 0) {
nodes {
id
actionStarted
Expand All @@ -3706,7 +3706,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
actionId
}
}
userActionResults(first: 3) {
userActionResults(first: 3, offset: 0) {
nodes {
id
value
Expand All @@ -3720,7 +3720,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
actionResultId
}
}
userActionItems(first: 3) {
userActionItems(first: 3, offset: 0) {
nodes {
id
value
Expand All @@ -3735,7 +3735,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
actionItemId
}
}
userPassActions(first: 3) {
userPassActions(first: 3, offset: 0) {
nodes {
id
createdBy
Expand All @@ -3746,7 +3746,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
actionId
}
}
userSavedActions(first: 3) {
userSavedActions(first: 3, offset: 0) {
nodes {
id
createdBy
Expand All @@ -3757,7 +3757,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
actionId
}
}
userViewedActions(first: 3) {
userViewedActions(first: 3, offset: 0) {
nodes {
id
createdBy
Expand All @@ -3768,7 +3768,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
actionId
}
}
userActionReactions(first: 3) {
userActionReactions(first: 3, offset: 0) {
nodes {
id
createdBy
Expand All @@ -3782,7 +3782,7 @@ exports[`GraphQL Code Generation getOne(): works with nested selection: getOne -
}
}
searchRank
goals(first: 3) {
goals(first: 3, offset: 0) {
nodes {
id
name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,74 @@ exports[`generate 1`] = `
",
}
`;

exports[`generate w count info 1`] = `
{
"getActionItemQuery": "query getActionItemQuery($id: UUID!) {
actionItem(id: $id) {
userActionItems
}
}
",
"getActionItemsPaginated": "query getActionItemsPaginated($first: Int, $last: Int, $offset: Int, $after: Cursor, $before: Cursor, $condition: ActionItemCondition, $filter: ActionItemFilter, $orderBy: [ActionItemsOrderBy!]) {
actionItems(
first: $first
last: $last
offset: $offset
after: $after
before: $before
condition: $condition
filter: $filter
orderBy: $orderBy
) {
totalCount
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
edges {
cursor
node {
userActionItems
}
}
}
}
",
"getActionItemsQuery": "query getActionItemsQuery($first: Int, $last: Int, $after: Cursor, $before: Cursor, $offset: Int, $condition: ActionItemCondition, $filter: ActionItemFilter, $orderBy: [ActionItemsOrderBy!]) {
actionItems(
first: $first
last: $last
offset: $offset
after: $after
before: $before
condition: $condition
filter: $filter
orderBy: $orderBy
) {
totalCount
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
nodes {
userActionItems
}
}
}
",
"getActionItemsQueryAll": "query getActionItemsQueryAll {
actionItems {
totalCount
nodes {
userActionItems
}
}
}
",
}
`;
28 changes: 27 additions & 1 deletion packages/launchql-gen/__tests__/granular.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { generateGranular as generate } from '../src';
import { print } from 'graphql';
import mutations from '../__fixtures__/api/mutations.json';
import queries from '../__fixtures__/api/queries.json';
// import queryNestedSelectionMany from '../__fixtures__/api/query-nested-selection-many.json';
// import queryNestedSelectionOne from '../__fixtures__/api/query-nested-selection-one.json';

it('generate', () => {
// @ts-ignore
Expand All @@ -11,7 +13,7 @@ it('generate', () => {
'name',
'approved'
]);

const output = Object.keys(gen).reduce((m, key) => {
if (gen[key]?.ast) {
// @ts-ignore
Expand All @@ -21,3 +23,27 @@ it('generate', () => {
}, {});
expect(output).toMatchSnapshot();
});

it('generate w count info', () => {
const gen = generate(
{ ...queries, ...mutations },
'ActionItem',
['userActionItems'],
// @ts-ignore
{
userActionItems: {
first: 10,
offset: 10
}
}
);

const output = Object.keys(gen).reduce((m, key) => {
if (gen[key]?.ast) {
// @ts-ignore
m[key] = print(gen[key].ast);
}
return m;
}, {});
expect(output).toMatchSnapshot();
});
31 changes: 20 additions & 11 deletions packages/launchql-gen/src/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface GetManyArgs {
operationName: string;
query: any; // You can type this more specifically if you know its structure
fields: string[];
paginated?: Record<string, { first?: number; offset?: number }>;
}

export interface GetManyResult {
Expand All @@ -97,13 +98,14 @@ export const getMany = ({
operationName,
query,
fields,
paginated
}: GetManyArgs): GetManyResult => {
const queryName = inflection.camelize(
['get', inflection.underscore(operationName), 'query', 'all'].join('_'),
true
);

const selections: FieldNode[] = getSelections(query, fields);
const selections: FieldNode[] = getSelections(query, fields, paginated);

const opSel: FieldNode[] = [
t.field({
Expand Down Expand Up @@ -137,6 +139,7 @@ export interface GetManyPaginatedEdgesArgs {
operationName: string;
query: GqlField;
fields: string[];
paginated?: Record<string, { first?: number; offset?: number }>;
}

export interface GetManyPaginatedEdgesResult {
Expand All @@ -148,6 +151,7 @@ export const getManyPaginatedEdges = ({
operationName,
query,
fields,
paginated
}: GetManyPaginatedEdgesArgs): GetManyPaginatedEdgesResult => {
const queryName = inflection.camelize(
['get', inflection.underscore(operationName), 'paginated'].join('_'),
Expand All @@ -160,7 +164,7 @@ export const getManyPaginatedEdges = ({
const Filter = `${Singular}Filter`;
const OrderBy = `${Plural}OrderBy`;

const selections: FieldNode[] = getSelections(query, fields);
const selections: FieldNode[] = getSelections(query, fields, paginated);

const variableDefinitions: VariableDefinitionNode[] = [
'first',
Expand Down Expand Up @@ -263,6 +267,7 @@ export interface GetManyPaginatedNodesArgs {
operationName: string;
query: GqlField;
fields: string[];
paginated?: Record<string, { first?: number; offset?: number }>;
}

export interface GetManyPaginatedNodesResult {
Expand All @@ -274,6 +279,7 @@ export const getManyPaginatedNodes = ({
operationName,
query,
fields,
paginated
}: GetManyPaginatedNodesArgs): GetManyPaginatedNodesResult => {
const queryName = inflection.camelize(
['get', inflection.underscore(operationName), 'query'].join('_'),
Expand All @@ -286,7 +292,7 @@ export const getManyPaginatedNodes = ({
const Filter = `${Singular}Filter`;
const OrderBy = `${Plural}OrderBy`;

const selections: FieldNode[] = getSelections(query, fields);
const selections: FieldNode[] = getSelections(query, fields, paginated);

const variableDefinitions: VariableDefinitionNode[] = [
'first',
Expand Down Expand Up @@ -491,6 +497,7 @@ export interface GetOneArgs {
operationName: string;
query: GqlField;
fields: string[];
paginated?: Record<string, { first?: number; offset?: number }>;
}

export interface GetOneResult {
Expand All @@ -502,6 +509,7 @@ export const getOne = ({
operationName,
query,
fields,
paginated
}: GetOneArgs): GetOneResult => {
const queryName = inflection.camelize(
['get', inflection.underscore(operationName), 'query'].join('_'),
Expand Down Expand Up @@ -539,7 +547,7 @@ export const getOne = ({
})
);

const selections: FieldNode[] = getSelections(query, fields);
const selections: FieldNode[] = getSelections(query, fields, paginated);

const opSel: FieldNode[] = [
t.field({
Expand Down Expand Up @@ -1096,10 +1104,10 @@ export const generateGranular = (
}, {});
};


export function getSelections(
query: GqlField,
fields: string[] = []
fields: string[] = [],
paginated: Record<string, { first?: number; offset?: number }> = {}
): FieldNode[] {
const useAll = fields.length === 0;

Expand All @@ -1119,14 +1127,15 @@ export function getSelections(
) {
if (!useAll && !fields.includes(field.name)) return null;

const pageObj = paginated[field.name] || {};
const first = pageObj.first ?? 3;
const offset = pageObj.offset ?? 0;

return t.field({
name: field.name,
args: [
t.argument({
name: 'first',
// @ts-ignore
value: t.intValue({ value: 3 }),
}),
t.argument({ name: 'first', value: t.intValue({ value: String(first) }) }),
t.argument({ name: 'offset', value: t.intValue({ value: String(offset) }) }),
],
selectionSet: t.selectionSet({
selections: [
Expand Down