Skip to content

GraphQLInterfaceType: TypeError: Cannot read property 'name' of undefined #1279

Closed
@nicky-lenaers

Description

@nicky-lenaers

I have two types that implement an interface:

1. project.type.ts

import {
  GraphQLObjectType
} from 'graphql';

import { ProjectFields } from './project.fields';
import { ProjectInterfaceType } from './project-interface.type';

export const ProjectType: GraphQLObjectType = new GraphQLObjectType({
  name: 'ProjectType',
  interfaces: [ProjectInterfaceType],
  description: 'GraphQL Type for Project',
  fields: ProjectFields
});

2. project-private.type.ts

import {
  GraphQLObjectType
} from 'graphql';

import { ProjectPrivateFields } from './project.fields';
import { ProjectInterfaceType } from './project-interface.type';

export const ProjectPrivateType: GraphQLObjectType = new GraphQLObjectType({
  name: 'ProjectPrivateType',
  interfaces: [ProjectInterfaceType],
  description: 'Project Private Type',
  fields: ProjectPrivateFields
});

project-interface.type.ts

import {
  GraphQLInterfaceType
} from 'graphql';

import { ProjectFields, ProjectPrivateFields } from './project.fields';
import { ProjectType } from './project.type';
import { ProjectPrivateType } from './project-private.type';

export const ProjectInterfaceType: GraphQLInterfaceType = new GraphQLInterfaceType({
  name: 'ProjectInterfaceType',
  fields: ProjectFields,
  resolveType: (data, context) => {
    return ProjectType; // <-- I need to conditionally return either ProjectType or ProjectPrivateType
  }
});

I also have the types declared in my schema:

...
export const ProjectSchema = new GraphQLSchema({
  types: [ProjectPrivateType, ProjectType, ProjectInterfaceType],
  ...
...

Whenever I change the return type from the resolveType from ProjectType to ProjectPrivateType I get the following error:

TypeError: Cannot read property 'name' of undefined
    at ...\node_modules\graphql\type\schema.js:136:52
    at Array.forEach (<anonymous>)
    at ...\node_modules\graphql\type\schema.js:135:30
    at Array.forEach (<anonymous>)
    at new GraphQLSchema (...\node_modules\graphql\type\schema.js:132:32)
    ...

Update

I found out that it seems like some circular reference not being resolved. If I simply return the name of the type as a string, it works:

import {
  GraphQLInterfaceType
} from 'graphql';

import { ProjectFields, ProjectPrivateFields } from './project.fields';
import { ProjectType } from './project.type';
import { ProjectPrivateType } from './project-private.type';

export const ProjectInterfaceType: GraphQLInterfaceType = new GraphQLInterfaceType({
  name: 'ProjectInterfaceType',
  fields: ProjectFields,
  resolveType: (data, context) => {
    return 'ProjectPrivateType'; // <-- this actually works
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions