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

expose graphql schema via factory.toSchema #96

Merged
merged 3 commits into from
Jun 9, 2021
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
10 changes: 8 additions & 2 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { updateEntity } from './model/updateEntity'
import { OperationError, OperationErrorType } from './errors/OperationError'
import { Database } from './db/Database'
import { generateRestHandlers } from './model/generateRestHandlers'
import { generateGraphQLHandlers } from './model/generateGraphQLHandlers'
import {
generateGraphQLHandlers,
generateGraphQLSchema,
} from './model/generateGraphQLHandlers'
import { sync } from './extensions/sync'
import { removeInternalProperties } from './utils/removeInternalProperties'

Expand Down Expand Up @@ -256,13 +259,16 @@ function createModelApi<

return records.map(removeInternalProperties)
},
toHandlers(type, baseUrl): any {
toHandlers(type: 'rest' | 'graphql', baseUrl: string): any {
if (type === 'graphql') {
return generateGraphQLHandlers(modelName, definition, api, baseUrl)
}

return generateRestHandlers(modelName, definition, api, baseUrl)
},
toGraphQLSchema() {
return generateGraphQLSchema(modelName, definition, api)
},
}

return api
Expand Down
15 changes: 11 additions & 4 deletions src/glossary.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GraphQLSchema } from 'graphql'
import { GraphQLHandler, RestHandler } from 'msw'
import {
BulkQueryOptions,
Expand Down Expand Up @@ -174,10 +175,16 @@ export interface ModelAPI<
/**
* Generate request handlers of the given type based on the model.
*/
toHandlers<HandlerType extends 'rest' | 'graphql'>(
type: HandlerType,
baseUrl?: string,
): HandlerType extends 'rest' ? RestHandler[] : GraphQLHandler[]
toHandlers(type: 'rest', baseUrl?: string): RestHandler[]
/**
* Generate request handlers of the given type based on the model.
*/
toHandlers(type: 'graphql', baseUrl?: string): GraphQLHandler[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for these overloads, this is great! ✨


/**
* Generate a graphql schema based on the model.
*/
toGraphQLSchema(): GraphQLSchema
}

export type UpdateManyValue<
Expand Down
22 changes: 18 additions & 4 deletions src/model/generateGraphQLHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,14 @@ export function definitionToFields(
)
}

export function generateGraphQLHandlers<
export function generateGraphQLSchema<
Dictionary extends ModelDictionary,
ModelName extends string
>(
modelName: ModelName,
definition: ModelDefinition,
model: ModelAPI<Dictionary, ModelName>,
baseUrl: string = '',
): GraphQLHandler[] {
const target = baseUrl ? graphql.link(baseUrl) : graphql
): GraphQLSchema {
const pluralModelName = pluralize(modelName)
const capitalModelName = capitalize(modelName)
const { fields, inputFields, queryInputFields } = definitionToFields(
Expand Down Expand Up @@ -268,6 +266,22 @@ export function generateGraphQLHandlers<
}),
})

return objectSchema
}

export function generateGraphQLHandlers<
Dictionary extends ModelDictionary,
ModelName extends string
>(
modelName: ModelName,
definition: ModelDefinition,
model: ModelAPI<Dictionary, ModelName>,
baseUrl: string = '',
): GraphQLHandler[] {
const target = baseUrl ? graphql.link(baseUrl) : graphql

const objectSchema = generateGraphQLSchema(modelName, definition, model)

return [
target.operation(async (req, res, ctx) => {
if (!req.body) {
Expand Down
77 changes: 77 additions & 0 deletions test/model/toGraphQLSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { datatype } from 'faker'
import { factory, primaryKey } from '@mswjs/data'
import { printSchema } from 'graphql'

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

test('generates a graphql schema', () => {
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
firstName: String
age: Int
}

input UserQueryInput {
id: IdQueryType
firstName: StringQueryType
age: IntQueryType
}

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
}

input IntQueryType {
equals: Int
notEquals: Int
between: Int
notBetween: Int
gt: Int
gte: Int
lt: Int
lte: Int
}

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
firstName: String
age: Int
}
"
`)
})