graphql2rest icon indicating copy to clipboard operation
graphql2rest copied to clipboard

TypeError: field.type.inspect is not a function

Open ITfromHell opened this issue 3 years ago • 8 comments

Hi guys,

I´m trying to test your pretty cool graphql2rest. In my environment I got always same error:

error: [gqlgenerator] Encountered error trying to generate GQL files for GraphQL operations. Aborting
error: TypeError: field.type.inspect is not a function

So I installed the example without any other code. I added the modules via

yarn add express body-parser
yarn add path graphql graphql2rest

which results in packages.conf:

{
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "graphql": "^16.0.1",
    "graphql2rest": "^0.6.4",
    "path": "^0.12.7"
  }
}

Executing ends up in the error

myuser@MacBook-Pro graphql2rest % node index.js
info: GQLGenerator initializing...
info: GQLGenerator initialized with query depthLimit of 1000
info: [gqlgenerator] Creating folder /Users/myuser/graphql2rest/myGqlFiles/
error: [gqlgenerator] Encountered error trying to generate GQL files for GraphQL operations. Aborting
error: TypeError: field.type.inspect is not a function
    at generateQuery (/Users/myuser/graphql2rest/node_modules/graphql2rest/src/gqlgenerator/schemaParser.js:70:33)
    at /Users/myuser/graphql2rest/node_modules/graphql2rest/src/gqlgenerator/index.js:123:23
    at Array.forEach (<anonymous>)
    at generateFile (/Users/myuser/graphql2rest/node_modules/graphql2rest/src/gqlgenerator/index.js:122:19)
    at generateQueries (/Users/myuser/graphql2rest/node_modules/graphql2rest/src/gqlgenerator/index.js:147:3)
    at Object.generateGqlQueryFiles (/Users/myuser/graphql2rest/node_modules/graphql2rest/src/gqlgenerator/index.js:59:3)
    at Object.<anonymous> (/Users/myuser/graphql2rest/index.js:77:14)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
error: FATAL: Error while attempting to load index.js from gqlGeneratorOutputFolder /Users/myuser/graphql2rest/myGqlFiles. Absolute path: "/Users/myuser/graphql2rest/myGqlFiles/".

                Function generateGqlQueryFiles() must be executed at least once before init() is invoked, to generate .gql query files and index from your schema. Did you run it?

                If you did and you specified a custom folder name, make sure you provide the "options" parameter to init(), and that "options.gqlGeneratorOutputFolder" is set to the correct folder.
...

My environment is

  • MacOS 10.15.7
  • Node.js v14.17.0 also tested with new LTS v16.13.1

ITfromHell avatar Dec 04 '21 15:12 ITfromHell

Which example did you try? (please provide link)
Can you share the GraphQL schema you've used that resulted in the error? Thanks

roy-mor avatar Dec 06 '21 14:12 roy-mor

This one: https://github.com/sisense/graphql2rest/tree/master/examples So the schema is provided in the index.js The project has just three files: two of the example directory and one package.json as see above. It will be started via "node index.js" (in other projects via npm, but this doesnt matter).

ITfromHell avatar Dec 07 '21 08:12 ITfromHell

@roy-mor: Do you need additional info or should I test something?

ITfromHell avatar Dec 28 '21 17:12 ITfromHell

Same problem here :'(

dragma avatar Jan 21 '22 15:01 dragma

Same problem. Also on Mac OS, Node v16.13.1.

Houski avatar May 28 '22 00:05 Houski

Might be related to the version of Node. https://github.com/graphql/graphql-js/issues/1599 it looks like inspect is a method that comes from the javascript language, and it has been deprecated.

Looking at the code, it looks like inspect methods are used as a quick and dirty way to get the name of a type. in src/gqlgenerator/schemaparser.js around line 70.

WarrenWilkinson avatar May 30 '22 13:05 WarrenWilkinson

I got a bit further by replacing field.type.inspect.replace(/[[]!]/g, ''); with field.type.toString().replace(/[[]!]/g, ''); at around line 70 of src/gqlgenerator/schemaparser.js.

WarrenWilkinson avatar May 30 '22 14:05 WarrenWilkinson

The issue definitely happens when updating graphql to v16 and I would agree with Warren's assessment that the removal of Node's inspect function in graphql is the culprit.

@roy-mor could you confirm Warren's fix to replace inspect with toString?

mitchell-bu avatar Jun 21 '22 20:06 mitchell-bu

I confirm that the problem was with object.inspect() being deprecated. I replaced inspect() with toJSON() (which is more appropriate than toString() here).

This issue was fixed by PR #73 . Closing this issue now.

roy-mor avatar Mar 11 '23 23:03 roy-mor