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

Mocking: Resolving a union throws an error #2382

Closed
nemonemi opened this issue Jun 29, 2021 · 8 comments
Closed

Mocking: Resolving a union throws an error #2382

nemonemi opened this issue Jun 29, 2021 · 8 comments
Labels
bug Something isn't working

Comments

@nemonemi
Copy link
Contributor

nemonemi commented Jun 29, 2021

Describe the bug

Without the union return type, everything works, but when having a union as a return type it throws an error.

To Reproduce
Steps to reproduce the behavior:

  1. Clone the https://github.com/nemonemi/mesh_gateway (yarn berry)
  2. Install dependencies
  3. Run yarn start
  4. Visit http://localhost:4000
  5. Use the uploadFile mutation. -> it works
  6. Use the uploadFileWithUnion mutation. -> throws an error
"message": "Abstract type \"UploadFileResult\" must resolve to an Object type at runtime for field \"Mutation.uploadFileWithUnion\". Either the \"UploadFileResult\" type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function.",

This is the resolver:

export default {
  uploadFile: () => {
    return {
      __typename: 'UploadFile',
      fileID: datatype.uuid()
    }
  },
  uploadFileWithUnion: (_, {value}) => {
    if (value !== 'valid') {
      return {
        __typename: 'WrongFileTypeError',
        message: 'Nope...'
      }
    }
    return {
      __typename: 'UploadFile',
      fileID: datatype.uuid()
    }
  }
};

Expected behavior

It should work.

Question
It suggest using the resolveType or isTypeOf. How can one provide those methods from within the graphql-mesh resolvers?

@nemonemi nemonemi changed the title Union types not working How to resolve a union? Dec 3, 2021
@nemonemi nemonemi changed the title How to resolve a union? Mocking: Resolving a union throws an error? Dec 5, 2021
@nemonemi nemonemi changed the title Mocking: Resolving a union throws an error? Mocking: Resolving a union throws an error Dec 5, 2021
@nemonemi nemonemi reopened this Dec 5, 2021
@dotansimha dotansimha added the bug Something isn't working label Dec 26, 2021
@dotansimha
Copy link
Collaborator

@ardatan thoughts?

@paradigm314
Copy link

@ardatan @dotansimha

I am getting a similar issue attempting to replace an openapi handler field that is generated as JSON with an Interface.

Generated Source Type

OrganizationsOrganizationProfiles {
  data: [JSON]
}

Replace-Field Transform

- replace-field:
     typeDefs: ./src/organizationsTypeDefs.graphql
     replacements:
       - from:
            type: OrganizationsOrganizationProfiles
            field: data
          to:
            type: OrganizationProfilesResponse
            field: data

organizationsTypeDefs

type OrganizationProfilesResponse {
    data: [OrganizationProfile]
}

interface OrganizationProfile {
    id: String
    organizationId: String
    type: String
}

type LocationProfile implements OrganizationProfile {
    id: String
    organizationId: String
    type: String
    status: Status2
    address_line_1: String
    address_line_2: String
    city: String
    state: String
    postal_code: String
    country: String
    timezone: String
    latitude: Int
    longitude: Int
}

type VideoContentProfile implements OrganizationProfile {
    id: String
    organizationId: String
    type: String
    status: Status2
    maxContent: Int
    scope: String
}

The typing works correctly but when it's resolved throws the same error.

Abstract type "OrganizationProfile" must resolve to an Object type at runtime for field "OrganizationsOrganizationProfiles.data". Either the "OrganizationProfile" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.

I have tried adding the __resolveType via both addtionalResolvers and extend transform but the error remains.

Attempted Additional Resolver

import { Resolvers } from '@mesh/index';

export const resolvers: Resolvers = {
  OrganizationProfile: {
    __resolveType: (obj, context, info) => {

      if (obj.type === 'organizations.organization_profiles.VideoContentProfile') {
        return 'VideoContentProfile'
      }

      if (obj.type === 'organizations.organization_profiles.LocationProfile') {
        return 'LocationProfile'
      }

      return null
    }
  },
};

@caribouflex
Copy link

Hello folks!

So it appears I got an issue somehow similar. All my types are well generated, but when trying to resolve the mutation I get the same error as you guys mentioned.

In my configuration I have an additional resolver added, so I tried to see if I added a custom __resolveType function for my type if would work but it doesn't seem to override the existing.

So I was wondering if someone found a solution, or if you know a way to override the __resolveType function maybe this would be a way to fix this issue.

gqlUnionIssue

errorMessage_union

Thank you! 🙏

@nemonemi
Copy link
Contributor Author

I have to say, I am pretty disappointed in how these tickets get addressed, let alone resolved.

@ardatan
Copy link
Owner

ardatan commented Jul 24, 2022

I have to say, I am pretty disappointed in how these tickets get addressed, let alone resolved.

Would you give a try to create a failing test in a PR or maybe get some progress towards fixing it
With all the stuff you provided so far; first we need to clone your project and install it in our local, then try to reproduce the same thing by assuming the problem is not the environment(which usually is). But instead, if you use CodeSandbox or StackBlitz, we can make sure that it is not environment but the library itself.
If you want to get this fixed quicker, you can create a PR with a faling test so anyone else from the community or a Guild member can use it to debug easier.
The best thing in OSS projects is to develop these projects together with the users, As a maintainer of this project, I'm really sad to hear that you're disappointed but I also feel same because those GitHub issues seem like "support tickets" rather than an opportunity to take and improve the project for everyone else in the community.

@theguild-bot theguild-bot mentioned this issue Aug 11, 2022
@ardatan ardatan closed this as completed Sep 5, 2022
@theguild-bot theguild-bot mentioned this issue Sep 28, 2023
@james-butter
Copy link

@nemonemi did you ever find a solution to this?

@nemonemi
Copy link
Contributor Author

Hi @james-butter, unfortunately, I have to say that I've decided to stop using graphql-mash. I was very excited about it, and after lots of issues, and not-so-responsive support, I gave up.

@james-butter
Copy link

james-butter commented Mar 12, 2024

Damn, that's a shame. There does seem to be so much magic going on behind the scenes and I'm really unsure on how to get the union resolver going here. Hope you went onto a framework that you love!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants