Skip to content

Commit

Permalink
Chicken out of doing input types
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Apr 4, 2020
1 parent 380a2f5 commit e57dc8c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 47 deletions.
29 changes: 24 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
TypeDefinitionNode,
GraphQLNamedType,
FieldDefinitionNode,
TypeNode
TypeNode,
EnumValueDefinitionNode
} from "graphql";
import { format } from "prettier";
import * as fc from "fast-check";
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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[] => {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 0 additions & 4 deletions test/codegen-github.yml

This file was deleted.

109 changes: 74 additions & 35 deletions test/test-schema.graphql
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e57dc8c

Please sign in to comment.