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

Feature/add hasura default types #103

Merged
merged 3 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions docs/pages/04.Plugins/02.GraphQL Query Generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ query getPerson($delay: Int, $delay2: Int) {

```json
{
"delay": 20,
"delay2": 20
"delay": 42,
"delay2": 42
}
```

Expand Down
270 changes: 167 additions & 103 deletions packages/plugins/query-generator/src/generator/defaultFactories.ts
Original file line number Diff line number Diff line change
@@ -1,109 +1,173 @@
import _ from 'lodash'
import type { GraphQLFactory } from './config'

const date = '2022-03-06T08:23:45.000Z'
const date = '2022-03-06'
const dateTime = '2022-03-06T08:23:45.000Z'
const id = '08a16b83-9094-4e89-8c05-2ccadd5c1c7e'
const bigNumber = 123456789
const url = 'https://website.com'
const float = 30.7

export const DEFAULT_FACTORIES: Record<string, GraphQLFactory> = {
String: (context) => {
switch (context.targetName.toLowerCase()) {
case 'email':
case 'emails':
case 'mail':
case 'mails':
return 'test-email@yourcompany.com'
case 'fullname':
case 'fullnames':
return 'John Doe'
case 'firstname':
case 'firstnames':
return 'John'
case 'lastname':
case 'lastnames':
return 'Doe'
case 'job':
case 'jobs':
case 'jobtitle':
case 'jobtitles':
return 'Engineer'
case 'jobarea':
return 'Engineering'
case 'phone':
case 'phones':
return '1+ 418-323-4236'
case 'country':
case 'countries':
return 'Canada'
case 'address':
case 'addresses':
return '2832 Sesame Street'
case 'state':
case 'states':
return 'Quebec'
case 'city':
case 'cities':
return 'Montreal'
case 'zipcode':
case 'zipcodes':
return 'G1G 43F'
case 'company':
case 'companies':
return 'Acme'
case 'department':
case 'departments':
return 'Engineering'
case 'date':
case 'dates':
case 'datetime':
case 'datetimes':
case 'instant':
return date
case 'price':
case 'prices':
return '500$'
case 'color':
case 'colors':
return '#e10098'
case 'product':
case 'products':
return 'Soup'
case 'material':
case 'materials':
return 'Wood'
case 'id':
case 'ids':
return id
case 'name':
case 'names':
return 'Some name'
case 'description':
case 'descriptions':
return 'Some description'
default:
return 'abc'
}
},
ID: () => id,
Boolean: () => true,
Int: (context) => {
switch (context.targetName.toLocaleLowerCase()) {
case 'salary':
case 'salaries':
return 70_000
case 'age':
case 'ages':
return 42
default:
return 20
}
},
Float: () => 30.7,
Date: () => date,
DateTime: () => date,
Instant: () => date,
BigInteger: () => bigNumber,
Long: () => bigNumber,
Url: () => url,
URL: () => url,
export const DEFAULT_FACTORIES: Record<string, GraphQLFactory> = withVariants(
withAliases({
// Common GraphQL Types
String: (context) => {
switch (context.targetName.toLowerCase()) {
case 'email':
case 'emails':
case 'mail':
case 'mails':
return 'test-email@yourcompany.com'
case 'fullname':
case 'fullnames':
return 'John Doe'
case 'firstname':
case 'firstnames':
return 'John'
case 'lastname':
case 'lastnames':
return 'Doe'
case 'job':
case 'jobs':
case 'jobtitle':
case 'jobtitles':
return 'Engineer'
case 'jobarea':
return 'Engineering'
case 'phone':
case 'phones':
return '1+ 418-323-4236'
case 'country':
case 'countries':
return 'Canada'
case 'address':
case 'addresses':
return '2832 Sesame Street'
case 'state':
case 'states':
case 'province':
case 'provinces':
return 'Quebec'
case 'city':
case 'cities':
case 'town':
case 'towns':
return 'Montreal'
case 'zipcode':
case 'zipcodes':
return 'G1G 43F'
case 'company':
case 'companies':
return 'Acme Inc'
case 'department':
case 'departments':
return 'Engineering'
case 'datetime':
case 'datetimes':
case 'timestamp':
case 'timestamps':
case 'timestampz':
case 'instant':
case 'instants':
return dateTime
case 'date':
case 'dates':
return date
case 'price':
case 'prices':
return '$1,234.56'
case 'color':
case 'colors':
case 'paint':
case 'paints':
return '#e10098'
case 'product':
case 'products':
return 'Soup'
case 'material':
case 'materials':
return 'Wood'
case 'id':
case 'ids':
case 'identifier':
case 'identifiers':
case 'uuid':
case 'uuids':
return id
case 'size':
case 'sizes':
return 'Large'
case 'url':
case 'urls':
return url
case 'name':
case 'names':
return 'A name'
case 'description':
case 'descriptions':
return 'A description'
default:
return 'lorem ipsum'
}
},
ID: () => id,
Boolean: () => true,
Int: (context) => {
switch (context.targetName.toLocaleLowerCase()) {
case 'salary':
case 'salaries':
return 70_000
case 'age':
case 'ages':
return 36
default:
return 42
}
},
Float: () => float,
Date: () => date,
DateTime: () => dateTime,
URL: () => url,
}),
)

function withVariants(
record: Record<string, GraphQLFactory>,
): Record<string, GraphQLFactory> {
return {
..._.reduce(
record,
(acc, value, key) => ({
...acc,
// Full lowercase
[key.toLowerCase()]: value,
// Only first letter lowercase
[key[0].toLowerCase() + key.slice(1)]: value,
}),
{},
),
...record,
}
}

function withAliases(
record: Record<string, GraphQLFactory>,
): Record<string, GraphQLFactory> {
return {
...record,
Instant: record['DateTime'],
Id: record['ID'],
BigInteger: record['Int'],
BigNumber: record['Int'],
Long: record['Int'],
Url: record['URL'],

// Hasura types
_text: record['String'],
Float8: record['Float'],
Numeric: record['Int'],
SmallInt: record['Int'],
Timestamp: record['DateTime'],
Timestamptz: record['DateTime'],
JsonB: () => ({}),
}
}
Loading