Skip to content

Commit

Permalink
Merge pull request #4 from danieljharvey/oh-shit
Browse files Browse the repository at this point in the history
Totally fucking smashed it
  • Loading branch information
danieljharvey committed Apr 5, 2020
2 parents d9681a3 + 84258c6 commit 04466fc
Show file tree
Hide file tree
Showing 14 changed files with 8,567 additions and 16 deletions.
Empty file added output/github/.gitkeep
Empty file.
8,427 changes: 8,427 additions & 0 deletions output/github/output.ts

Large diffs are not rendered by default.

Empty file added output/twitter/.gitkeep
Empty file.
82 changes: 82 additions & 0 deletions output/twitter/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as fc from "fast-check";
export const getArbitraryQuery = (): fc.Arbitrary<any> =>
fc.record({
Tweet: getArbitraryTweet(),
Tweets: fc.array(getArbitraryTweet()),
TweetsMeta: getArbitraryMeta(),
User: getArbitraryUser(),
Notifications: fc.array(getArbitraryNotification()),
NotificationsMeta: getArbitraryMeta(),
});

export const getArbitraryID = (): fc.Arbitrary<any> => fc.string();

export const getArbitraryTweet = (): fc.Arbitrary<any> =>
fc.record({
id: getArbitraryID(),
body: getArbitraryString(),
date: getArbitraryDate(),
Author: getArbitraryUser(),
Stats: getArbitraryStat(),
});

export const getArbitraryString = (): fc.Arbitrary<any> => fc.string();

export const getArbitraryDate = (): fc.Arbitrary<any> => fc.string();

export const getArbitraryUser = (): fc.Arbitrary<any> =>
fc.record({
id: getArbitraryID(),
username: getArbitraryString(),
first_name: getArbitraryString(),
last_name: getArbitraryString(),
full_name: getArbitraryString(),
name: getArbitraryString(),
avatar_url: getArbitraryUrl(),
});

export const getArbitraryUrl = (): fc.Arbitrary<any> => fc.string();

export const getArbitraryStat = (): fc.Arbitrary<any> =>
fc.record({
views: getArbitraryInt(),
likes: getArbitraryInt(),
retweets: getArbitraryInt(),
responses: getArbitraryInt(),
});

export const getArbitraryInt = (): fc.Arbitrary<any> => fc.integer();

export const getArbitraryMeta = (): fc.Arbitrary<any> =>
fc.record({ count: getArbitraryInt() });

export const getArbitraryNotification = (): fc.Arbitrary<any> =>
fc.record({
id: getArbitraryID(),
date: getArbitraryDate(),
type: getArbitraryString(),
});

export const getArbitraryMutation = (): fc.Arbitrary<any> =>
fc.record({
createTweet: getArbitraryTweet(),
deleteTweet: getArbitraryTweet(),
markTweetRead: getArbitraryBoolean(),
});

export const getArbitraryBoolean = (): fc.Arbitrary<any> => fc.boolean();

export const getArbitraryActionExecutionCapabilitySetting = (): fc.Arbitrary<
any
> =>
fc.oneof(
fc.constant("ALL_ACTIONS"),
fc.constant("DISABLED"),
fc.constant("LOCAL_ACTIONS_ONLY"),
fc.constant("NO_POLICY")
);

export const getArbitraryAddCommentInput = (): fc.Arbitrary<any> => fc.string();

export const getArbitraryStatOrNotification = (): fc.Arbitrary<any> =>
fc.oneof(getArbitraryStat(), getArbitraryNotification());
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"babel-jest": "^25.2.6",
"graphql": "^14.6.0",
"jest": "^25.2.4",
"ts-node": "^8.8.1",
"typescript": "^3.8.3"
},
"peerDependencies": {
Expand Down
17 changes: 11 additions & 6 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type FieldReturn = {

const withNamedFieldType = (namedTypeNode: NamedTypeNode): FieldReturn => {
const name = namedTypeNode.name.value as TypeName;
return { output: getArbitraryName(name), deps: [name] };
const output = `${getArbitraryName(name)}()` as Generated;
return { output, deps: [name] };
};

const withFieldType = (typeNode: TypeNode): FieldReturn => {
Expand Down Expand Up @@ -86,7 +87,7 @@ const withAstNode = (name: TypeName, node: TypeDefinitionNode): Output => {
return {
kind: "Scalar",
name,
output: `fc.anything()` as Generated,
output: `fc.string()` as Generated,
deps: []
};
case "EnumTypeDefinition":
Expand All @@ -101,7 +102,7 @@ const withAstNode = (name: TypeName, node: TypeDefinitionNode): Output => {
return {
kind: "InputObject",
name,
output: "fc.anything()" as Generated,
output: "fc.string()" as Generated,
deps: []
};
case "InterfaceTypeDefinition":
Expand Down Expand Up @@ -153,7 +154,7 @@ const withPrimitive = (node: GraphQLNamedType): Generated | null => {
};

const getArbitraryName = (typeName: TypeName): Generated =>
`arbitrary${typeName}` as Generated;
`getArbitrary${typeName}` as Generated;

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

const render = (val: Output) => {
const { name, output } = val;
return `export const ${getArbitraryName(name)} = ${output}`;
return `export const ${getArbitraryName(
name
)} = (): fc.Arbitrary<any> => ${output}`;
};

export const getSchemaDeclarations = (schema: GraphQLSchema): string =>
sortASTs(getNamedTypes(schema).map(withNamedType).filter(notNull))
sortASTs2(getNamedTypes(schema).map(withNamedType).filter(notNull))
.map(render)
.join("\n\n");

const sortASTs2 = <A>(a: A): A => a;

const filterSplit = <A>(
as: A[],
f: (a: A) => boolean
Expand Down
23 changes: 19 additions & 4 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
# die if anything goes wrong
set -euxo pipefail

# remove old output so it doesn't get typechecked
rm ./output/output.ts || true
# replace twitter output with something that typechecks
echo "export const a = 1" | tee ./output/twitter/output.ts

# replace github output with something that typechecks
echo "export const a = 1" | tee ./output/github/output.ts

# install shit
yarn install

# run tests
yarn test
Expand All @@ -13,7 +19,16 @@ yarn test
yarn build

# run codegen basic
yarn graphql-codegen --config ./test/codegen.yml
yarn graphql-codegen --config ./test/twitter/codegen.yml

# typecheck result
yarn tsc --noEmit ./output/output.ts
yarn tsc --noEmit ./output/twitter/output.ts

# run codegen advanced
yarn graphql-codegen --config ./test/github/codegen.yml

# typecheck advanced result
yarn tsc --noEmit ./output/github/output.ts

# output some shit (only from the basic one, the advanced one blows the stack)
yarn ts-node ./test/test-output.ts
5 changes: 0 additions & 5 deletions test/codegen.yml

This file was deleted.

5 changes: 5 additions & 0 deletions test/github/codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
schema: ./test/github/github-schema.graphql
# documents: './src/**/*.graphql'
generates:
./output/github/output.ts:
- ./dist/src/index.js
File renamed without changes.
16 changes: 16 additions & 0 deletions test/test-output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as generated from "../output/twitter/output";
import * as fc from "fast-check";

const items: any = { ...generated };

Object.keys(items).forEach((key: string) => {
const func = items[key];
console.log(`---------------${key}-----------------`);
const arb = (func as any)();
fc.assert(
fc.property(arb, a => {
console.log(a);
return true;
})
);
});
5 changes: 5 additions & 0 deletions test/twitter/codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
schema: ./test/twitter/twitter-schema.graphql
# documents: './src/**/*.graphql'
generates:
./output/twitter/output.ts:
- ./dist/src/index.js
File renamed without changes.
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5935,7 +5935,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/ts-log/-/ts-log-2.1.4.tgz#063c5ad1cbab5d49d258d18015963489fb6fb59a"
integrity sha512-P1EJSoyV+N3bR/IWFeAqXzKPZwHpnLY6j7j58mAvewHRipo+BQM2Y1f9Y9BjEQznKwgqqZm7H8iuixmssU7tYQ==

ts-node@^8:
ts-node@^8, ts-node@^8.8.1:
version "8.8.1"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.8.1.tgz#7c4d3e9ed33aa703b64b28d7f9d194768be5064d"
integrity sha512-10DE9ONho06QORKAaCBpPiFCdW+tZJuY/84tyypGtl6r+/C7Asq0dhqbRZURuUlLQtZxxDvT8eoj8cGW0ha6Bg==
Expand Down

0 comments on commit 04466fc

Please sign in to comment.