Skip to content

Union Type Problem #2041

Closed
Closed
@KostaProsenikov

Description

@KostaProsenikov

Hi Guys, I have a question.

I have a union field type:

defined here:

union FloorType = IntBox | StringBox

Also I have the two types:

type IntBox {
  value: Int
}

type StringBox {
  field: String
}

And then finally in the resolver I have the code to resolve the type:

const resolvers: IResolvers = {
  IntBox: {
    __isTypeOf(numberField: number) {
      console.log('number is', numberField);
      const newObj = {};
      newObj['value'] = numberField;
      return newObj;
    },
  },
  StringBox: {
    __isTypeOf(object: 'string') {
      const newObj = {};
      newObj['field'] = object;
      return newObj;
    },
  },
  FloorType: {
    __resolveType(obj: any) {
      if (typeof obj === 'string') {
        return 'StringBox';
      }

      if (typeof obj === 'number') {
        return 'IntBox';
      }
    },
  },
};

However when I make the query:

 aparts(page:1, newest: false, limit: 2) {
   name
    price
    currency
    squares
    floor {
      __typename
      ... on StringBox {field}
      ... on IntBox { value }
    }
    created
    link
    _id
  }

I receive the following input:

"floor": { "__typename": "IntBox", "value": null },

And it should be for instance

floor: { value: 4 }

Can you give any advise how to receive correct value?

My data looks like this:

{
"_id": "5d30231107766daaa23253bd",
"name": "Name 123",
"currency": "EUR",
"price": 649,
"city": "Sofia",
"district": "Geo Milev",
"type": "1-Bedroom",
"firstOrLast": true,
"floor": 1,
"totalFloors": 4,
"squares": 65,
"created": "2019-07-18T10:43:13.313Z"
},

Originally posted by @KostaProsenikov in graphql/graphql-spec#236 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions