Graphql-code-generator: federation bug: Cannot find name 'ReferenceResolver'.

Created on 3 Oct 2019  路  13Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

When federation is enabled, you now get the error: Cannot find name 'ReferenceResolver'.

To Reproduce
Steps to reproduce the behavior:

Edit graphql-codegen-issue-template

  1. run yarn generate
  2. run yarn typecheck

My GraphQL schema:

type Query {
    user(id: ID!): User!
}

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

My codegen.yml config file:

schema: schema.graphql
generates:
  types.ts:
    plugins:
      - typescript
      - typescript-resolvers
    config:
      federation: true

Expected behavior

It should output the type, https://github.com/dotansimha/graphql-code-generator/blob/587599f47336afff05e42f2cd11c74c69bd5ab40/packages/plugins/typescript/resolvers/src/index.ts#L90-L96 depends on a property that doesn't get set until after the rest of the code is generated.

bug plugins waiting-for-release

All 13 comments

@ForbesLindesay Can you please try to move the federation config upper?

schema: schema.graphql
config:
  federation: true
generates:
  types.ts:
    plugins:
      - typescript
      - typescript-resolvers

@kamilkisiela

@dotansimha, I see the same bug with v1.8. moving config federation to upper didn't help

@kamilkisiela can you please take a look? 鉂わ笍

I have the same issue, running codegen v1.8.1
even after moving the config upper, it doesn't help

The workaround that I have now is to declare it in global.d.ts

import {GraphQLResolveInfo} from 'graphql';

declare global {
  type ReferenceResolver<TResult, TReference, TContext> = (
    reference: TReference,
    context: TContext,
    info: GraphQLResolveInfo
  ) => Promise<TResult> | TResult;
}

====
Hmm, the type of generated ReferenceResolver doesn't look correct to me according to Apollo Federation documentation: https://www.apollographql.com/docs/apollo-server/api/apollo-federation/#__resolvereference

somehow @ruanyl's fix didn't work consistently.
Currently using a slightly modified approach:

plugins:
  - add: 'import {ReferenceResolver} from "./fix";'
  - typescript
  - typescript-resolvers

with fix.ts:
```import { GraphQLResolveInfo } from "graphql";

export type ReferenceResolver = (
reference: TReference,
context: TContext,
info: GraphQLResolveInfo
) => Promise | TResult;
```

@kamilkisiela fixed it in: https://github.com/dotansimha/graphql-code-generator/pull/2760

Alpha available: 1.8.2-alpha-f9569d0c.47, can you please try it? @ForbesLindesay @lkleuver @ruanyl

@dotansimha tried it and seems to work :D awesome!
"@graphql-codegen/typescript-resolvers": "1.8.2-alpha-f9569d0c.47",

Fixed in 1.8.2.

I'm sorry, I'm am on 1.11.2 and the ReferenceResolver doesn't get included, wherever I put the federation: true:

overwrite: true
schema: "http://localhost:4001"
config:
  federation: true
#documents: "**/*.graphql"
generates:
  "@types/graphql.d.ts":
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-resolvers"
      - "typescript-graphql-files-modules"

EDIT: I believe the underlying issue has nothing to do with graphql-code-generator, but rather with the way my schema is created. It is being built by merge-graphql-schemas and fed into buildFederatedSchema. Somewhere along the way the directives are lost, hence nothing to be generated. Manually adding the directives on types does generate the necessary codes, all is fine. Sorry for the confusion.

Hi @dotansimha, it turns out it is an issue during schema download. The federated service has its sdl including the directives in

{
  _service { sdl }
}

but the directives are not not included in the schema which is displayed in the playground. I'm using [email protected], is this version supported? It seems to me that codegen downloads the "wrong" schema.

@nadilas If it's still relevant, can you please open a new issue with an example?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mszczepanczyk picture mszczepanczyk  路  3Comments

dotansimha picture dotansimha  路  3Comments

mdaouas picture mdaouas  路  3Comments

NickClark picture NickClark  路  3Comments

fvisticot picture fvisticot  路  3Comments