Skip to content

Commit

Permalink
Totally fucking smashed it
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Apr 5, 2020
1 parent d9681a3 commit 6d67809
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
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
5 changes: 4 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
set -euxo pipefail

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

# run tests
yarn test
Expand All @@ -17,3 +17,6 @@ yarn graphql-codegen --config ./test/codegen.yml

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

# output some shit
yarn ts-node ./output/test-output.ts
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 6d67809

Please sign in to comment.