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

Federation with custom mappers #5646

Open
Tracked by #8296 ...
ftognetto opened this issue Mar 3, 2021 · 3 comments
Open
Tracked by #8296 ...

Federation with custom mappers #5646

ftognetto opened this issue Mar 3, 2021 · 3 comments
Labels
kind/enhancement New feature or request plugins

Comments

@ftognetto
Copy link

Is your feature request related to a problem? Please describe.

Hello, i'm using codegen in a federated project with custom mappers.
On db side my "id" field is _id (i'm using mongo) so i set up the db instance like this

export interface UserDb {
    _id: ObjectId;
    name: string;
    email?: string;
}

In schema:

type User @key(fields: "id") {
  id: ID!
  name: String!
  email: String
}

And in codegen i added this class as a mapper

overwrite: true
schema: "src/schema/schema.ts"
documents: null
generates:
  src/generated/graphql.ts:
    plugins:
      - typescript
      - typescript-resolvers
    config:
      federation: true
      contextType: ../context#MyCtx
      maybeValue: T | undefined
      mappers:
        User: ../db/user_db#UserDb
  ./graphql.schema.json:
    plugins:
      - "introspection"

But this is generating for user resolver

export type UserResolvers<ContextType = MyCtx, ParentType extends ResolversParentTypes['User'] = ResolversParentTypes['User']> = {
  __resolveReference?: ReferenceResolver<Maybe<ResolversTypes['User']>, { __typename: 'User' } & GraphQLRecursivePick<ParentType, {"id":true}>, ContextType>;
  id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>;
  name?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
  email?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
  __isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

So in this line
__resolveReference?: ReferenceResolver<Maybe<ResolversTypes['User']>, { __typename: 'User' } & GraphQLRecursivePick<ParentType, {"id":true}>, ContextType>; is searching "id" inside UserDb interface which is not present because in my interface is called "_id"

Is this customizable? Thank you!

@quant-daddy
Copy link

I am facing a similar problem. I think the problem is in this line: GraphQLRecursivePick<ParentType, {"id":true}
The parent parameter of __resolverReference function should just be an object with key value pairs corresponding to the key of the federation and the return type should be the mapper of the object (if there's one). This might be related to #4724?

@dotansimha
Copy link
Owner

This is totally related to #4724 . Please note that @kamilkisiela is doing some work at the moment to improve the integration with Federation (see #5645)

@MarcBridner
Copy link

Also running into this, GraphQLRecursivePick<> type is looking at my mapped classes, and they don't have the schema that's in @keys on purpose.

For example, I have a schema like

type Attribute
  @key(fields: "name namespace { prefix }") {
  name: String!
  namespace: Namespace!
}

type Namespace
  @key(fields: "prefix") {
  prefix: String!
}

I have mapped types named MyAttribute and MyNamespace that look like

class MyAttribute {
  name: string
  __namespacePrefix: string
}

class Namespace {
 prefix: string
}

I have to modify MyAttribute to look like:

class MyAttribute {
  name: string
  namespace: {
    prefix: string
  }
}

For the reference resolver to work. It's a bit annoying and I may have to disable federation in that plugin and try to write my own plugin to compensate (I don't even know if it's possible to modify the output of the typescript plugin with another plugin, haven't checked)

meiamsome added a commit to meiamsome/graphql-code-generator that referenced this issue Nov 28, 2023
These types come over from other servers, and so should use types from the output, not the internal side of the resolvers.
Fixes dotansimha#5646
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request plugins
Projects
None yet
Development

No branches or pull requests

4 participants