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

feat: add operationName to GraphQL request #1568

Merged
merged 2 commits into from
Mar 20, 2023

Conversation

ligaz
Copy link
Contributor

@ligaz ligaz commented Mar 18, 2023

Reasoning:

When a wildcard GraphQL query handler is registered (via regex) it is useful to have the parsed operationName in the request. Having the operationName allows the consumers to implement more complex/dynamic resolvers.

Reasoning:

When a wildcard GraphQL query handler is registered (via regex) it is
useful to have the parsed operationName in the request. Having the
operationName allows the consumers to implement more complex/dynamic
resolvers.
@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 18, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 970548f:

Sandbox Source
MSW React Configuration

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.

Thanks for shipping this improvement, @ligaz!

Could you please double-check that request.operationName is also properly available in TypeScript when using MSW?

You can do that by pasting the following code in any TS project with MSW installed:

import { graphql } from 'msw'

export const handlers = [
  graphql.query(/.+/, (req) => req.operationName.toUpperCase())
]

@ligaz
Copy link
Contributor Author

ligaz commented Mar 20, 2023

Thanks @kettanaito for the follow up. I just tested this locally and the operationName is available on the request object.

Here are the steps:

# Create a React app with TypeScript template
yarn create react-app my-app --template typescript
# Add the package generated from the MSW CI build
yarn add https://pkg.csb.dev/mswjs/msw/commit/0f7697fd/msw
// dummy handlers.ts
import { graphql } from 'msw';

export const handlers = [
  graphql.query(/.+/, (req, res, ctx) =>
    req.operationName === 'MyQuery' ? res(ctx.data({})) : res(ctx.data({})),
  ),
];

Here is the type declaration signature that is being generated:

declare class GraphQLRequest<Variables extends GraphQLVariables> extends MockedRequest<GraphQLRequestBody<Variables>> {
    readonly variables: Variables;
    readonly operationName: string;
    constructor(request: MockedRequest, variables: Variables, operationName: string);
}

@kettanaito
Copy link
Member

@ligaz, do you think it'd make sense to also expose the operationType? ("query"/"mutation")

@ligaz
Copy link
Contributor Author

ligaz commented Mar 20, 2023

@ligaz, do you think it'd make sense to also expose the operationType? ("query"/"mutation")

I do not have such a need in my current project. If someone expresses such a need in the future we can extend the request to provide this information as well.

@kettanaito kettanaito merged commit 115b435 into mswjs:main Mar 20, 2023
@kettanaito kettanaito mentioned this pull request Mar 21, 2023
34 tasks
@kettanaito
Copy link
Member

Released: v1.2.0 🎉

This has been released in v1.2.0!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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