diff --git a/TODO b/TODO index ce7804d82..f26c75773 100644 --- a/TODO +++ b/TODO @@ -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 ---- diff --git a/packages/launchql-gen/__tests__/__snapshots__/gql.test.ts.snap b/packages/launchql-gen/__tests__/__snapshots__/gql.test.ts.snap index b431e14b7..4c243b701 100644 --- a/packages/launchql-gen/__tests__/__snapshots__/gql.test.ts.snap +++ b/packages/launchql-gen/__tests__/__snapshots__/gql.test.ts.snap @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/packages/launchql-gen/__tests__/__snapshots__/granular.test.ts.snap b/packages/launchql-gen/__tests__/__snapshots__/granular.test.ts.snap index 15e095a22..ef1c1e3fc 100644 --- a/packages/launchql-gen/__tests__/__snapshots__/granular.test.ts.snap +++ b/packages/launchql-gen/__tests__/__snapshots__/granular.test.ts.snap @@ -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 + } + } +} +", +} +`; diff --git a/packages/launchql-gen/__tests__/granular.test.ts b/packages/launchql-gen/__tests__/granular.test.ts index 8397127cc..6d04251ec 100644 --- a/packages/launchql-gen/__tests__/granular.test.ts +++ b/packages/launchql-gen/__tests__/granular.test.ts @@ -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 @@ -11,7 +13,7 @@ it('generate', () => { 'name', 'approved' ]); - + const output = Object.keys(gen).reduce((m, key) => { if (gen[key]?.ast) { // @ts-ignore @@ -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(); +}); \ No newline at end of file diff --git a/packages/launchql-gen/src/gql.ts b/packages/launchql-gen/src/gql.ts index cc59aaf7b..f60b3a90a 100644 --- a/packages/launchql-gen/src/gql.ts +++ b/packages/launchql-gen/src/gql.ts @@ -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; } export interface GetManyResult { @@ -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({ @@ -137,6 +139,7 @@ export interface GetManyPaginatedEdgesArgs { operationName: string; query: GqlField; fields: string[]; + paginated?: Record; } export interface GetManyPaginatedEdgesResult { @@ -148,6 +151,7 @@ export const getManyPaginatedEdges = ({ operationName, query, fields, + paginated }: GetManyPaginatedEdgesArgs): GetManyPaginatedEdgesResult => { const queryName = inflection.camelize( ['get', inflection.underscore(operationName), 'paginated'].join('_'), @@ -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', @@ -263,6 +267,7 @@ export interface GetManyPaginatedNodesArgs { operationName: string; query: GqlField; fields: string[]; + paginated?: Record; } export interface GetManyPaginatedNodesResult { @@ -274,6 +279,7 @@ export const getManyPaginatedNodes = ({ operationName, query, fields, + paginated }: GetManyPaginatedNodesArgs): GetManyPaginatedNodesResult => { const queryName = inflection.camelize( ['get', inflection.underscore(operationName), 'query'].join('_'), @@ -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', @@ -491,6 +497,7 @@ export interface GetOneArgs { operationName: string; query: GqlField; fields: string[]; + paginated?: Record; } export interface GetOneResult { @@ -502,6 +509,7 @@ export const getOne = ({ operationName, query, fields, + paginated }: GetOneArgs): GetOneResult => { const queryName = inflection.camelize( ['get', inflection.underscore(operationName), 'query'].join('_'), @@ -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({ @@ -1096,10 +1104,10 @@ export const generateGranular = ( }, {}); }; - export function getSelections( query: GqlField, - fields: string[] = [] + fields: string[] = [], + paginated: Record = {} ): FieldNode[] { const useAll = fields.length === 0; @@ -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: [