Neo4j-graphql-js: TypeError: Cannot read property 'type' of undefined | apologies for not thinking of a better title

Created on 1 Nov 2017  ·  4Comments  ·  Source: neo4j-graphql/neo4j-graphql-js

Neo4j sample data:

CREATE (Fruits:Category{categoryName:"Fruits",displayOrder: 0}),
(Vegetables:Category{categoryName:"Vegetables", displayOrder: 1}),
(Wheat:Category{categoryName:"Wheat",displayOrder:2})

CREATE(Pineapple:Product{productName:"Pineapple"})
CREATE(Mango:Product{productName:"Mango"})
CREATE(Kiwi:Product{productName:"Kiwi"})

CREATE(Carrot:Product{productName:"Carrot"})
CREATE(Onion:Product{productName:"Onion"})
CREATE(Tomato:Product{productName:"Tomato"})

CREATE(Lokwan1:Product{productName:"Lokwan1"})
CREATE(Pilsburry:Product{productName:"Pilsburry"})
CREATE(ChakkiFresh:Product{productName:"ChakkiFresh"})


CREATE
  (Fruits)-[:HAS_PRODUCT]->(Pineapple),
  (Fruits)-[:HAS_PRODUCT]->(Mango),
  (Fruits)-[:HAS_PRODUCT]->(Kiwi),

  (Vegetables)-[:HAS_PRODUCT]->(Carrot),
  (Vegetables)-[:HAS_PRODUCT]->(Onion),
  (Vegetables)-[:HAS_PRODUCT]->(Tomato),

  (Wheat)-[:HAS_PRODUCT]->(Lokwan1),
  (Wheat)-[:HAS_PRODUCT]->(Pilsburry),
  (Wheat)-[:HAS_PRODUCT]->(ChakkiFresh)

Graphql Server details:

├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Graphiql query

query {
  AllCategories {
    categoryName
    displayOrder
  }
}

Graphiql Response

{
  "data": {
    "AllCategories": [
      {
        "categoryName": "Fruits",
        "displayOrder": 0
      },
      {
        "categoryName": "Vegetables",
        "displayOrder": 1
      },
      {
        "categoryName": "Wheat",
        "displayOrder": 2
      }
    ]
  }
}

However, executing the same query through react-apollo gives below error:

Error Stacktrace:

TypeError: Cannot read property 'type' of undefined
    at buildCypherSelection (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:103:52)
    at buildCypherSelection (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:133:12)
    at buildCypherSelection (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:133:12)
    at cypherQuery (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:82:51)
    at neo4jgraphql (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:37:15)
    at AllCategories (/mnt/c/projs/strade/live/apps-neo-be/src/schema/schema.js:29:14)
    at /mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql-tools/src/schemaGenerator.ts:536:22
    at resolveFieldValueOrError (/mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql/execution/execute.js:498:12)
    at resolveField (/mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql/execution/execute.js:462:16)
    at /mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql/execution/execute.js:311:18
{ TypeError: Cannot read property 'type' of undefined
    at buildCypherSelection (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:103:52)
    at buildCypherSelection (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:133:12)
    at buildCypherSelection (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:133:12)
    at cypherQuery (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:82:51)
    at neo4jgraphql (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:37:15)
    at AllCategories (/mnt/c/projs/strade/live/apps-neo-be/src/schema/schema.js:29:14)
    at /mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql-tools/src/schemaGenerator.ts:536:22
    at resolveFieldValueOrError (/mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql/execution/execute.js:498:12)
    at resolveField (/mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql/execution/execute.js:462:16)
    at /mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql/execution/execute.js:311:18
  originalMessage: 'Cannot read property \'type\' of undefined',
  message: 'Error in resolver Query.AllCategories\nCannot read property \'type\' of undefined' }
TypeError: Cannot read property 'type' of undefined
    at buildCypherSelection (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:103:52)
    at buildCypherSelection (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:133:12)
    at buildCypherSelection (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:133:12)
    at cypherQuery (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:82:51)
    at neo4jgraphql (/mnt/c/projs/strade/live/apps-neo-be/node_modules/neo4j-graphql-js/dist/index.js:37:15)
    at AllCategories (/mnt/c/projs/strade/live/apps-neo-be/src/schema/schema.js:29:14)
    at /mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql-tools/src/schemaGenerator.ts:536:22
    at resolveFieldValueOrError (/mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql/execution/execute.js:498:12)
    at resolveField (/mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql/execution/execute.js:462:16)
    at /mnt/c/projs/strade/live/apps-neo-be/node_modules/graphql/execution/execute.js:311:18

React Server details:

├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

I'm using next.js for my react app @ https://github.com/zeit/next.js/tree/canary/examples/with-apollo

Please let me know if further details are required for debugging.

Most helpful comment

Thanks @rohanray and @justinjrussell. I pushed a fix for this just now that is a modified version of @justinjrussell's suggestion to handle more general cases.

It is published to npm as v0.1.6. Please test. Thanks!

All 4 comments

I had the same issue. The problem is react-apollo is sending __typename as a field to graphql. This library is trying to parse __typename as a field which is causing the error. I added a little hack to my local repo to check if field name exist. Works as expected now.

Before:

var fieldName = headSelection.name.value,
      fieldType = schemaType.getFields()[fieldName].type; //Causing the Error

After:

  var fieldName = headSelection.name.value;
  if (!schemaType.getFields()[fieldName]){
    return initial.substring(initial.lastIndexOf(','), 1); //Removing trialing comma
  }
  var fieldType = schemaType.getFields()[fieldName].type;

File: node_modules/neo4j-graphql-js/dist/index.js
Function: buildCypherSelection

@justinjrussell you are right! That solved it :)

I guess __typename should be handled wisely though. AFAIR it's used internally by a few apollo tools for introspection etc. However, I can live now with your hack, but keeping this open to get an opinion from @johnymontana

Cheers!

@justinjrussell I guess you should PR with your solution. Rethinking after that quick response above, your solution is actually good and compatible since __typename is also sent in the response.

Thanks @rohanray and @justinjrussell. I pushed a fix for this just now that is a modified version of @justinjrussell's suggestion to handle more general cases.

It is published to npm as v0.1.6. Please test. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bebbi picture bebbi  ·  3Comments

NathanPimlott picture NathanPimlott  ·  4Comments

AdrienLemaire picture AdrienLemaire  ·  4Comments

momegas picture momegas  ·  3Comments

albert-the-creator picture albert-the-creator  ·  3Comments