Skip to content

Commit f38f9ec

Browse files
authored
Support custom scalar types (rubengrill#6)
1 parent 558a8a2 commit f38f9ec

25 files changed

+300
-133
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ $ yarn add apollo-typed-documents
3232
```yml
3333
schema: schema.graphql
3434
documents: src/**/*.graphql
35+
config:
36+
scalars:
37+
Date: string
3538
generates:
3639
./src/codegenTypedDocuments.d.ts:
3740
plugins:
@@ -80,6 +83,7 @@ Add `node_modules/apollo-typed-documents/lib/reactHooks.d.ts` in `include` to ov
8083
query authors {
8184
authors {
8285
id
86+
createdAt
8387
name
8488
description
8589
books {
@@ -99,6 +103,7 @@ query authors {
99103
mutation createAuthor($input: AuthorInput!) {
100104
createAuthor(input: $input) {
101105
id
106+
createdAt
102107
name
103108
description
104109
books {
@@ -287,6 +292,9 @@ $ yarn add apollo-typed-documents
287292
```yml
288293
schema: schema.graphql
289294
documents: src/**/*.graphql
295+
config:
296+
scalars:
297+
Date: string
290298
generates:
291299
./src/apolloMock.js:
292300
plugins:
@@ -301,8 +309,11 @@ generates:
301309
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/schema.graphql) -->
302310
<!-- The below code snippet is automatically added from ./examples/docs/schema.graphql -->
303311
```graphql
312+
scalar Date
313+
304314
type Author {
305315
id: ID!
316+
createdAt: Date!
306317
name: String!
307318
description: String
308319
books: [Book!]!
@@ -346,6 +357,7 @@ schema {
346357
query authors {
347358
authors {
348359
id
360+
createdAt
349361
name
350362
description
351363
books {
@@ -365,6 +377,7 @@ query authors {
365377
mutation createAuthor($input: AuthorInput!) {
366378
createAuthor(input: $input) {
367379
id
380+
createdAt
368381
name
369382
description
370383
books {
@@ -414,6 +427,7 @@ describe("apolloMock", () => {
414427
{
415428
__typename: "Author",
416429
id: "Author-id",
430+
createdAt: "Author-createdAt",
417431
name: "Author-name",
418432
description: null,
419433
books: [],
@@ -445,6 +459,7 @@ describe("apolloMock", () => {
445459
createAuthor: {
446460
__typename: "Author",
447461
id: "Author-id",
462+
createdAt: "Author-createdAt",
448463
name: "Foo",
449464
description: null,
450465
books: [
@@ -459,6 +474,33 @@ describe("apolloMock", () => {
459474
},
460475
});
461476
});
477+
478+
it("allows overriding default values for scalar types", () => {
479+
const scalarValues = { Date: "2020-01-01" };
480+
481+
expect(
482+
apolloMock(authors, {}, { authors: [{}] }, { scalarValues })
483+
).toEqual({
484+
request: {
485+
query: authors,
486+
variables: {},
487+
},
488+
result: {
489+
data: {
490+
authors: [
491+
{
492+
__typename: "Author",
493+
id: "Author-id",
494+
createdAt: "2020-01-01",
495+
name: "Author-name",
496+
description: null,
497+
books: [],
498+
},
499+
],
500+
},
501+
},
502+
});
503+
});
462504
});
463505
```
464506
<!-- AUTO-GENERATED-CONTENT:END -->

examples/cra-ts/codegen.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
schema: ../docs/schema.graphql
22
documents: src/**/*.graphql
3+
config:
4+
scalars:
5+
Date: string
36
generates:
47
./src/codegenTypedDocuments.d.ts:
58
plugins:

examples/cra-ts/src/apolloMock.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,36 @@ operations.authors.variables = (values = {}, options = {}) => {
1414
return {
1515

1616
};
17-
}
17+
};
1818
operations.authors.data = (values = {}, options = {}) => {
1919
const __typename = '';
2020
values = (({ authors = null }) => ({ authors }))(values);
2121
values.__typename = __typename;
2222
return {
2323
authors: (values.authors || []).map(item => ((values = {}, options = {}) => {
2424
const __typename = 'Author';
25-
values = (({ id = null, name = null, description = null, books = null }) => ({ id, name, description, books }))(values);
25+
values = (({ id = null, createdAt = null, name = null, description = null, books = null }) => ({ id, createdAt, name, description, books }))(values);
2626
values.__typename = __typename;
2727
return {
28-
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
29-
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
28+
id: (values.id === null || values.id === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'ID', mappedTypeName: 'string', fieldName: 'id', __typename, scalarValues: options.scalarValues }) : values.id,
29+
createdAt: (values.createdAt === null || values.createdAt === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'Date', mappedTypeName: 'string', fieldName: 'createdAt', __typename, scalarValues: options.scalarValues }) : values.createdAt,
30+
name: (values.name === null || values.name === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'String', mappedTypeName: 'string', fieldName: 'name', __typename, scalarValues: options.scalarValues }) : values.name,
3031
description: values.description,
3132
books: (values.books || []).map(item => ((values = {}, options = {}) => {
3233
const __typename = 'Book';
3334
values = (({ id = null, title = null }) => ({ id, title }))(values);
3435
values.__typename = __typename;
3536
return {
36-
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
37-
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title,
37+
id: (values.id === null || values.id === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'ID', mappedTypeName: 'string', fieldName: 'id', __typename, scalarValues: options.scalarValues }) : values.id,
38+
title: (values.title === null || values.title === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'String', mappedTypeName: 'string', fieldName: 'title', __typename, scalarValues: options.scalarValues }) : values.title,
3839
...(options.addTypename ? { __typename } : {})
3940
};
4041
})(item, options)),
4142
...(options.addTypename ? { __typename } : {})
4243
};
4344
})(item, options))
4445
};
45-
}
46+
};
4647

4748
operations.createAuthor = {};
4849
operations.createAuthor.variables = (values = {}, options = {}) => {
@@ -52,52 +53,53 @@ operations.createAuthor.variables = (values = {}, options = {}) => {
5253
return {
5354
input: (AuthorInput)(values.input || undefined, options)
5455
};
55-
}
56+
};
5657
operations.createAuthor.data = (values = {}, options = {}) => {
5758
const __typename = '';
5859
values = (({ createAuthor = null }) => ({ createAuthor }))(values);
5960
values.__typename = __typename;
6061
return {
6162
createAuthor: ((values = {}, options = {}) => {
6263
const __typename = 'Author';
63-
values = (({ id = null, name = null, description = null, books = null }) => ({ id, name, description, books }))(values);
64+
values = (({ id = null, createdAt = null, name = null, description = null, books = null }) => ({ id, createdAt, name, description, books }))(values);
6465
values.__typename = __typename;
6566
return {
66-
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
67-
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
67+
id: (values.id === null || values.id === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'ID', mappedTypeName: 'string', fieldName: 'id', __typename, scalarValues: options.scalarValues }) : values.id,
68+
createdAt: (values.createdAt === null || values.createdAt === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'Date', mappedTypeName: 'string', fieldName: 'createdAt', __typename, scalarValues: options.scalarValues }) : values.createdAt,
69+
name: (values.name === null || values.name === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'String', mappedTypeName: 'string', fieldName: 'name', __typename, scalarValues: options.scalarValues }) : values.name,
6870
description: values.description,
6971
books: (values.books || []).map(item => ((values = {}, options = {}) => {
7072
const __typename = 'Book';
7173
values = (({ id = null, title = null }) => ({ id, title }))(values);
7274
values.__typename = __typename;
7375
return {
74-
id: (values.id === null || values.id === undefined) ? [__typename, 'id'].filter(v => v).join('-') : values.id,
75-
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title,
76+
id: (values.id === null || values.id === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'ID', mappedTypeName: 'string', fieldName: 'id', __typename, scalarValues: options.scalarValues }) : values.id,
77+
title: (values.title === null || values.title === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'String', mappedTypeName: 'string', fieldName: 'title', __typename, scalarValues: options.scalarValues }) : values.title,
7678
...(options.addTypename ? { __typename } : {})
7779
};
7880
})(item, options)),
7981
...(options.addTypename ? { __typename } : {})
8082
};
8183
})(values.createAuthor || undefined, options)
8284
};
83-
}
85+
};
8486

8587
const BookInput = (values = {}, options = {}) => {
8688
const __typename = 'BookInput';
8789
values = (({ title = undefined }) => ({ title }))(values);
8890
values.__typename = __typename;
8991
return {
90-
title: (values.title === null || values.title === undefined) ? [__typename, 'title'].filter(v => v).join('-') : values.title
92+
title: (values.title === null || values.title === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'String', mappedTypeName: 'string', fieldName: 'title', __typename, scalarValues: options.scalarValues }) : values.title
9193
};
92-
}
94+
};
9395

9496
const AuthorInput = (values = {}, options = {}) => {
9597
const __typename = 'AuthorInput';
9698
values = (({ name = undefined, description = undefined, books = undefined }) => ({ name, description, books }))(values);
9799
values.__typename = __typename;
98100
return {
99-
name: (values.name === null || values.name === undefined) ? [__typename, 'name'].filter(v => v).join('-') : values.name,
101+
name: (values.name === null || values.name === undefined) ? options.getDefaultScalarValue({ scalarTypeName: 'String', mappedTypeName: 'string', fieldName: 'name', __typename, scalarValues: options.scalarValues }) : values.name,
100102
description: values.description,
101103
books: (values.books || []).map(item => (BookInput)(item, options))
102104
};
103-
}
105+
};

examples/cra-ts/src/authors.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
query authors {
22
authors {
33
id
4+
createdAt
45
name
56
description
67
books {

examples/cra-ts/src/codegenTypes.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ export type Scalars = {
99
Boolean: boolean;
1010
Int: number;
1111
Float: number;
12+
Date: string;
1213
};
1314

1415
export type Author = {
1516
__typename?: 'Author';
1617
id: Scalars['ID'];
18+
createdAt: Scalars['Date'];
1719
name: Scalars['String'];
1820
description?: Maybe<Scalars['String']>;
1921
books: Array<Book>;
@@ -35,6 +37,7 @@ export type BookInput = {
3537
title: Scalars['String'];
3638
};
3739

40+
3841
export type Mutation = {
3942
__typename?: 'Mutation';
4043
createAuthor: Author;
@@ -57,7 +60,7 @@ export type AuthorsQuery = (
5760
{ __typename?: 'Query' }
5861
& { authors: Array<(
5962
{ __typename?: 'Author' }
60-
& Pick<Author, 'id' | 'name' | 'description'>
63+
& Pick<Author, 'id' | 'createdAt' | 'name' | 'description'>
6164
& { books: Array<(
6265
{ __typename?: 'Book' }
6366
& Pick<Book, 'id' | 'title'>
@@ -74,7 +77,7 @@ export type CreateAuthorMutation = (
7477
{ __typename?: 'Mutation' }
7578
& { createAuthor: (
7679
{ __typename?: 'Author' }
77-
& Pick<Author, 'id' | 'name' | 'description'>
80+
& Pick<Author, 'id' | 'createdAt' | 'name' | 'description'>
7881
& { books: Array<(
7982
{ __typename?: 'Book' }
8083
& Pick<Book, 'id' | 'title'>

examples/cra-ts/src/createAuthor.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mutation createAuthor($input: AuthorInput!) {
22
createAuthor(input: $input) {
33
id
4+
createdAt
45
name
56
description
67
books {

examples/cra/codegen.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
schema: ../docs/schema.graphql
22
documents: src/**/*.graphql
3+
config:
4+
scalars:
5+
Date: string
36
generates:
47
./src/codegenTypedDocuments.d.ts:
58
plugins:

0 commit comments

Comments
 (0)