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

Typed returned Data #59

Open
jBernavaPrah opened this issue Jul 31, 2023 · 3 comments
Open

Typed returned Data #59

jBernavaPrah opened this issue Jul 31, 2023 · 3 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@jBernavaPrah
Copy link

Hi! Thanks for sharing this package!

It's possible to use Types during mocking?

I'm using graphql-codegen to generate all the Types for my graphQL endpoint.

export type ReturnDataQuery = { __typename?: 'Query', something?: string | null };

const intercept = laika.intercept({operationName: 'getUsers'});
// something like this:
intercept.mockResult<ReturnDataQuery>(
  {
  result: {
  data: {
         something: "ABC" // typescript checked
  }
  }
);

So this will be more easy create the mocked response, knowing what is required and what is not.

Thank!

@danielvanmil
Copy link

danielvanmil commented Nov 27, 2023

+1

For now we use https://the-guild.dev/graphql/codegen/plugins/typescript/typed-document-node and created a helper function like:

export function getLaikaMatcher<D, V>(query: ComponentDocument<D, V>): Matcher {
  return {operationName: (query.definitions[0] as OperationDefinitionNode).name.value};
}

and then use the generated typed document (GetAllAccountsDocument in this case):

laika.intercept(getLaikaMatcher(GetAllAccountsDocument)).mockResult(() => {
  const result: Data<typeof GetAllAccountsDocument> = {allAccounts: accounts};
  return {
    result: {data: result},
  };
});

This provides at least type safety when our API changes etc.

It would be nice when Laika could add the generics to the API so we can use typed-document-node without custom helper functions etc.

@niieani
Copy link
Contributor

niieani commented Nov 28, 2023

Good suggestion!
PRs welcome.

@bbrzoska
Copy link
Collaborator

bbrzoska commented May 6, 2024

Adding one more suggestion: it would be nice if we could provide the operation document itself as the argument, i.e.:

laika.intercept({operation: QueryDocument})

@bbrzoska bbrzoska added good first issue Good for newcomers help wanted Extra attention is needed labels May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants