graphql-nexus / nexus

Code-First, Type-Safe, GraphQL Schema Construction
https://nexusjs.org
MIT License
3.4k stars 275 forks source link

isTypeOf malfunctioning for Interfaces #1167

Closed jefersoneiji closed 11 months ago

jefersoneiji commented 1 year ago

Hello! Thank you Nexus team for creating this library that's realy helpful for working with GraphQL.

I'm using typescript, apollo server and nexus graphql.

By following the GraphQL specs on interfaces an issue on the type level occurs. type_error

But if i change from data.brand to one of fields from Character Interface only character fields are returned.

And this query stops working by no longer returning the brand field only name field.

query HeroForEpisode($ep: Episode!) {
  hero(episode: $ep) {
    name
    ... on Droid {
      brand
    }
  }
}

The Interface

const Character = interfaceType({
    name: 'Character',
    definition(t) {
        t.nonNull.id('id');
        t.nonNull.string('name');
        t.nonNull.int('age');
    },
});

Human Object Type

const Human = objectType({
    name: 'Human',
    description: 'Human type of character',
    definition(t) {
        t.implements('Character');
        t.nonNull.id('id');
        t.nonNull.string('name');
        t.nonNull.int('age');
        t.nonNull.string('country');
    },
    isTypeOf(data) {
        return Boolean(data.country);
    }
});

Droid Object Type

const Droid = objectType({
    name: 'Droid',
    description: 'Droid type of character',
    definition(t) {
        t.implements('Character');
        t.nonNull.id('id');
        t.nonNull.string('name');
        t.nonNull.int('age');
        t.nonNull.string('brand');
    },
    isTypeOf(data) {
        return Boolean(data.brand);
    }
});

Hero Query

const Hero = extendType({
    type: 'Query',
    definition(t) {
        t.field('hero', {
            type: 'Character',
            args: { id: idArg() },
            description: 'Query for characters',
            resolve(parent, args, context) {
                return expectedResult;
            }
        });
    },
});