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

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

Closed
nicky-lenaers opened this issue Mar 7, 2018 · 2 comments

Comments

@nicky-lenaers
Copy link

nicky-lenaers commented Mar 7, 2018

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
  }
});
@IvanGoncharov
Copy link
Member

@nicky-lenaers You still can return object types if you would write:

export const ProjectPrivateType = new GraphQLObjectType({
  name: 'ProjectPrivateType',
  interfaces: () => [ProjectInterfaceType],
});

Arrow function will break circular reference.

In general, it's circular dependency in JS that means the only thing that could be improved in GraphQL is producing a proper error message.

@nicky-lenaers
Copy link
Author

@IvanGoncharov That's a shame! Your solution for the arrow function works, so thanks a lot! I've spent hours trying refactored version of the codebase, didn't come up with this.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants