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

Conversation

phryneas
Copy link
Contributor

This allows to expose the schema directly, to be consumed for example by graphql-codegen - and in consequence by TypeScript (via generated types) or the Apollo Graphql VSCode extension (via an exported introspection schema)

As an example:

// src/mocks/db.ts
import { factory, primaryKey } from '@mswjs/data'
const db = factory({
  post: {
    id: primaryKey(String),
    name: String,
    title: String,
    author: String,
    content: String,
    status: String,
    created_at: String,
    updated_at: String,
  },
})

export const handlers = db.post.toHandlers('graphql')
export const schema = db.post.toSchema('graphql')
// src/mocks/schema.js
require('ts-node/register')
module.exports = require('./db').schema
// codegen.yml
overwrite: true
schema: './src/mocks/schema.js'
documents: 'src/**/*.graphql'
generates:
  src/app/services/graphql.generated.ts:
    plugins:
      - 'typescript'
      - 'typescript-operations'
      - 'typed-document-node'
  .introspection.json:
    plugins:
      - introspection

src/factory.ts Outdated
if (type === 'graphql') {
return generateGraphQLHandlers(modelName, definition, api, baseUrl)
}

return generateRestHandlers(modelName, definition, api, baseUrl)
},
toSchema(type: 'graphql') {
Copy link
Member

Choose a reason for hiding this comment

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

Given we support only GraphQL schema, what do you think about naming this method .toGraphQLSchema() instead? Once we support more types, if ever, we can move to a more generic .toSchema.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! I was assuming this would be the "least breaking" upgrade path in case you introduce stuff like OpenAPI schemas in the future, but I see why it could be confusing for users that discover the API without TypeScript.
I'll change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed it as requested :)

/**
* 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! ✨

@kettanaito
Copy link
Member

Hey, @phryneas. Thank you for these improvements, I think they're great. I've only had one concern about the .toSchema API. Please, would you have time to address it? I can take over with that change as well, if you're okay with it.

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

This is great. Thank you for your work on this and welcome to contributors! 🎉

@kettanaito kettanaito merged commit 672226d into mswjs:master Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants