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

Refactor createHandler to createHandlerFromSchema #83

Merged
merged 22 commits into from
Aug 29, 2024

Conversation

alessbell
Copy link
Member

@alessbell alessbell commented Aug 1, 2024

This PR does a few things:

  • introduces utilities that generates a base set of default resolvers when calling createHandler with a schema document
  • renames the previous version of createHandler to createHandlerFromSchema
  • introduces a custom request handler class for future customization (see Code feedback #44)
  • pulls the public GH schema into the repo for testing purposes (see PR diff size 😅)
  • accepts a generic type argument for Resolvers which now type checks any resolvers passed into createHandler or createHandlerFromSchema

Copy link

changeset-bot bot commented Aug 1, 2024

🦋 Changeset detected

Latest commit: 674da60

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/graphql-testing-library Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Aug 1, 2024

Deploy Preview for graphql-testing-library canceled.

Name Link
🔨 Latest commit 674da60
🔍 Latest deploy log https://app.netlify.com/sites/graphql-testing-library/deploys/66d0bd6ea6fb3200083c1b26

@alessbell
Copy link
Member Author

/release:pr

Copy link
Contributor

github-actions bot commented Aug 1, 2024

Please add a changeset via npx changeset before attempting a snapshot release.

@alessbell
Copy link
Member Author

/release:pr

Copy link
Contributor

github-actions bot commented Aug 1, 2024

A new release has been made for this PR. You can install it with:

npm i @apollo/graphql-testing-library@0.0.0-pr-83-20240801212913

@alessbell
Copy link
Member Author

/release:pr

Copy link
Contributor

github-actions bot commented Aug 5, 2024

A new release has been made for this PR. You can install it with:

npm i @apollo/graphql-testing-library@0.0.0-pr-83-20240805191102

@@ -0,0 +1,19 @@
import type { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
Copy link
Member Author

Choose a reason for hiding this comment

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

Introduces codegen to be able to resolver typing support - will add type tests in a future PR


Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder }, // jest
Copy link
Member Author

Choose a reason for hiding this comment

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

performance and clearImmediate were unnecessary here and causing issues

resolvers: {
Query: {
products: () =>
Array.from({ length: products.length }, (_element, id) => ({
id,
id: `${id}`,
Copy link
Member Author

Choose a reason for hiding this comment

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

Type checking caught this :)

@alessbell
Copy link
Member Author

/release:pr

Copy link
Contributor

A new release has been made for this PR. You can install it with:

npm i @apollo/graphql-testing-library@0.0.0-pr-83-20240813204214

Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

I really like the changes here! I was curious about beefing up the changeset and had a couple other questions, but otherwise looks great!

const sortEnumValues = () => {
const key = "value";
return (a: GraphQLEnumValue, b: GraphQLEnumValue) =>
a[key] > b[key] ? 1 : b[key] > a[key] ? -1 : 0;
Copy link
Member

Choose a reason for hiding this comment

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

I'm curious about the use of key here instead of a.value, b.value, etc. Is this a TypeScript thing to get around a private property access or something?

const schemaWithMocks = addMocksToSchema({
schema,
const graphQLHandler = createHandler<Resolvers>({
typeDefs,
resolvers: {
Query: {
products: () =>
Array.from({ length: products.length }, (_element, id) => ({
Copy link
Member

Choose a reason for hiding this comment

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

I know this isn't part of this PR so feel free to ignore, but a potential optimization here might be to just map over products so you can avoid the whole array filling:

products.map((product, id) => ({
  id: `${id}`,
  title: product,
  // etc.
})


export { createHandler };
export {
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 not doing export * 🙏

"@apollo/graphql-testing-library": patch
---

Adds `createHandlerFromSchema`
Copy link
Member

Choose a reason for hiding this comment

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

I see you've also added a bunch of exports for utilities. Would that be worth calling out here or in a separate changeset?

I've also noticed that createHandlerFromSchema is more-or-less the same as createHandler was before this since the old createHandler took a schema as an argument. Would it be worth calling out that breaking change to specify that createHandler now accepts type defs and provides a bunch of the default resolvers for you?

@alessbell alessbell merged commit 2cf1dcd into main Aug 29, 2024
12 checks passed
@alessbell alessbell deleted the accept-resolvers-in-createHandler branch August 29, 2024 18:42
@github-actions github-actions bot mentioned this pull request Aug 29, 2024
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