graphql-zeus icon indicating copy to clipboard operation
graphql-zeus copied to clipboard

TypeError: undefined is not an object (evaluating 't[1].v')

Open GauBen opened this issue 3 years ago • 0 comments

While not an error of Zeus strictly speaking, the client does not succeed in building specific malformed queries:

// Valid query
me: {
  id: true,
  contacts: { // Contact does not expect arguments
    id: true,
    name: true
  }
}

// Crashes Zeus at runtime with a cryptic error
me: {
  id: true,
  contacts: [{}, {
    id: true,
    name: true
  }]
}

I'd like either the types to be stricter (because the invalid query is not reported at compile-time) or the runtime error to be more explicit (see below for proposal).

  const ResolveReturnType = (mappedParts: Part[]) => {
    if (mappedParts.length === 0) {
      return 'not';
    }
    const oKey = ops[mappedParts[0].v];
    const returnP1 = oKey ? returns[oKey] : returns[mappedParts[0].v];
    if (typeof returnP1 === 'object') {
+     if (!mappedParts[1]) throw new Error(`${mappedParts[0].v} does not take arguments`);
+     // Proposal: ^ Explicit runtime error
      const returnP2 = returnP1[mappedParts[1].v];
      //                                   ^^^ This thing does not exist if the field is not expecting args
      if (returnP2) {
        // ...

Link

I'll submit a PR with the addition

GauBen avatar Jun 28 '22 07:06 GauBen