Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full graphql schema (broken) #1

Merged
merged 4 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Sorting sorted
  • Loading branch information
danieljharvey committed Apr 4, 2020
commit ffd4a8722f481feaca98437189d5b9005165c335
37 changes: 30 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const withEnum = (values: readonly EnumValueDefinitionNode[]) => {
return `fc.oneof(${v})`;
};

const withUnion = (types: readonly NamedTypeNode[]): string => {
const nodes = types.map(withNamedFieldType).join(", ");
return `fc.oneof(${nodes})`;
};

const withAstNode = (node: TypeDefinitionNode): [Kind, string] => {
switch (node.kind) {
case "ScalarTypeDefinition":
Expand All @@ -66,12 +71,13 @@ const withAstNode = (node: TypeDefinitionNode): [Kind, string] => {
// not yet supported
return ["InputObject", ""];
case "InterfaceTypeDefinition":
throw "Haven't implemented Interface yet";
// not yet supported
return ["Interface", ""];
case "ObjectTypeDefinition":
const fields = (node.fields || []).map(withField);
return ["Object", `fc.record({${fields.join(",")}})`];
case "UnionTypeDefinition":
throw "Haven't implemented Union yet";
return ["Union", withUnion(node.types || [])];
}
};

Expand All @@ -85,6 +91,8 @@ const withPrimitive = (node: GraphQLNamedType): string | null => {
return "fc.string()";
case "ID":
return `fc.string()`;
case "Float":
return `fc.float()`;
}
// console.log(`Primitive ${node.name} not found`);

Expand All @@ -93,7 +101,14 @@ const withPrimitive = (node: GraphQLNamedType): string | null => {

const getArbitraryName = (typeName: TypeName): string => `arbitrary${typeName}`;

type Kind = "Object" | "Scalar" | "Primitive" | "Enum" | "InputObject";
type Kind =
| "Object"
| "Scalar"
| "Primitive"
| "Enum"
| "Union"
| "InputObject"
| "Interface";
type NamedType = [Kind, TypeName, string];

const getNamedTypes = (schema: GraphQLSchema): GraphQLNamedType[] => {
Expand Down Expand Up @@ -155,13 +170,21 @@ const getSchemaDeclarations = (schema: GraphQLSchema): string => {
.map(render)
.join("\n");

const objects = sortASTs(
namedTypes.map(withNamedType).filter(notNull).filter(byKind("Object"))
)
const unions = namedTypes
.map(withNamedType)
.filter(notNull)
.filter(byKind("Union"));

const objects = namedTypes
.map(withNamedType)
.filter(notNull)
.filter(byKind("Object"));

const mixed = sortASTs([...unions, ...objects])
.map(render)
.join("\n");

return `${primitives}\n${enums}\n${scalars}\n${objects}`;
return `${primitives}\n${enums}\n${scalars}\n${mixed}`;
};

// if one object mentions another definition, put it after that definition
Expand Down
2 changes: 1 addition & 1 deletion test/codegen.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
schema: ./test/test-schema.graphql
schema: ./test/github-schema.graphql
# documents: './src/**/*.graphql'
generates:
./output/output.ts:
Expand Down