Graphql-tools: Improve error message for mocks that don't return anything

Created on 13 Sep 2017  Â·  3Comments  Â·  Source: ardatan/graphql-tools

while using addMockFunctionsToSchema:

addMockFunctionsToSchema({
  schema: executableSchema,
  mocks: {
    DateTime: () => date('YYYY-MM-DD'),
    Email: () => casual.email,
    Password: () => casual.password,
    URL: () => 'http://via.placeholder.com/156',
    JSON: () => {},
  },
  preserveResolvers: true,
})

and having a resource type that have a field name 'settings':

type UserState {
  userId: String
  settings: JSON
}

running this query:

query withUserState($id: String!) {
  userState(id: $id) {
    userId
    settings
    __typename
  }
}

I'm getting this stack trace:

TypeError: Cannot convert undefined or null to object
    at Function.assign (<anonymous>)
    at mergeObjects (/Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql-tools/src/mock.ts:93:19)
    at mergeMocks (/Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql-tools/src/mock.ts:134:14)
    at /Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql-tools/src/mock.ts:220:20
    at field.resolve (/Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql-tools/src/mock.ts:354:13)
    at resolveFieldValueOrError (/Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql/execution/execute.js:501:12)
    at resolveField (/Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql/execution/execute.js:465:16)
    at /Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql/execution/execute.js:314:18
    at Array.reduce (native)
    at executeFields (/Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql/execution/execute.js:311:42)
    at collectAndExecuteSubfields (/Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql/execution/execute.js:749:10)
    at completeObjectValue (/Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql/execution/execute.js:731:10)
    at completeValue (/Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql/execution/execute.js:628:12)
    at /Users/ehudshahak/WebstormProjects/zengaming/apollo-server/node_modules/graphql/execution/execute.js:585:14
    at <anonymous>

when removing the addMockFunctionsToSchema code
the query works and returns data without any exception.

what's going on here?
thanks

feature

Most helpful comment

You probably wanted to do JSON => ({}) (notice the parentheses around the brackets) otherwise the brackets are considered to be function brackets and thus JSON is a function with an empty body, that returns undefined by default.

All 3 comments

You probably wanted to do JSON => ({}) (notice the parentheses around the brackets) otherwise the brackets are considered to be function brackets and thus JSON is a function with an empty body, that returns undefined by default.

Yeah - we don't have default mocks for unknown custom scalars. I think a PR to make this throw an actually useful error would be great, because clearly this isn't a great experience.

Whoops, I misunderstood the issue!

Yeah I think this error message is not ideal, but it's a result of a mock returning undefined. We could go through and improve the messages around this, but I'm going to close the issue since I'm not sure there's a specific action item.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Adherentman picture Adherentman  Â·  4Comments

MehrdadKhnzd picture MehrdadKhnzd  Â·  3Comments

alfaproject picture alfaproject  Â·  4Comments

adamkl picture adamkl  Â·  3Comments

dcworldwide picture dcworldwide  Â·  4Comments