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

Supports relational types in GraphQL #110

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
12 changes: 7 additions & 5 deletions src/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ export type InternalEntity<

export type ModelDictionary = Limit<Record<string, Record<string, any>>>

export type ModelDictionaryValue<T extends Record<string, any>> =
| (() => BaseTypes)
| PrimaryKeyDeclaration
| OneOf<keyof T>
| ManyOf<keyof T>

export type Limit<T extends Record<string, any>> = {
[RK in keyof T]: {
[SK in keyof T[RK]]: T[RK][SK] extends
| (() => BaseTypes)
| PrimaryKeyDeclaration
| OneOf<keyof T>
| ManyOf<keyof T>
[SK in keyof T[RK]]: T[RK][SK] extends ModelDictionaryValue<T>
? T[RK][SK]
: {
error: 'expected a value or a relation'
Expand Down
40 changes: 31 additions & 9 deletions src/model/generateGraphQLHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ import {
GraphQLInputType,
GraphQLScalarType,
GraphQLFieldConfigArgumentMap,
GraphQLNonNull,
} from 'graphql'
import { GraphQLHandler, graphql } from 'msw'
import { ModelAPI, ModelDefinition, ModelDictionary } from '../glossary'
import {
ModelAPI,
ModelDefinition,
ModelDictionary,
ModelDictionaryValue,
} from '../glossary'
import { capitalize } from '../utils/capitalize'
import { QueryToComparator } from '../query/queryTypes'
import { booleanComparators } from '../comparators/boolean'
Expand All @@ -32,16 +38,32 @@ interface GraphQLFieldsMap {
/**
* Derive a GraphQL scalar type from a variable.
*/
export function getGraphQLType(value: any) {
export function getGraphQLType(
value: ModelDictionaryValue<any>,
): GraphQLScalarType {
const resolvedValue = typeof value === 'function' ? value() : value
switch (resolvedValue.constructor.name) {
case 'Number':
return GraphQLInt
case 'Boolean':
return GraphQLBoolean
default:
return GraphQLString

if (typeof resolvedValue === 'number') {
return GraphQLID
}

if (typeof resolvedValue === 'string') {
return GraphQLString
}

if (typeof resolvedValue === 'boolean') {
return GraphQLBoolean
}

if ('kind' in resolvedValue) {
return new GraphQLScalarType({
name: capitalize(resolvedValue.modelName.toString()),
})
}

console.log({ resolvedValue })

return GraphQLString
}

/**
Expand Down
83 changes: 73 additions & 10 deletions test/model/toGraphQLSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { datatype } from 'faker'
import { factory, primaryKey } from '@mswjs/data'
import { printSchema } from 'graphql'
import { factory, primaryKey, oneOf } from '@mswjs/data'

const db = factory({
user: {
id: primaryKey(datatype.uuid),
firstName: String,
age: Number,
},
})

test('generates a graphql schema', () => {
it('generates a graphql schema', () => {
const db = factory({
user: {
id: primaryKey(datatype.uuid),
firstName: String,
age: Number,
},
})
const schema = db.user.toGraphQLSchema()

expect(printSchema(schema)).toMatchInlineSnapshot(`
"type Query {
user(where: UserQueryInput): User
Expand Down Expand Up @@ -75,3 +75,66 @@ test('generates a graphql schema', () => {
"
`)
})

it.only('infers relational property types', () => {
const db = factory({
user: {
id: primaryKey(datatype.uuid),
role: oneOf('role'),
},
role: {
id: primaryKey(datatype.uuid),
name: String,
},
})
const schema = db.user.toGraphQLSchema()

expect(printSchema(schema)).toMatchInlineSnapshot(`
"type Query {
user(where: UserQueryInput): User
users(take: Int, skip: Int, cursor: ID, where: UserQueryInput): [User]
}

type User {
id: ID
role: Role
}

input UserQueryInput {
id: IdQueryType
role: StringQueryType
}

input IdQueryType {
equals: ID
notEquals: ID
contains: ID
notContains: ID
in: ID
notIn: ID
}

input StringQueryType {
equals: String
notEquals: String
contains: String
notContains: String
in: String
notIn: String
}

type Mutation {
createUser(data: UserInput): User
updateUser(where: UserQueryInput, data: UserInput): User
updateUsers(where: UserQueryInput, data: UserInput): [User]
deleteUser(where: UserQueryInput): User
deleteUsers(where: UserQueryInput): [User]
}

input UserInput {
id: ID
role: String
}
"
`)
})