From e57dc8ccb768171ab36e1b66b785702a5c31a525 Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Wed, 1 Apr 2020 20:14:45 +0100 Subject: [PATCH] Chicken out of doing input types --- src/index.ts | 29 +++++++++-- test.sh | 3 -- test/codegen-github.yml | 4 -- test/test-schema.graphql | 109 ++++++++++++++++++++++++++------------- 4 files changed, 98 insertions(+), 47 deletions(-) delete mode 100644 test/codegen-github.yml diff --git a/src/index.ts b/src/index.ts index a0d5493..7dd4603 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,8 @@ import { TypeDefinitionNode, GraphQLNamedType, FieldDefinitionNode, - TypeNode + TypeNode, + EnumValueDefinitionNode } from "graphql"; import { format } from "prettier"; import * as fc from "fast-check"; @@ -45,14 +46,25 @@ const withField = (field: FieldDefinitionNode): string => { return `${fieldName}: ${fieldType}`; }; +const withEnum = (values: readonly EnumValueDefinitionNode[]) => { + const v = values + .map(value => { + const val = value.name.value; + return `fc.constant("${val}")`; + }) + .join(", "); + return `fc.oneof(${v})`; +}; + const withAstNode = (node: TypeDefinitionNode): [Kind, string] => { switch (node.kind) { case "ScalarTypeDefinition": return ["Scalar", `fc.anything()`]; case "EnumTypeDefinition": - throw "Haven't implemented Enums yet"; + return ["Enum", withEnum(node.values || [])]; case "InputObjectTypeDefinition": - throw "Haven't implemented InputObject yet"; + // not yet supported + return ["InputObject", ""]; case "InterfaceTypeDefinition": throw "Haven't implemented Interface yet"; case "ObjectTypeDefinition": @@ -81,7 +93,7 @@ const withPrimitive = (node: GraphQLNamedType): string | null => { const getArbitraryName = (typeName: TypeName): string => `arbitrary${typeName}`; -type Kind = "Object" | "Scalar" | "Primitive"; +type Kind = "Object" | "Scalar" | "Primitive" | "Enum" | "InputObject"; type NamedType = [Kind, TypeName, string]; const getNamedTypes = (schema: GraphQLSchema): GraphQLNamedType[] => { @@ -136,13 +148,20 @@ const getSchemaDeclarations = (schema: GraphQLSchema): string => { .map(render) .join("\n"); + const enums = namedTypes + .map(withNamedType) + .filter(notNull) + .filter(byKind("Enum")) + .map(render) + .join("\n"); + const objects = sortASTs( namedTypes.map(withNamedType).filter(notNull).filter(byKind("Object")) ) .map(render) .join("\n"); - return `${primitives}\n${scalars}\n${objects}`; + return `${primitives}\n${enums}\n${scalars}\n${objects}`; }; // if one object mentions another definition, put it after that definition diff --git a/test.sh b/test.sh index 388f266..4f9dca7 100755 --- a/test.sh +++ b/test.sh @@ -8,6 +8,3 @@ yarn build # run codegen basic yarn graphql-codegen --config ./test/codegen.yml - -# run complex one -yarn graphql-codegen --config ./test/codegen-github.yml diff --git a/test/codegen-github.yml b/test/codegen-github.yml deleted file mode 100644 index 6993f71..0000000 --- a/test/codegen-github.yml +++ /dev/null @@ -1,4 +0,0 @@ -schema: ./test/github-schema.graphql -generates: - ./output/github.ts: - - ./dist/index.js diff --git a/test/test-schema.graphql b/test/test-schema.graphql index f0b0c93..5121a9d 100644 --- a/test/test-schema.graphql +++ b/test/test-schema.graphql @@ -1,58 +1,97 @@ type Tweet { - id: ID! - # The tweet text. No more than 140 characters! - body: String - # When the tweet was published - date: Date - # Who published the tweet - Author: User - # Views, retweets, likes, etc - Stats: Stat + id: ID! + # The tweet text. No more than 140 characters! + body: String + # When the tweet was published + date: Date + # Who published the tweet + Author: User + # Views, retweets, likes, etc + Stats: Stat } type User { - id: ID! - username: String - first_name: String - last_name: String - full_name: String - name: String @deprecated - avatar_url: Url + id: ID! + username: String + first_name: String + last_name: String + full_name: String + name: String @deprecated + avatar_url: Url } type Stat { - views: Int - likes: Int - retweets: Int - responses: Int + views: Int + likes: Int + retweets: Int + responses: Int } type Notification { - id: ID - date: Date - type: String + id: ID + date: Date + type: String } +union StatOrNotification = Stat | Notification + type Meta { - count: Int + count: Int +} + +enum ActionExecutionCapabilitySetting { + """ + All action executions are enabled. + """ + ALL_ACTIONS + + """ + All action executions are disabled. + """ + DISABLED + + """ + Only actions defined within the repo are allowed. + """ + LOCAL_ACTIONS_ONLY + + """ + Organization administrators action execution capabilities. + """ + NO_POLICY +} + +input AddCommentInput { + """ + The contents of the comment. + """ + body: String! + + """ + A unique identifier for the client performing the mutation. + """ + clientMutationId: String + + """ + The Node ID of the subject to modify. + """ + subjectId: ID! } scalar Url scalar Date type Query { - Tweet(id: ID!): Tweet - Tweets(limit: Int, skip: Int, sort_field: String, sort_order: String): [Tweet] - TweetsMeta: Meta - User(id: ID!): User - Notifications(limit: Int): [Notification] - NotificationsMeta: Meta + Tweet(id: ID!): Tweet + Tweets(limit: Int, skip: Int, sort_field: String, sort_order: String): [Tweet] + TweetsMeta: Meta + User(id: ID!): User + Notifications(limit: Int): [Notification] + NotificationsMeta: Meta } type Mutation { - createTweet ( - body: String - ): Tweet - deleteTweet(id: ID!): Tweet - markTweetRead(id: ID!): Boolean + createTweet(body: String): Tweet + deleteTweet(id: ID!): Tweet + markTweetRead(id: ID!): Boolean }