2.39.0 • Published 1 year ago

@homebound/graphql-typescript-response-factories v2.39.0

Weekly downloads
654
License
-
Repository
-
Last release
1 year ago

graphql-typescript-response-factories

This graphql-code-generator plugin generates factory methods for the react-apollo MockedResponses that are used for testing client-side GraphQL code.

I.e. for a given query like:

query GetAuthorSummaries {
  authorSummaries {
    author {
      name
    }
  }
}

This plugin will generate a newGetAuthorSummariesResponse factory function:

export function newGetAuthorSummariesResponse(
  data: GetAuthorSummariesQuery | Error,
): MockedResponse<GetAuthorSummariesQueryVariables, GetAuthorSummariesQuery> {
  return {
    request: { query: GetAuthorSummariesDocument },
    result: { data: data instanceof Error ? undefined : data },
    error: data instanceof Error ? data : undefined,
  };
}

That you can use in a test like:

const response = newGetAuthorSummariesResponse({
  // Use newAuthorSummary from the graphql-typescript-factories project
  authorSummaries: [newAuthorSummary()],
});

// Something react-testing-library's render
const component = render(
  <MockedProvider mocks={[response]}>
    <YourComponent />
  </MockedProvider>
);

Or you can simulate an error with:

const response = newGetAuthorSummariesResponse(new Error("bad"));

For non-react-apollo-specific factories for the rest of your GraphQL schema's types, see the graphql-typescript-factories sister project.

Install

npm -i @homebound/graphql-typescript-response-factories

Add the plugin to your graphql-codegen.yml, i.e.:

overwrite: true
schema: ./schema.json
documents: src/**/*.graphql
generates:
  src/generated/graphql-types.tsx:
    plugins:
      - typescript
      - typescript-operations
      - typescript-react-apollo
      - "@homebound/graphql-typescript-factories"
      - "@homebound/graphql-typescript-response-factories"

License

MIT

2.34.0

1 year ago

2.39.0

1 year ago

2.36.0

1 year ago

2.35.0

1 year ago

2.38.0

1 year ago

2.37.0

1 year ago

2.32.0

2 years ago

2.31.0

2 years ago

2.33.0

2 years ago

2.30.0

2 years ago

2.29.0

2 years ago

2.28.0

3 years ago

2.26.0

3 years ago

2.25.0

3 years ago

2.24.0

4 years ago

1.23.0

5 years ago

1.22.0

5 years ago

1.21.0

5 years ago

1.19.0

5 years ago

1.18.0

5 years ago

1.17.0

5 years ago

1.15.0

5 years ago

1.14.0

5 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago