-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[resolvers][federation] Fix mapper being incorrectly used as the base type for reference #10216
Open
eddeee888
wants to merge
8
commits into
federation-fixes
Choose a base branch
from
fix-federation-mapper
base: federation-fixes
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
661e039
Fix reference being assigned mappers incorrectly
eddeee888 64ab400
Add test for federation mappers usage in reference
eddeee888 9273310
Add changeset
eddeee888 c932044
Remove extraneous UnwrappedObject type
eddeee888 bab88db
Change to major because it may break existing use cases
eddeee888 c3a9410
Run CI on federation-fixes feature branch
eddeee888 9d1df52
Update dev tests
eddeee888 a784d3b
Clean up tests
eddeee888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@graphql-codegen/visitor-plugin-common': patch | ||
'@graphql-codegen/typescript-resolvers': patch | ||
'@graphql-codegen/plugin-helpers': patch | ||
--- | ||
|
||
Fix `mappers` usage with Federation | ||
|
||
`mappers` was previously used as `__resolveReference`'s first param (usually called "reference"). However, this is incorrect because `reference` interface comes directly from `@key` and `@requires` directives. This patch fixes the issue by creating a new `FederationTypes` type and use it as the base for federation entity types when being used to type entity references |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,13 +106,6 @@ export type ResolverWithResolve<TResult, TParent, TContext, TArgs> = { | |
const stitchingResolverUsage = `StitchingResolver<TResult, TParent, TContext, TArgs>`; | ||
|
||
if (visitor.hasFederation()) { | ||
if (visitor.config.wrapFieldDefinitions) { | ||
defsToInclude.push(`export type UnwrappedObject<T> = { | ||
[P in keyof T]: T[P] extends infer R | Promise<infer R> | (() => infer R2 | Promise<infer R2>) | ||
? R & R2 : T[P] | ||
};`); | ||
Comment on lines
-110
to
-113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a given type: export type ProductMapper = {
id: () => Promise<string>;
}; This type seems to return something like Regardless, since we move from |
||
} | ||
|
||
defsToInclude.push( | ||
`export type ReferenceResolver<TResult, TReference, TContext> = ( | ||
reference: TReference, | ||
|
@@ -244,6 +237,7 @@ export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs | |
) => TResult | Promise<TResult>; | ||
`; | ||
|
||
const federationTypes = visitor.buildFederationTypes(); | ||
const resolversTypeMapping = visitor.buildResolversTypes(); | ||
const resolversParentTypeMapping = visitor.buildResolversParentTypes(); | ||
const resolversUnionTypesMapping = visitor.buildResolversUnionTypes(); | ||
|
@@ -287,6 +281,7 @@ export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs | |
prepend, | ||
content: [ | ||
header, | ||
federationTypes, | ||
resolversUnionTypesMapping, | ||
resolversInterfaceTypesMapping, | ||
resolversTypeMapping, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
packages/plugins/typescript/resolvers/tests/ts-resolvers.federation.mappers.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import '@graphql-codegen/testing'; | ||
import { codegen } from '@graphql-codegen/core'; | ||
import { parse } from 'graphql'; | ||
import { TypeScriptResolversPluginConfig } from '../src/config.js'; | ||
import { plugin } from '../src/index.js'; | ||
|
||
function generate({ schema, config }: { schema: string; config: TypeScriptResolversPluginConfig }) { | ||
return codegen({ | ||
filename: 'graphql.ts', | ||
schema: parse(schema), | ||
documents: [], | ||
plugins: [{ 'typescript-resolvers': {} }], | ||
config, | ||
pluginMap: { 'typescript-resolvers': { plugin } }, | ||
}); | ||
} | ||
|
||
describe('TypeScript Resolvers Plugin + Apollo Federation - mappers', () => { | ||
it('generates FederationTypes and use it for reference type', async () => { | ||
const federatedSchema = /* GraphQL */ ` | ||
type Query { | ||
me: User | ||
} | ||
|
||
type User @key(fields: "id") { | ||
id: ID! | ||
name: String | ||
} | ||
|
||
type UserProfile { | ||
id: ID! | ||
user: User! | ||
} | ||
`; | ||
|
||
const content = await generate({ | ||
schema: federatedSchema, | ||
config: { | ||
federation: true, | ||
mappers: { | ||
User: './mappers#UserMapper', | ||
}, | ||
}, | ||
}); | ||
|
||
// User should have it | ||
expect(content).toMatchInlineSnapshot(` | ||
"import { GraphQLResolveInfo } from 'graphql'; | ||
import { UserMapper } from './mappers'; | ||
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | ||
|
||
|
||
export type ResolverTypeWrapper<T> = Promise<T> | T; | ||
|
||
export type ReferenceResolver<TResult, TReference, TContext> = ( | ||
reference: TReference, | ||
context: TContext, | ||
info: GraphQLResolveInfo | ||
) => Promise<TResult> | TResult; | ||
|
||
type ScalarCheck<T, S> = S extends true ? T : NullableCheck<T, S>; | ||
type NullableCheck<T, S> = Maybe<T> extends T ? Maybe<ListCheck<NonNullable<T>, S>> : ListCheck<T, S>; | ||
type ListCheck<T, S> = T extends (infer U)[] ? NullableCheck<U, S>[] : GraphQLRecursivePick<T, S>; | ||
export type GraphQLRecursivePick<T, S> = { [K in keyof T & keyof S]: ScalarCheck<T[K], S[K]> }; | ||
|
||
|
||
export type ResolverWithResolve<TResult, TParent, TContext, TArgs> = { | ||
resolve: ResolverFn<TResult, TParent, TContext, TArgs>; | ||
}; | ||
export type Resolver<TResult, TParent = {}, TContext = {}, TArgs = {}> = ResolverFn<TResult, TParent, TContext, TArgs> | ResolverWithResolve<TResult, TParent, TContext, TArgs>; | ||
|
||
export type ResolverFn<TResult, TParent, TContext, TArgs> = ( | ||
parent: TParent, | ||
args: TArgs, | ||
context: TContext, | ||
info: GraphQLResolveInfo | ||
) => Promise<TResult> | TResult; | ||
|
||
export type SubscriptionSubscribeFn<TResult, TParent, TContext, TArgs> = ( | ||
parent: TParent, | ||
args: TArgs, | ||
context: TContext, | ||
info: GraphQLResolveInfo | ||
) => AsyncIterable<TResult> | Promise<AsyncIterable<TResult>>; | ||
|
||
export type SubscriptionResolveFn<TResult, TParent, TContext, TArgs> = ( | ||
parent: TParent, | ||
args: TArgs, | ||
context: TContext, | ||
info: GraphQLResolveInfo | ||
) => TResult | Promise<TResult>; | ||
|
||
export interface SubscriptionSubscriberObject<TResult, TKey extends string, TParent, TContext, TArgs> { | ||
subscribe: SubscriptionSubscribeFn<{ [key in TKey]: TResult }, TParent, TContext, TArgs>; | ||
resolve?: SubscriptionResolveFn<TResult, { [key in TKey]: TResult }, TContext, TArgs>; | ||
} | ||
|
||
export interface SubscriptionResolverObject<TResult, TParent, TContext, TArgs> { | ||
subscribe: SubscriptionSubscribeFn<any, TParent, TContext, TArgs>; | ||
resolve: SubscriptionResolveFn<TResult, any, TContext, TArgs>; | ||
} | ||
|
||
export type SubscriptionObject<TResult, TKey extends string, TParent, TContext, TArgs> = | ||
| SubscriptionSubscriberObject<TResult, TKey, TParent, TContext, TArgs> | ||
| SubscriptionResolverObject<TResult, TParent, TContext, TArgs>; | ||
|
||
export type SubscriptionResolver<TResult, TKey extends string, TParent = {}, TContext = {}, TArgs = {}> = | ||
| ((...args: any[]) => SubscriptionObject<TResult, TKey, TParent, TContext, TArgs>) | ||
| SubscriptionObject<TResult, TKey, TParent, TContext, TArgs>; | ||
|
||
export type TypeResolveFn<TTypes, TParent = {}, TContext = {}> = ( | ||
parent: TParent, | ||
context: TContext, | ||
info: GraphQLResolveInfo | ||
) => Maybe<TTypes> | Promise<Maybe<TTypes>>; | ||
|
||
export type IsTypeOfResolverFn<T = {}, TContext = {}> = (obj: T, context: TContext, info: GraphQLResolveInfo) => boolean | Promise<boolean>; | ||
|
||
export type NextResolverFn<T> = () => Promise<T>; | ||
|
||
export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs = {}> = ( | ||
next: NextResolverFn<TResult>, | ||
parent: TParent, | ||
args: TArgs, | ||
context: TContext, | ||
info: GraphQLResolveInfo | ||
) => TResult | Promise<TResult>; | ||
|
||
/** Mapping of federation types */ | ||
export type FederationTypes = { | ||
User: User; | ||
}; | ||
|
||
|
||
|
||
/** Mapping between all available schema types and the resolvers types */ | ||
export type ResolversTypes = { | ||
Query: ResolverTypeWrapper<{}>; | ||
User: ResolverTypeWrapper<UserMapper>; | ||
ID: ResolverTypeWrapper<Scalars['ID']['output']>; | ||
String: ResolverTypeWrapper<Scalars['String']['output']>; | ||
UserProfile: ResolverTypeWrapper<Omit<UserProfile, 'user'> & { user: ResolversTypes['User'] }>; | ||
Boolean: ResolverTypeWrapper<Scalars['Boolean']['output']>; | ||
}; | ||
|
||
/** Mapping between all available schema types and the resolvers parents */ | ||
export type ResolversParentTypes = { | ||
Query: {}; | ||
User: UserMapper; | ||
ID: Scalars['ID']['output']; | ||
String: Scalars['String']['output']; | ||
UserProfile: Omit<UserProfile, 'user'> & { user: ResolversParentTypes['User'] }; | ||
Boolean: Scalars['Boolean']['output']; | ||
}; | ||
|
||
export type QueryResolvers<ContextType = any, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = { | ||
me?: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType>; | ||
}; | ||
|
||
export type UserResolvers<ContextType = any, ParentType extends ResolversParentTypes['User'] = ResolversParentTypes['User'], FederationType extends FederationTypes['User'] = FederationTypes['User']> = { | ||
__resolveReference?: ReferenceResolver<Maybe<ResolversTypes['User']>, { __typename: 'User' } & GraphQLRecursivePick<FederationType, {"id":true}>, ContextType>; | ||
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>; | ||
name?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>; | ||
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>; | ||
}; | ||
|
||
export type UserProfileResolvers<ContextType = any, ParentType extends ResolversParentTypes['UserProfile'] = ResolversParentTypes['UserProfile']> = { | ||
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>; | ||
user?: Resolver<ResolversTypes['User'], ParentType, ContextType>; | ||
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>; | ||
}; | ||
|
||
export type Resolvers<ContextType = any> = { | ||
Query?: QueryResolvers<ContextType>; | ||
User?: UserResolvers<ContextType>; | ||
UserProfile?: UserProfileResolvers<ContextType>; | ||
}; | ||
|
||
" | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
FederationTypes
type will be used by federation entities to refer to the base types, similar to other types likeResolversParentTypes
andResolversTypes