Skip to content
Merged
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: 2 additions & 0 deletions graphql/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { generateKeyedObjFromIntrospection } from '../test-utils/generate-from-i

const sql = (f: string) => join(__dirname, '../sql', f);

jest.setTimeout(30000);

let teardown: () => Promise<void>;
let query: GraphQLQueryFn;
let introspection: IntrospectionQueryResult;
Expand Down
24 changes: 24 additions & 0 deletions graphql/codegen/__tests__/gql.mutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,28 @@ describe('gql mutation builders', () => {
expect(s).toContain('$tags: [String]!')
expect(s).toContain('clientMutationId')
})

it('createOne: handles Regimen singularization and input key', () => {
const mutation: MutationSpec = {
model: 'Regimens',
properties: {
input: {
properties: {
regimen: {
properties: {
name: { type: 'String', isNotNull: true }
}
}
}
}
}
}

const res = createOne({ operationName: 'createRegimen', mutation })
expect(res).toBeDefined()
const s = print(res!.ast)
expect(s).toContain('mutation createRegimenMutation')
expect(s).toContain('$name: String!')
expect(s).toContain('regimen')
})
})
5 changes: 2 additions & 3 deletions graphql/codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"devDependencies": {
"@constructive-io/graphql-test": "workspace:^",
"@types/babel__generator": "^7.27.0",
"@types/pluralize": "0.0.33",
"@types/inflection": "^2.0.0",
"makage": "^0.1.9"
},
"dependencies": {
Expand All @@ -48,8 +48,7 @@
"graphql-request": "6.1.0",
"graphql-tag": "2.12.6",
"inflection": "^1.12.0",
"introspectron": "workspace:^",
"pluralize": "^8.0.0"
"introspectron": "workspace:^"
},
"keywords": [
"graphql",
Expand Down
6 changes: 3 additions & 3 deletions graphql/codegen/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ export async function runCodegen(opts: GraphQLCodegenOptions, cwd: string) {
filename: sdkFile,
schema: schema as any,
documents,
config: { scalars: options.scalars || {} },
config: { scalars: options.scalars || {}, dedupeOperationSuffix: true },
plugins: [
{ typescript: {} },
{ 'typescript-operations': {} },
{ 'typescript-graphql-request': {} }
{ 'typescript-graphql-request': { dedupeOperationSuffix: true } }
],
pluginMap: {
typescript: typescriptPlugin as any,
Expand Down Expand Up @@ -219,7 +219,7 @@ export async function runCodegen(opts: GraphQLCodegenOptions, cwd: string) {
filename: reactQueryFile,
schema: schema as any,
documents,
config: rqConfig,
config: { ...rqConfig, dedupeOperationSuffix: true },
plugins: [
{ typescript: {} },
{ 'typescript-operations': {} },
Expand Down
48 changes: 25 additions & 23 deletions graphql/codegen/src/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import {
TypeNode,
VariableDefinitionNode,
} from 'graphql';
// @ts-ignore
import inflection from 'inflection';
import plz from 'pluralize';
import inflection from 'inflection'

const NON_MUTABLE_PROPS = [
'id',
Expand All @@ -20,7 +18,7 @@ const NON_MUTABLE_PROPS = [
'updatedBy',
];

const objectToArray = (obj: Record<string, any>): { name: string; [key: string]: any }[] =>
const objectToArray = (obj: Record<string, any>): { name: string;[key: string]: any }[] =>
Object.keys(obj).map((k) => ({ name: k, ...obj[k] }));

type TypeIndex = { byName: Record<string, any>; getInputFieldType: (typeName: string, fieldName: string) => any };
Expand Down Expand Up @@ -70,6 +68,10 @@ function extractNamedTypeName(node: any): string | null {
return n && n.name ? n.name : null;
}

function singularModel(name: string): string {
return inflection.singularize(name);
}

interface CreateGqlMutationArgs {
operationName: string;
mutationName: string;
Expand Down Expand Up @@ -650,7 +652,7 @@ export const createOne = ({
}

const modelName = inflection.camelize(
[plz.singular(mutation.model)].join('_'),
[singularModel(mutation.model)].join('_'),
true
);

Expand Down Expand Up @@ -736,9 +738,9 @@ export const createOne = ({

const nested: FieldNode[] = (finalFields.length > 0)
? [t.field({
name: modelName,
selectionSet: t.selectionSet({ selections: finalFields.map((f) => t.field({ name: f })) }),
})]
name: modelName,
selectionSet: t.selectionSet({ selections: finalFields.map((f) => t.field({ name: f })) }),
})]
: [];

const ast: DocumentNode = createGqlMutation({
Expand Down Expand Up @@ -800,7 +802,7 @@ export const patchOne = ({
}

const modelName = inflection.camelize(
[plz.singular(mutation.model)].join('_'),
[singularModel(mutation.model)].join('_'),
true
);

Expand All @@ -821,7 +823,7 @@ export const patchOne = ({
const patchers = patchByAttrs.map((p) => p.name);

const useCollapsedOpt = selection?.mutationInputMode === 'patchCollapsed';
const ModelPascal = inflection.camelize(plz.singular(mutation.model), false);
const ModelPascal = inflection.camelize(singularModel(mutation.model), false);
const patchTypeName = `${ModelPascal}Patch`;
const inputTypeName = resolveTypeName('input', (mutation.properties as any)?.input?.type || (mutation.properties as any)?.input, typeNameOverrides);
let unresolved = 0;
Expand Down Expand Up @@ -914,9 +916,9 @@ export const patchOne = ({

const nestedPatch: FieldNode[] = (idSelection.length > 0)
? [t.field({
name: modelName,
selectionSet: t.selectionSet({ selections: idSelection.map((f) => t.field({ name: f })) }),
})]
name: modelName,
selectionSet: t.selectionSet({ selections: idSelection.map((f) => t.field({ name: f })) }),
})]
: [];

const ast: DocumentNode = createGqlMutation({
Expand Down Expand Up @@ -959,7 +961,7 @@ export const deleteOne = ({
}

const modelName = inflection.camelize(
[plz.singular(mutation.model)].join('_'),
[singularModel(mutation.model)].join('_'),
true
);

Expand Down Expand Up @@ -1092,8 +1094,8 @@ export const createMutation = ({
value: mustUseRaw
? (t.variable({ name: 'input' }) as any)
: t.objectValue({
fields: otherAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
}),
fields: otherAttrs.map((f) => t.objectField({ name: f.name, value: t.variable({ name: f.name }) })),
}),
}),
];

Expand Down Expand Up @@ -1123,13 +1125,13 @@ export const createMutation = ({
const modelType = typeIndex && modelTypeName ? (typeIndex as any).byName?.[modelTypeName] : null;
const fieldNames: string[] = (modelType && Array.isArray(modelType.fields))
? modelType.fields
.filter((f: any) => {
let r = f.type;
while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST')) r = r.ofType;
const kind = r?.kind;
return kind === 'SCALAR' || kind === 'ENUM';
})
.map((f: any) => f.name)
.filter((f: any) => {
let r = f.type;
while (r && (r.kind === 'NON_NULL' || r.kind === 'LIST')) r = r.ofType;
const kind = r?.kind;
return kind === 'SCALAR' || kind === 'ENUM';
})
.map((f: any) => f.name)
: [];
selections.push(
t.field({
Expand Down
3 changes: 1 addition & 2 deletions graphql/query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"ajv": "^7.0.4",
"gql-ast": "workspace:^",
"graphql": "15.10.1",
"inflection": "1.12.0",
"pluralize": "8.0.0"
"inflection": "1.12.0"
},
"keywords": [
"query",
Expand Down
7 changes: 3 additions & 4 deletions graphql/query/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import * as t from 'gql-ast';
import inflection from 'inflection';
import plz from 'pluralize';

import { getCustomAst, isIntervalType } from './custom-ast';

Expand Down Expand Up @@ -353,7 +352,7 @@ export const createOne = ({
}

const modelName = inflection.camelize(
[plz.singular(mutation.model)].join('_'),
[inflection.singularize(mutation.model)].join('_'),
true
);

Expand Down Expand Up @@ -418,7 +417,7 @@ export const patchOne = ({
}

const modelName = inflection.camelize(
[plz.singular(mutation.model)].join('_'),
[inflection.singularize(mutation.model)].join('_'),
true
);

Expand Down Expand Up @@ -492,7 +491,7 @@ export const deleteOne = ({ mutationName, operationName, mutation }) => {
}

const modelName = inflection.camelize(
[plz.singular(mutation.model)].join('_'),
[inflection.singularize(mutation.model)].join('_'),
true
);

Expand Down
Loading