Skip to content

Commit 396d166

Browse files
authored
Support interfaces (rubengrill#3)
1 parent 5f147de commit 396d166

File tree

10 files changed

+360
-149
lines changed

10 files changed

+360
-149
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
examples/**/apolloMock.js
22
examples/**/codegenTypedDocuments.d.ts
3-
examples/**/codegenTypes.d.ts
3+
examples/**/codegenTypes.d.ts
4+
node_modules

examples/cra-ts/src/apolloMock.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,92 @@
11
/* eslint-disable */
22

3-
import { createApolloMock } from "apollo-typed-documents";
3+
import { createApolloMock } from 'apollo-typed-documents';
44

55
const operations = {};
66

77
export default createApolloMock(operations);
88

99
operations.authors = {};
1010
operations.authors.variables = (values = {}, options = {}) => {
11+
const __typename = '';
1112
values = (({ }) => ({ }))(values);
1213
return {
1314

1415
};
1516
}
1617
operations.authors.data = (values = {}, options = {}) => {
18+
const __typename = '';
1719
values = (({ authors = null }) => ({ authors }))(values);
1820
return {
1921
authors: (values.authors || []).map(item => ((values = {}, options = {}) => {
22+
const __typename = 'Author';
2023
values = (({ id = null, name = null, description = null, books = null }) => ({ id, name, description, books }))(values);
2124
return {
22-
id: (values.id === null || values.id === undefined) ? "Author-id" : values.id,
23-
name: (values.name === null || values.name === undefined) ? "Author-name" : values.name,
25+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
26+
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
2427
description: values.description,
2528
books: (values.books || []).map(item => ((values = {}, options = {}) => {
29+
const __typename = 'Book';
2630
values = (({ id = null, title = null }) => ({ id, title }))(values);
2731
return {
28-
id: (values.id === null || values.id === undefined) ? "Book-id" : values.id,
29-
title: (values.title === null || values.title === undefined) ? "Book-title" : values.title,
30-
...(options.addTypename ? {__typename: "Book"} : {})
32+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
33+
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title,
34+
...(options.addTypename ? { __typename } : {})
3135
};
3236
})(item, options)),
33-
...(options.addTypename ? {__typename: "Author"} : {})
37+
...(options.addTypename ? { __typename } : {})
3438
};
3539
})(item, options))
3640
};
3741
}
3842

3943
operations.createAuthor = {};
4044
operations.createAuthor.variables = (values = {}, options = {}) => {
45+
const __typename = '';
4146
values = (({ input = undefined }) => ({ input }))(values);
4247
return {
4348
input: (AuthorInput)(values.input || undefined, options)
4449
};
4550
}
4651
operations.createAuthor.data = (values = {}, options = {}) => {
52+
const __typename = '';
4753
values = (({ createAuthor = null }) => ({ createAuthor }))(values);
4854
return {
4955
createAuthor: ((values = {}, options = {}) => {
56+
const __typename = 'Author';
5057
values = (({ id = null, name = null, description = null, books = null }) => ({ id, name, description, books }))(values);
5158
return {
52-
id: (values.id === null || values.id === undefined) ? "Author-id" : values.id,
53-
name: (values.name === null || values.name === undefined) ? "Author-name" : values.name,
59+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
60+
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
5461
description: values.description,
5562
books: (values.books || []).map(item => ((values = {}, options = {}) => {
63+
const __typename = 'Book';
5664
values = (({ id = null, title = null }) => ({ id, title }))(values);
5765
return {
58-
id: (values.id === null || values.id === undefined) ? "Book-id" : values.id,
59-
title: (values.title === null || values.title === undefined) ? "Book-title" : values.title,
60-
...(options.addTypename ? {__typename: "Book"} : {})
66+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
67+
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title,
68+
...(options.addTypename ? { __typename } : {})
6169
};
6270
})(item, options)),
63-
...(options.addTypename ? {__typename: "Author"} : {})
71+
...(options.addTypename ? { __typename } : {})
6472
};
6573
})(values.createAuthor || undefined, options)
6674
};
6775
}
6876

6977
const BookInput = (values = {}, options = {}) => {
78+
const __typename = 'BookInput';
7079
values = (({ title = undefined }) => ({ title }))(values);
7180
return {
72-
title: (values.title === null || values.title === undefined) ? "BookInput-title" : values.title
81+
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title
7382
};
7483
}
7584

7685
const AuthorInput = (values = {}, options = {}) => {
86+
const __typename = 'AuthorInput';
7787
values = (({ name = undefined, description = undefined, books = undefined }) => ({ name, description, books }))(values);
7888
return {
79-
name: (values.name === null || values.name === undefined) ? "AuthorInput-name" : values.name,
89+
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
8090
description: values.description,
8191
books: (values.books || []).map(item => (BookInput)(item, options))
8292
};

examples/cra/src/apolloMock.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,92 @@
11
/* eslint-disable */
22

3-
import { createApolloMock } from "apollo-typed-documents";
3+
import { createApolloMock } from 'apollo-typed-documents';
44

55
const operations = {};
66

77
export default createApolloMock(operations);
88

99
operations.authors = {};
1010
operations.authors.variables = (values = {}, options = {}) => {
11+
const __typename = '';
1112
values = (({ }) => ({ }))(values);
1213
return {
1314

1415
};
1516
}
1617
operations.authors.data = (values = {}, options = {}) => {
18+
const __typename = '';
1719
values = (({ authors = null }) => ({ authors }))(values);
1820
return {
1921
authors: (values.authors || []).map(item => ((values = {}, options = {}) => {
22+
const __typename = 'Author';
2023
values = (({ id = null, name = null, description = null, books = null }) => ({ id, name, description, books }))(values);
2124
return {
22-
id: (values.id === null || values.id === undefined) ? "Author-id" : values.id,
23-
name: (values.name === null || values.name === undefined) ? "Author-name" : values.name,
25+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
26+
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
2427
description: values.description,
2528
books: (values.books || []).map(item => ((values = {}, options = {}) => {
29+
const __typename = 'Book';
2630
values = (({ id = null, title = null }) => ({ id, title }))(values);
2731
return {
28-
id: (values.id === null || values.id === undefined) ? "Book-id" : values.id,
29-
title: (values.title === null || values.title === undefined) ? "Book-title" : values.title,
30-
...(options.addTypename ? {__typename: "Book"} : {})
32+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
33+
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title,
34+
...(options.addTypename ? { __typename } : {})
3135
};
3236
})(item, options)),
33-
...(options.addTypename ? {__typename: "Author"} : {})
37+
...(options.addTypename ? { __typename } : {})
3438
};
3539
})(item, options))
3640
};
3741
}
3842

3943
operations.createAuthor = {};
4044
operations.createAuthor.variables = (values = {}, options = {}) => {
45+
const __typename = '';
4146
values = (({ input = undefined }) => ({ input }))(values);
4247
return {
4348
input: (AuthorInput)(values.input || undefined, options)
4449
};
4550
}
4651
operations.createAuthor.data = (values = {}, options = {}) => {
52+
const __typename = '';
4753
values = (({ createAuthor = null }) => ({ createAuthor }))(values);
4854
return {
4955
createAuthor: ((values = {}, options = {}) => {
56+
const __typename = 'Author';
5057
values = (({ id = null, name = null, description = null, books = null }) => ({ id, name, description, books }))(values);
5158
return {
52-
id: (values.id === null || values.id === undefined) ? "Author-id" : values.id,
53-
name: (values.name === null || values.name === undefined) ? "Author-name" : values.name,
59+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
60+
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
5461
description: values.description,
5562
books: (values.books || []).map(item => ((values = {}, options = {}) => {
63+
const __typename = 'Book';
5664
values = (({ id = null, title = null }) => ({ id, title }))(values);
5765
return {
58-
id: (values.id === null || values.id === undefined) ? "Book-id" : values.id,
59-
title: (values.title === null || values.title === undefined) ? "Book-title" : values.title,
60-
...(options.addTypename ? {__typename: "Book"} : {})
66+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
67+
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title,
68+
...(options.addTypename ? { __typename } : {})
6169
};
6270
})(item, options)),
63-
...(options.addTypename ? {__typename: "Author"} : {})
71+
...(options.addTypename ? { __typename } : {})
6472
};
6573
})(values.createAuthor || undefined, options)
6674
};
6775
}
6876

6977
const BookInput = (values = {}, options = {}) => {
78+
const __typename = 'BookInput';
7079
values = (({ title = undefined }) => ({ title }))(values);
7180
return {
72-
title: (values.title === null || values.title === undefined) ? "BookInput-title" : values.title
81+
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title
7382
};
7483
}
7584

7685
const AuthorInput = (values = {}, options = {}) => {
86+
const __typename = 'AuthorInput';
7787
values = (({ name = undefined, description = undefined, books = undefined }) => ({ name, description, books }))(values);
7888
return {
79-
name: (values.name === null || values.name === undefined) ? "AuthorInput-name" : values.name,
89+
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
8090
description: values.description,
8191
books: (values.books || []).map(item => (BookInput)(item, options))
8292
};

examples/docs/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"private": true,
3+
"scripts": {
4+
"generate": "../../node_modules/@graphql-codegen/cli/bin.js --config codegenApolloMock.yml && ../../node_modules/@graphql-codegen/cli/bin.js --config codegenTypedDocuments.yml"
5+
},
26
"dependencies": {
37
"apollo-typed-documents": "link:../.."
48
}

examples/docs/src/apolloMock.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,90 @@
1-
import { createApolloMock } from "apollo-typed-documents";
1+
import { createApolloMock } from 'apollo-typed-documents';
22

33
const operations = {};
44

55
export default createApolloMock(operations);
66

77
operations.authors = {};
88
operations.authors.variables = (values = {}, options = {}) => {
9+
const __typename = '';
910
values = (({ }) => ({ }))(values);
1011
return {
1112

1213
};
1314
}
1415
operations.authors.data = (values = {}, options = {}) => {
16+
const __typename = '';
1517
values = (({ authors = null }) => ({ authors }))(values);
1618
return {
1719
authors: (values.authors || []).map(item => ((values = {}, options = {}) => {
20+
const __typename = 'Author';
1821
values = (({ id = null, name = null, description = null, books = null }) => ({ id, name, description, books }))(values);
1922
return {
20-
id: (values.id === null || values.id === undefined) ? "Author-id" : values.id,
21-
name: (values.name === null || values.name === undefined) ? "Author-name" : values.name,
23+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
24+
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
2225
description: values.description,
2326
books: (values.books || []).map(item => ((values = {}, options = {}) => {
27+
const __typename = 'Book';
2428
values = (({ id = null, title = null }) => ({ id, title }))(values);
2529
return {
26-
id: (values.id === null || values.id === undefined) ? "Book-id" : values.id,
27-
title: (values.title === null || values.title === undefined) ? "Book-title" : values.title,
28-
...(options.addTypename ? {__typename: "Book"} : {})
30+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
31+
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title,
32+
...(options.addTypename ? { __typename } : {})
2933
};
3034
})(item, options)),
31-
...(options.addTypename ? {__typename: "Author"} : {})
35+
...(options.addTypename ? { __typename } : {})
3236
};
3337
})(item, options))
3438
};
3539
}
3640

3741
operations.createAuthor = {};
3842
operations.createAuthor.variables = (values = {}, options = {}) => {
43+
const __typename = '';
3944
values = (({ input = undefined }) => ({ input }))(values);
4045
return {
4146
input: (AuthorInput)(values.input || undefined, options)
4247
};
4348
}
4449
operations.createAuthor.data = (values = {}, options = {}) => {
50+
const __typename = '';
4551
values = (({ createAuthor = null }) => ({ createAuthor }))(values);
4652
return {
4753
createAuthor: ((values = {}, options = {}) => {
54+
const __typename = 'Author';
4855
values = (({ id = null, name = null, description = null, books = null }) => ({ id, name, description, books }))(values);
4956
return {
50-
id: (values.id === null || values.id === undefined) ? "Author-id" : values.id,
51-
name: (values.name === null || values.name === undefined) ? "Author-name" : values.name,
57+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
58+
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
5259
description: values.description,
5360
books: (values.books || []).map(item => ((values = {}, options = {}) => {
61+
const __typename = 'Book';
5462
values = (({ id = null, title = null }) => ({ id, title }))(values);
5563
return {
56-
id: (values.id === null || values.id === undefined) ? "Book-id" : values.id,
57-
title: (values.title === null || values.title === undefined) ? "Book-title" : values.title,
58-
...(options.addTypename ? {__typename: "Book"} : {})
64+
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
65+
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title,
66+
...(options.addTypename ? { __typename } : {})
5967
};
6068
})(item, options)),
61-
...(options.addTypename ? {__typename: "Author"} : {})
69+
...(options.addTypename ? { __typename } : {})
6270
};
6371
})(values.createAuthor || undefined, options)
6472
};
6573
}
6674

6775
const BookInput = (values = {}, options = {}) => {
76+
const __typename = 'BookInput';
6877
values = (({ title = undefined }) => ({ title }))(values);
6978
return {
70-
title: (values.title === null || values.title === undefined) ? "BookInput-title" : values.title
79+
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title
7180
};
7281
}
7382

7483
const AuthorInput = (values = {}, options = {}) => {
84+
const __typename = 'AuthorInput';
7585
values = (({ name = undefined, description = undefined, books = undefined }) => ({ name, description, books }))(values);
7686
return {
77-
name: (values.name === null || values.name === undefined) ? "AuthorInput-name" : values.name,
87+
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
7888
description: values.description,
7989
books: (values.books || []).map(item => (BookInput)(item, options))
8090
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"postversion": "git push && git push --tags",
2626
"test": "jest",
2727
"lint": "eslint --ext .js,.ts src/ examples/docs/",
28-
"docs": "yarn run build && (cd examples/docs && graphql-codegen --config codegenApolloMock.yml && graphql-codegen --config codegenTypedDocuments.yml) && md-magic --path README.md"
28+
"generate": "yarn run build && (cd examples/cra && yarn run generate) && (cd examples/cra-ts && yarn run generate) && (cd examples/docs && yarn run generate)",
29+
"docs": "yarn run generate && md-magic --path README.md"
2930
},
3031
"peerDependencies": {
3132
"graphql": "^14.6.0",

0 commit comments

Comments
 (0)