dotansimha / dotansimha/graphql-code-generator-community

The provided `baseOptions` for an operation are combined with `defaultOptions` only at the first level of nesting, overriding the pre-specified `defaultOptions.context`

Open
#399 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

### Which packages are impacted by your issue?

@graphql-codegen/typescript-react-apollo

### Describe the bug

There is the following configuration for the `typescript-react-apollo` plugin:
```json5
{
...
plugins: ["typescript-operations", "typescript-react-apollo"],
config: {
...
reactApolloVersion: 3,
defaultBaseOptions: {
context: {
backend: "myBackendName"
}
},
...
}
}
```

As you can see, the `defaultBaseOptions` property contains an object with a `context` property, which in turn contains a `backend` property.

This configuration generates the following code:

```ts
import { gql } from '@apollo/client';
import * as Apollo from '@apollo/client';
import * as Types from '../types.d.js';

const defaultOptions = { context: { backend: "myBackendName" } } as const;
export type HeroDetailsQueryVariables = Types.Exact<{
episode?: Types.InputMaybe;
}>;

export type HeroDetailsQuery = {
__typename?: 'Query';
hero?:
| { __typename?: 'Droid'; primaryFunction?: string | null; name: string }
| { __typename?: 'Human'; height?: number | null; name: string }
| null;
};

export const HeroDetailsDocument = gql`
query HeroDetails($episode: Episode) {
hero(episode: $episode) {
name
... on Human {
height
}
... on Droid {
primaryFunction
}
}
}
`;

/**
* __useHeroDetailsQuery__
*
* To run a query within a React component, call `useHeroDetailsQuery` and pass it any options that fit your needs.
* When your component renders, `useHeroDetailsQuery` returns an object from Apollo Client that contains loading, error, and data properties
* you can use to render your UI.
*
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
*
* @example
* const { data, loading, error } = useHeroDetailsQuery({
* variables: {
* episode: // value for 'episode'
* },
* });
*/
export function useHeroDetailsQuery(
baseOptions?: Apollo.QueryHookOptions,
) {
const options = {...defaultOptions, ...baseOptions}; // Pay attention to this line
return Apollo.useQuery(HeroDetailsDocument, options);
}
export function useHeroDetailsLazyQuery(
baseOptions?: Apollo.LazyQueryHookOptions,
) {
const options = {...defaultOptions, ...baseOptions}; // Pay attention to this line
return Apollo.useLazyQuery(
HeroDetailsDocument,
options,
);
}
export type HeroDetailsQueryHookResult = ReturnType;
export type HeroDetailsLazyQueryHookResult = ReturnType;
export type HeroDetailsQueryResult = Apollo.QueryResult<
HeroDetailsQuery,
HeroDetailsQueryVariables
>;
```

Thus, when I call the operation with the passed configuration overriding the `context` field:
```ts
useHeroDetailsLazyQuery({
context: {
anotherContextParam: "myAnotherContextParam"
},
});
```

The `backend: "myBackendName"` field defined in the plugin configuration will be removed and I will have to re-specify it in the passed `context` field when calling operation:
```ts
useHeroDetailsLazyQuery({
context: {
backend: "myBackendName",
anotherContextParam: "myAnotherContextParam"
},
});
```

### Your Example Website or App

nope

### Steps to Reproduce the Bug or Issue

nope

### Expected behavior

As a user, I expected that the `context` field passed into the operation parameters would not overwrite the `context` field defined in the plugin configuration, but would complement it.

When calling an operation:
```ts
useHeroDetailsLazyQuery({
context: {
anotherContextParam: "myAnotherContextParam"
},
});
```

The `context` field of the operation should be:
```ts
context: {
backend: "myBackendName",
anotherContextParam: "myAnotherContextParam"
},
```

### Screenshots or Videos

_No response_

### Platform

- OS: All of them
- NodeJS: 18.16.1
- `graphql` version: 16.8.0
- `@graphql-codegen/typescript-react-apollo` version: 3.3.7

### Codegen Config File

_No response_

### Additional context

I created this issue to properly format Pull Request with the addition of full deep configuration merging functionality.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.