Skip to content

Commit

Permalink
added support for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Jan 16, 2024
1 parent 5d3fc2e commit 2ac8c1a
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 141 deletions.
4 changes: 2 additions & 2 deletions examples/olympus/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "olympus",
"version": "1.1.7",
"version": "1.1.8",
"description": "Example of using GraphQL Demeter",
"private": true,
"main": "index.js",
Expand All @@ -10,6 +10,6 @@
"author": "Aexol <[email protected]> (http://aexol.com)",
"license": "ISC",
"dependencies": {
"graphql-demeter": "^1.0.4"
"graphql-demeter": "^1.0.5"
}
}
273 changes: 137 additions & 136 deletions examples/olympus/zeus.graphql
Original file line number Diff line number Diff line change
@@ -1,175 +1,176 @@
"""Aws S3 File"""
type S3Object {
""""""
bucket: String!

""""""
key: String!

""""""
region: String!
type Beer implements Product{
name: String!
SKU: String!
price: Int!
kind: BeerKind
}

""""""
union ChangeCard = SpecialCard | EffectCard

""""""
enum SpecialSkills {
"""Lower enemy defense -5<br>"""
THUNDER

"""Attack multiple Cards at once<br>"""
RAIN

"""50% chance to avoid any attack<br>"""
FIRE
type ClientQuery{
menu: [Product!]!
}

"""Card used in card game<br>"""
type Card implements Nameable {
"""The attack power<br>"""
Attack: Int!

"""<div>How many children the greek god had</div>"""
Children: Int

"""The defense power<br>"""
Defense: Int!

"""Attack other cards on the table , returns Cards after attack<br>"""
attack(
"""Attacked card/card ids<br>"""
cardID: [String!]!
): [Card!]

"""Put your description here"""
cardImage: S3Object

"""Description of a card<br>"""
description: String!

""""""
id: ID!

""""""
image: String!

""""""
info: JSON!
type Query{
clientQuery: ClientQuery!
pubQuery: PubQuery!
restProxy: Anything
getFile(
fileKey: String!
): String
}

"""The name of a card<br>"""
name: String!
scalar Anything

""""""
skills: [SpecialSkills!]
input ProxyInput{
val: String!
}

""""""
interface Nameable {
""""""
name: String!
type Order{
number: Int!
createdAt: String!
orderedBeers: [OrderedBeer!]!
}

""""""
scalar JSON

""""""
type Powerup {
""""""
name: String
type OrderedBeer{
quantity: Int!
beer: Beer!
}

""""""
type Mutation {
"""add Card to Cards database<br>"""
addCard(card: createCard!): Card!
type PubQuery{
orders: [Order!]!
}

"""Stack of cards"""
type CardStack implements Nameable {
""""""
cards: [Card!]

""""""
name: String!
type PubMutation{
submitOrder(
order: SubmitOrder!
): Boolean
addBeer(
beer: AddBeer!
): Boolean
editBeer(
editBeer: EditBeer!
sku: SKUInput!
): Boolean
removeBeer(
sku: SKUInput!
): Boolean
cancelOrder(
orderDetail: OrderDetail!
): Boolean
uploadFile(
fileInput: FileInput!
): FileUploadResponse!
addSnack(
snack: AddSnack!
): Boolean
editSnack(
sku: SKUInput!
snack: EditSnack!
): Boolean
deleteSnack: Boolean
}

""""""
type Public {
""""""
powerups(filter: String!): [Powerup!]
input FileInput{
filename: String!
contentType: String!
}

""""""
type Query {
""""""
cardById(cardId: String): Card

"""Draw a card<br>"""
drawCard: Card!
type FileUploadResponse{
putUrl: String!
fileKey: String!
}

""""""
drawChangeCard: ChangeCard!
input AddBeer{
name: String!
SKU: String!
price: Int!
kind: BeerKind
}

"""list All Cards availble<br>"""
listCards: [Card!]!
input EditBeer{
name: String
price: Int
kind: BeerKind
}

""""""
myStacks: [CardStack!]
input SKUInput{
SKU: String!
}

""""""
nameables: [Nameable!]!
input SubmitOrder{
productInOrder: [ProductInOrder!]! = []
}

""""""
public: Public
input ProductInOrder{
SKU: String!
quantity: Int!
}

""""""
type Subscription {
""""""
deck: [Card!]
input OrderDetail{
number: Int!
}

""""""
type SpecialCard implements Nameable {
""""""
effect: String!
type Mutation{
pubMutation: PubMutation
"""
user
"""
register(
user: RegisterInput!
): RegisterResponse!
}

""""""
name: String!
type RegisterResponse{
registered: Boolean
hasError: RegisterErrors
}

""""""
type EffectCard implements Nameable {
""""""
effectSize: Float!
input RegisterInput{
username: String!
password: String!
invitationToken: String
}

""""""
name: String!
enum RegisterErrors{
USERNAME_EXISTS
PASSWORD_WEAK
INVITE_DOMAIN_INCORRECT
LINK_EXPIRED
USERNAME_INVALID
}

"""create card inputs<br>"""
input createCard {
"""The name of a card<br>"""
name: String!
schema{
query: Query
mutation: Mutation
}

"""Description of a card<br>"""
description: String!
enum BeerKind{
IPA
APA
Lager
Sour
Ale
}

"""<div>How many children the greek god had</div>"""
Children: Int
interface Product{
SKU: String!
name: String!
price: Int!
}

"""The attack power<br>"""
Attack: Int!
type Snack implements Product{
SKU: String!
name: String!
price: Int!
}

"""The defense power<br>"""
Defense: Int!
input AddSnack{
SKU: String!
name: String!
price: Int!
}

"""input skills"""
skills: [SpecialSkills!]
input EditSnack{
SKU: String
name: String
price: Int
}
schema{
query: Query,
mutation: Mutation,
subscription: Subscription
}
2 changes: 1 addition & 1 deletion packages/graphql-demeter-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-demeter-core",
"version": "1.0.4",
"version": "1.0.5",
"private": false,
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
13 changes: 13 additions & 0 deletions packages/graphql-demeter-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const mockObjectValue = (f: FieldType, required = false): unknown => {
export const createFakeResolvers = (schemaString: string, fakerConfig?: FakerConfig) => {
const tree = Parser.parse(schemaString);
const scalars = tree.nodes.filter((n) => n.data.type === TypeDefinition.ScalarTypeDefinition).map((n) => n.name);
const enums = tree.nodes.filter((n) => n.data.type === TypeDefinition.EnumTypeDefinition).map((n) => n.name);
const resolvers = Object.fromEntries(
tree.nodes
.filter((n) => n.data.type === TypeDefinition.ObjectTypeDefinition)
Expand All @@ -48,6 +49,18 @@ export const createFakeResolvers = (schemaString: string, fakerConfig?: FakerCon
return [
a.name,
() => {
const isEnum = enums.find((s) => s === tName);
if (isEnum) {
return mockValue(a.type.fieldType, () => {
const e = tree.nodes.find((tn) => tn.name === tName);
if (!e) {
console.warn(`Can't find enum "${tName} in schema returning brokenEnum"`);
return 'brokenEnum';
}
const possibleEnumValues = e.args.map((a) => a.name);
return possibleEnumValues[Math.floor(Math.random() * possibleEnumValues.length)];
});
}
const isCustomScalar = scalars.find((s) => s === tName);
const resolverValuesScalar = isCustomScalar && fakerConfig?.scalars?.[isCustomScalar];
const resolverValues = resolverValuesScalar || fakerConfig?.objects?.[n.name]?.[a.name];
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-demeter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-demeter",
"version": "1.0.4",
"version": "1.0.5",
"private": false,
"scripts": {
"start": "tspc --watch",
Expand All @@ -26,7 +26,7 @@
"dependencies": {
"cross-fetch": "^3.0.4",
"graphql": "15.4.0",
"graphql-demeter-core": "^1.0.4",
"graphql-demeter-core": "^1.0.5",
"graphql-yoga": "^5.1.1",
"yargs": "^16.1.1"
},
Expand Down

0 comments on commit 2ac8c1a

Please sign in to comment.