Graphql-code-generator: __isTypeOf not available on generated GraphQL type.

Created on 8 Jan 2020  路  5Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

We are using TypeScript and our schema uses interfaces and unions. We need to add a resolver for __isTypeOf in our resolver for the two types PlaneSearch and PlaneCreate that are part of the union SearchData. The problem is that the generated type for Resolvers does not include a property __isTypeOf. Are we doing something terribly wrong or is there another recommended way to handle the resolving of __isTypeOf?

To Reproduce
Steps to reproduce the behavior:

  1. My GraphQL schema:
type Query {
  search: [SearchResult]
}

type SearchResult {
    data: SearchData
}

union SearchData = PlaneSearch | PlaneCreate

type PlaneSearch {
    ...properties
}

type PlaneCreate {
    ...properties
}
  1. My resolver:
import { Resolvers } from "../gql.types.generated";

const resolvers: Resolvers = {
  PlaneSearch: {
    __isTypeOf(obj) {
      return obj.dataType === "PlaneSearch";
    }
  },
  PlaneCreate: {
    __isTypeOf(obj) {
      return obj.anotherType === "Something";
    }
  }
};

Expected behavior
Expected the type PlaneSearch and PlaneCreate to include all of our own properties and an __isTypeOf property.

Environment:

  • "@graphql-codegen/add": "1.9.1"
  • "@graphql-codegen/cli": "1.9.1"
  • "@graphql-codegen/schema-ast": "1.9.1"
  • "@graphql-codegen/typescript": "1.9.1"
  • "@graphql-codegen/typescript-operations": "1.9.1"
  • "@graphql-codegen/typescript-react-apollo": "1.9.1"
  • "@graphql-codegen/typescript-resolvers": "1.9.1"
enhancement plugins waiting-for-release

All 5 comments

@andrzey Can you please share a documentation regarding __isTypeOf? I think using __resolveType should be simpler to identify types for interface/union.

From the grahql documentation:

type GraphQLObjectTypeConfig = {
  name: string;
  interfaces?: GraphQLInterfacesThunk | Array<GraphQLInterfaceType>;
  fields: GraphQLFieldConfigMapThunk | GraphQLFieldConfigMap;
  isTypeOf?: (value: any, info?: GraphQLResolveInfo) => boolean;
  description?: ?string
}

We probably could use __resolveType and resolve each type of SearchData in the resolver for SearchData, but if we have an union of possibly 10 or more types that would create a huge if-else block inside the resolver for SearchData. We wanted that each type in SearchData could resolve its own type in their respective resolver.

I added this in: https://github.com/dotansimha/graphql-code-generator/pull/3405 (it's being applied only for type resolvers, excluding unions, interfaces and root types).

Thanks @andrzey , can you please try the latest alpha for the PR? 1.11.3-alpha-f020cd9c.106

Available in v1.12.0

Sorry for the late reply. Version 1.11.3-alpha-f020cd9c.106 worked perfectly!

Was this page helpful?
0 / 5 - 0 ratings