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

Doesn't work with aliases. #167

Open
MarkLyck opened this issue May 20, 2022 · 2 comments
Open

Doesn't work with aliases. #167

MarkLyck opened this issue May 20, 2022 · 2 comments

Comments

@MarkLyck
Copy link

MarkLyck commented May 20, 2022

This mocking library is fantastic (although it sadly looks abandoned?), non-the-less it's the only functional dynamic mocks library I've found that still works for the most part.

I did find that it has issues with queries that use aliases though.

Take this query which works just fine.

query SiteCounts {
    crm {
      sitesConnection(condition: { active: true }, first: 1) {
        totalCount
      }
    }
  }

I can mock it lik this:

const mocks = {
      SiteCounts: () => ({
        crm: {
          sitesConnection: {
            totalCount: 1,
          },
        },
      }),
    }

but if I add an alias to it like this:

query SiteCounts {
    crm {
      sites: sitesConnection {
        totalCount
      }
      activeSites: sitesConnection(condition: { active: true }, first: 1) {
        totalCount
      }
    }
  }

I can no longer mock it :(

const mocks = {
      SiteCounts: () => ({
        crm: {
          activeSites: {
            totalCount: 1,
          },
        },
      }),
    }

Doesn't work, and the original sitesConnection mock would override both parts of the query.

I haven't found any other issues, or any mention in the code of alias so I assume this was just never implemented.

But with the last update over 9 months ago, I'm guessing this project is dead? But I thought I'd create the ticket anyway just in case.

@canac
Copy link

canac commented Feb 2, 2024

It's not pretty, but I found this workaround.

const mocks = {
  SiteCounts: () => ({
    crm: {
      sitesConnect: (_root, _args, _context, info) => {
        if (info.path.key === 'sites') {
          return { totalCount: 2 };
        } else if (info.path.key === 'activeSites') {
          return { totalCount: 1 };
        }
      }),
    },
  }),
};

@MarkLyck
Copy link
Author

MarkLyck commented Apr 4, 2024

Funny enough I ran into this issue again 2 years later and found my old ticket, Thanks @canac that worked!

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