Skip to content

Commit 146b1ad

Browse files
committed
Support alias selections
1 parent ddf126c commit 146b1ad

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

src/__tests__/codegenApolloMock.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,87 @@ describe("codegenApolloMock", () => {
12761276
`);
12771277
});
12781278
});
1279+
1280+
describe("with alias", () => {
1281+
let document: DocumentNode;
1282+
let output: string;
1283+
let apolloMock: ApolloMockFn;
1284+
1285+
beforeEach(async () => {
1286+
document = parse(`
1287+
query authors {
1288+
allAuthors: authors {
1289+
idField
1290+
}
1291+
}
1292+
`);
1293+
1294+
const documents = [{ document, location: "authors.gql" }];
1295+
const config = getConfig({ documents });
1296+
1297+
output = await codegen(config);
1298+
apolloMock = getApolloMock(output);
1299+
});
1300+
1301+
it("should have matching operation", () => {
1302+
expect(output).toMatchInlineSnapshot(`
1303+
"import { createApolloMock } from 'apollo-typed-documents';
1304+
1305+
const operations = {};
1306+
1307+
export default createApolloMock(operations);
1308+
1309+
operations.authors = {};
1310+
operations.authors.variables = (values = {}, options = {}) => {
1311+
const __typename = '';
1312+
values = (({ }) => ({ }))(values);
1313+
values.__typename = __typename;
1314+
return {
1315+
1316+
};
1317+
};
1318+
operations.authors.data = (values = {}, options = {}) => {
1319+
const __typename = '';
1320+
values = (({ allAuthors = null }) => ({ allAuthors }))(values);
1321+
values.__typename = __typename;
1322+
return {
1323+
allAuthors: !values.allAuthors ? values.allAuthors : values.allAuthors.map(item => ((values = {}, options = {}) => {
1324+
const __typename = 'Author';
1325+
values = (({ idField = null }) => ({ idField }))(values);
1326+
values.__typename = __typename;
1327+
return {
1328+
idField: (values.idField === null || values.idField === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'ID', mappedTypeName: 'string', fieldName: 'idField', __typename, scalarValues: options.scalarValues }) : values.idField,
1329+
...(options.addTypename ? { __typename } : {})
1330+
};
1331+
})(item, options))
1332+
};
1333+
};"
1334+
`);
1335+
});
1336+
1337+
it("should use alias for both input and output", () => {
1338+
const result = apolloMock(document, {}, { data: { allAuthors: [{}] } });
1339+
1340+
expect(result).toMatchInlineSnapshot(`
1341+
{
1342+
"request": {
1343+
"query": "...",
1344+
"variables": {}
1345+
},
1346+
"result": {
1347+
"data": {
1348+
"allAuthors": [
1349+
{
1350+
"idField": "Author-idField",
1351+
"__typename": "Author"
1352+
}
1353+
]
1354+
}
1355+
}
1356+
}
1357+
`);
1358+
});
1359+
});
12791360
});
12801361

12811362
describe("mutation", () => {

src/visitors/TypedVisitor.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,10 @@ export default class TypedVisitor {
234234
}
235235

236236
getOutputFieldForField(node: FieldNode, parentField: TypedField) {
237-
const name = node.name.value;
237+
const fieldName = node.name.value;
238+
const name = node.alias?.value || fieldName;
238239

239-
if (name === "__typename") {
240+
if (fieldName === "__typename") {
240241
return createTypedField({ name, isTypename: true });
241242
}
242243

@@ -247,11 +248,11 @@ export default class TypedVisitor {
247248
}
248249

249250
const fields = parentType.getFields();
250-
const field = fields[name];
251+
const field = fields[fieldName];
251252

252253
if (!field) {
253254
throw new Error(
254-
`Field "${name}" does not exist on type "${parentType.name}"`
255+
`Field "${fieldName}" does not exist on type "${parentType.name}"`
255256
);
256257
}
257258

0 commit comments

Comments
 (0)