Describe the bug
When using typescript-resolvers with federation: true. The generated code for the resolver uses the following type for the resolver:
__resolveReference: ReferenceResolver<Maybe<IResolversTypes['User']>, { __typename: 'User' } & Pick<ParentType, 'id'>, ContextType>,
However, when combined with a mapped type, this code is incorrect. ParentType is the mapped object, which may not have an id property at all, or its id property may be a completely unrelated type.
To Reproduce
Steps to reproduce the behavior:
type User @key(fields: "id') {
id: ID!
name: String
}
codegen.yml config file:schema: "src/schema.ts"
documents: null
generates:
src/generated/schema.ts:
plugins:
- "typescript"
- "typescript-resolvers"
config:
avoidOptionals: true
immutableTypes: true
mappers:
# Map GraphQL types in your schema to typescript types that are returned by resolvers and passed to child resolvers.
User: ../user#User
useIndexSignature: true
typesPrefix: "I"
federation: true
export class User {
_id: string;
name: string | null;
}
export UserResolver: IUserResolvers = {
id: user => Buffer.from(`User-${user._id}`, "ascii").toString("base64"),
name: user => user.name,
__resolveReference: (user) => {
const decoded = new Buffer(user.id, "base64").toString("ascii");
if (!decoded.startsWith("User-")) {
throw new Error("Unexpected ID value.");
}
const userId = decoded.slice(5);
return mongodb.users.findOne({_id: userId}) as User;
},
};
Expected behavior
My code should type-check correctly.
Actual behavior
Type '"id"' does not satisfy the constraint 'keyof ParentType'.
Environment:
Additional context
I am going to make a sample repo for this shortly, because the code samples above are artificial extractions from a larger running project.
I have now created an testcase repository showing this issue: https://github.com/bsmedberg-xometry/graphql-codegen-issue-3207-demo
I think I can provide a patch, which would be to replace this:
__resolveReference: ReferenceResolver<Maybe<IResolversTypes['User']>, { __typename: 'User' } & Pick<ParentType, 'id'>, ContextType>,
with this:
__resolveReference: ReferenceResolver<Maybe<IResolversTypes['User']>, { __typename: 'User' } & { id: IResolversTypes['Int'] }, ContextType>,
@kamilkisiela you seem to be involved in most of the federation issues, can you confirm whether my proposal is reasonable?
Would appreciate a fix for this, as our generated code won't compile when using @key on types using mappers. Thanks for all the work done on this great tool.
I have come across this issue too. Any progress on a fix or preferred work around?
Most helpful comment
Would appreciate a fix for this, as our generated code won't compile when using
@keyon types using mappers. Thanks for all the work done on this great tool.