SurveyMonkey / graphql-ergonomock

Automatic mocking of GraphQL queries
MIT License
19 stars 5 forks source link

Doesn't work with aliases. #167

Open MarkLyck opened 2 years ago

MarkLyck commented 2 years ago

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 commented 7 months ago

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 commented 5 months ago

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