Skip to content

Commit 04466fc

Browse files
Merge pull request #4 from danieljharvey/oh-shit
Totally fucking smashed it
2 parents d9681a3 + 84258c6 commit 04466fc

File tree

14 files changed

+8567
-16
lines changed

14 files changed

+8567
-16
lines changed

output/github/.gitkeep

Whitespace-only changes.

output/github/output.ts

Lines changed: 8427 additions & 0 deletions
Large diffs are not rendered by default.

output/twitter/.gitkeep

Whitespace-only changes.

output/twitter/output.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import * as fc from "fast-check";
2+
export const getArbitraryQuery = (): fc.Arbitrary<any> =>
3+
fc.record({
4+
Tweet: getArbitraryTweet(),
5+
Tweets: fc.array(getArbitraryTweet()),
6+
TweetsMeta: getArbitraryMeta(),
7+
User: getArbitraryUser(),
8+
Notifications: fc.array(getArbitraryNotification()),
9+
NotificationsMeta: getArbitraryMeta(),
10+
});
11+
12+
export const getArbitraryID = (): fc.Arbitrary<any> => fc.string();
13+
14+
export const getArbitraryTweet = (): fc.Arbitrary<any> =>
15+
fc.record({
16+
id: getArbitraryID(),
17+
body: getArbitraryString(),
18+
date: getArbitraryDate(),
19+
Author: getArbitraryUser(),
20+
Stats: getArbitraryStat(),
21+
});
22+
23+
export const getArbitraryString = (): fc.Arbitrary<any> => fc.string();
24+
25+
export const getArbitraryDate = (): fc.Arbitrary<any> => fc.string();
26+
27+
export const getArbitraryUser = (): fc.Arbitrary<any> =>
28+
fc.record({
29+
id: getArbitraryID(),
30+
username: getArbitraryString(),
31+
first_name: getArbitraryString(),
32+
last_name: getArbitraryString(),
33+
full_name: getArbitraryString(),
34+
name: getArbitraryString(),
35+
avatar_url: getArbitraryUrl(),
36+
});
37+
38+
export const getArbitraryUrl = (): fc.Arbitrary<any> => fc.string();
39+
40+
export const getArbitraryStat = (): fc.Arbitrary<any> =>
41+
fc.record({
42+
views: getArbitraryInt(),
43+
likes: getArbitraryInt(),
44+
retweets: getArbitraryInt(),
45+
responses: getArbitraryInt(),
46+
});
47+
48+
export const getArbitraryInt = (): fc.Arbitrary<any> => fc.integer();
49+
50+
export const getArbitraryMeta = (): fc.Arbitrary<any> =>
51+
fc.record({ count: getArbitraryInt() });
52+
53+
export const getArbitraryNotification = (): fc.Arbitrary<any> =>
54+
fc.record({
55+
id: getArbitraryID(),
56+
date: getArbitraryDate(),
57+
type: getArbitraryString(),
58+
});
59+
60+
export const getArbitraryMutation = (): fc.Arbitrary<any> =>
61+
fc.record({
62+
createTweet: getArbitraryTweet(),
63+
deleteTweet: getArbitraryTweet(),
64+
markTweetRead: getArbitraryBoolean(),
65+
});
66+
67+
export const getArbitraryBoolean = (): fc.Arbitrary<any> => fc.boolean();
68+
69+
export const getArbitraryActionExecutionCapabilitySetting = (): fc.Arbitrary<
70+
any
71+
> =>
72+
fc.oneof(
73+
fc.constant("ALL_ACTIONS"),
74+
fc.constant("DISABLED"),
75+
fc.constant("LOCAL_ACTIONS_ONLY"),
76+
fc.constant("NO_POLICY")
77+
);
78+
79+
export const getArbitraryAddCommentInput = (): fc.Arbitrary<any> => fc.string();
80+
81+
export const getArbitraryStatOrNotification = (): fc.Arbitrary<any> =>
82+
fc.oneof(getArbitraryStat(), getArbitraryNotification());

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"babel-jest": "^25.2.6",
2929
"graphql": "^14.6.0",
3030
"jest": "^25.2.4",
31+
"ts-node": "^8.8.1",
3132
"typescript": "^3.8.3"
3233
},
3334
"peerDependencies": {

src/convert.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ type FieldReturn = {
3030

3131
const withNamedFieldType = (namedTypeNode: NamedTypeNode): FieldReturn => {
3232
const name = namedTypeNode.name.value as TypeName;
33-
return { output: getArbitraryName(name), deps: [name] };
33+
const output = `${getArbitraryName(name)}()` as Generated;
34+
return { output, deps: [name] };
3435
};
3536

3637
const withFieldType = (typeNode: TypeNode): FieldReturn => {
@@ -86,7 +87,7 @@ const withAstNode = (name: TypeName, node: TypeDefinitionNode): Output => {
8687
return {
8788
kind: "Scalar",
8889
name,
89-
output: `fc.anything()` as Generated,
90+
output: `fc.string()` as Generated,
9091
deps: []
9192
};
9293
case "EnumTypeDefinition":
@@ -101,7 +102,7 @@ const withAstNode = (name: TypeName, node: TypeDefinitionNode): Output => {
101102
return {
102103
kind: "InputObject",
103104
name,
104-
output: "fc.anything()" as Generated,
105+
output: "fc.string()" as Generated,
105106
deps: []
106107
};
107108
case "InterfaceTypeDefinition":
@@ -153,7 +154,7 @@ const withPrimitive = (node: GraphQLNamedType): Generated | null => {
153154
};
154155

155156
const getArbitraryName = (typeName: TypeName): Generated =>
156-
`arbitrary${typeName}` as Generated;
157+
`getArbitrary${typeName}` as Generated;
157158

158159
const getNamedTypes = (schema: GraphQLSchema): GraphQLNamedType[] => {
159160
const typesMap = schema.getTypeMap();
@@ -192,14 +193,18 @@ const removeKind = (k: Kind) => (a: Output) => a.kind !== k;
192193

193194
const render = (val: Output) => {
194195
const { name, output } = val;
195-
return `export const ${getArbitraryName(name)} = ${output}`;
196+
return `export const ${getArbitraryName(
197+
name
198+
)} = (): fc.Arbitrary<any> => ${output}`;
196199
};
197200

198201
export const getSchemaDeclarations = (schema: GraphQLSchema): string =>
199-
sortASTs(getNamedTypes(schema).map(withNamedType).filter(notNull))
202+
sortASTs2(getNamedTypes(schema).map(withNamedType).filter(notNull))
200203
.map(render)
201204
.join("\n\n");
202205

206+
const sortASTs2 = <A>(a: A): A => a;
207+
203208
const filterSplit = <A>(
204209
as: A[],
205210
f: (a: A) => boolean

test.sh

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
# die if anything goes wrong
44
set -euxo pipefail
55

6-
# remove old output so it doesn't get typechecked
7-
rm ./output/output.ts || true
6+
# replace twitter output with something that typechecks
7+
echo "export const a = 1" | tee ./output/twitter/output.ts
8+
9+
# replace github output with something that typechecks
10+
echo "export const a = 1" | tee ./output/github/output.ts
11+
12+
# install shit
13+
yarn install
814

915
# run tests
1016
yarn test
@@ -13,7 +19,16 @@ yarn test
1319
yarn build
1420

1521
# run codegen basic
16-
yarn graphql-codegen --config ./test/codegen.yml
22+
yarn graphql-codegen --config ./test/twitter/codegen.yml
1723

1824
# typecheck result
19-
yarn tsc --noEmit ./output/output.ts
25+
yarn tsc --noEmit ./output/twitter/output.ts
26+
27+
# run codegen advanced
28+
yarn graphql-codegen --config ./test/github/codegen.yml
29+
30+
# typecheck advanced result
31+
yarn tsc --noEmit ./output/github/output.ts
32+
33+
# output some shit (only from the basic one, the advanced one blows the stack)
34+
yarn ts-node ./test/test-output.ts

test/codegen.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/github/codegen.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
schema: ./test/github/github-schema.graphql
2+
# documents: './src/**/*.graphql'
3+
generates:
4+
./output/github/output.ts:
5+
- ./dist/src/index.js
File renamed without changes.

0 commit comments

Comments
 (0)