gatsbyjs / gatsby

The best React-based framework with performance, scalability and security built in.
https://www.gatsbyjs.com
MIT License
55.28k stars 10.31k forks source link

gatsby-source-graphql > 2.1.29: schema.getDirectives is not a function #21443

Closed lablancas closed 4 years ago

lablancas commented 4 years ago

Description

Encountered schema.getDirectives is not a function using gatsby-source-graphql@2.1.33.

The issue seems to be with dependency graphql-tool-fork@8.

Using gatsby-source-graphql@2.1.29 which depends on graphql-tool-fork@7 is how I worked around this issue.

Steps to reproduce

# clone repo
git clone https://github.com/lablancas/gatsby-reproduce-bug.git
cd gatsby-reproduce-bug

# edit .env.development to connect to your GraphQL server

# install dependencies and start gatsby
yarn
gatsby develop

Expected result

Gatsby development server starts without error and lineups node has been successfully created

Open http://localhost:8000/___graphql and run

query {
  lineups {
    __typename
  }
}

Response should be

{
  "data": {
    "lineups": {
      "__typename": "Lineups"
    }
  }
}

Actual result

Gatsby server log has an error TypeError: schema.getDirectives is not a function and lineups node was not created

GraphQL response from the query above is

{
  "errors": [
    {
      "message": "Cannot query field \"lineups\" on type \"Query\".",
      "locations": [
        {
          "line": 3,
          "column": 3
        }
      ],
      "stack": [
        "GraphQLError: Cannot query field \"lineups\" on type \"Query\".",
        "    at Object.Field (/Users/lucas/Development/lineups/gatsby-reproduce-bug/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)",
        "    at Object.enter (/Users/lucas/Development/lineups/gatsby-reproduce-bug/node_modules/graphql/language/visitor.js:324:29)",
        "    at Object.enter (/Users/lucas/Development/lineups/gatsby-reproduce-bug/node_modules/graphql/language/visitor.js:375:25)",
        "    at visit (/Users/lucas/Development/lineups/gatsby-reproduce-bug/node_modules/graphql/language/visitor.js:242:26)",
        "    at validate (/Users/lucas/Development/lineups/gatsby-reproduce-bug/node_modules/graphql/validation/validate.js:73:24)",
        "    at getGraphQLParams.then.then.optionsData (/Users/lucas/Development/lineups/gatsby-reproduce-bug/node_modules/express-graphql/index.js:121:32)",
        "    at process._tickCallback (internal/process/next_tick.js:68:7)"
      ]
    }
  ]
}

Environment

System: OS: macOS 10.15.3 CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz Shell: 5.7.1 - /bin/zsh Binaries: Node: 10.17.0 - ~/.nvm/versions/node/v10.17.0/bin/node Yarn: 1.22.0 - ~/.yvm/shim/yarn npm: 6.11.3 - ~/.nvm/versions/node/v10.17.0/bin/npm Browsers: Chrome: 79.0.3945.130 Safari: 13.0.5 npmPackages: gatsby: ^2.19.7 => 2.19.7 gatsby-image: ^2.2.39 => 2.2.39 gatsby-plugin-manifest: ^2.2.39 => 2.2.39 gatsby-plugin-offline: ^3.0.32 => 3.0.32 gatsby-plugin-react-helmet: ^3.1.21 => 3.1.21 gatsby-plugin-sharp: ^2.4.3 => 2.4.3 gatsby-source-filesystem: ^2.1.46 => 2.1.46 gatsby-source-graphql: ^2.1.33 => 2.1.33 gatsby-transformer-sharp: ^2.3.13 => 2.3.13

narration-sd commented 4 years ago

Confirmed -- thanks, @lablancas.

I have just the same error, on two separate long-term projects, data from two quite differing GraphQL servers.

Running gatsby-source-graphql back to 2.1.29 recovers operation.

$ npm run build

gatsby-starter-default@1.0.0 build C:\vagrant\release\lv-gatsby_html gatsby build

success open and validate gatsby-configs - 0.063s success load plugins - 1.972s success onPreInit - 0.005s success delete html and css files from previous builds - 0.011s success initialize cache - 0.022s success copy gatsby files - 0.232s success onPreBootstrap - 0.019s success createSchemaCustomization - 0.006s

ERROR #11321 PLUGIN

"gatsby-source-graphql" threw an error while running the sourceNodes lifecycle:

schema.getDirectives is not a function

99 | }; 100 |

101 | const schema = transformSchema({ | ^ 102 | schema: introspectionSchema, 103 | link 104 | }, [new StripNonQueryTransform(), new RenameTypes(name => ${typeName}_${name}), new NamespaceUnderFieldTransform({

File: node_modules\gatsby-source-graphql\gatsby-node.js:101:18

not finished source and transform nodes - 2.410s npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! gatsby-starter-default@1.0.0 build: gatsby build npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the gatsby-starter-default@1.0.0 build script.

secretlifeof commented 4 years ago

I can confirm this error using the following steps:

  1. gatsby new my-graphql-error
  2. yarn add gatsby-source-graphql
  3. add gatsby-source-graphql to gatsby-config.js:plugins
  4. yarn develop

Having the same error with v.2.1.33, v.2.1.32, v.2.1.31. Confirmed working with v.2.1.28.

roryashfordbentley commented 4 years ago

Running gatsby-source-graphql back to 2.1.29 recovers operation.

Thanks so much @narration-sd you just saved me hours of debugging.

vladar commented 4 years ago

@lablancas Kudos for the repro! 👍

@yaacovCR Mind taking a look at this?

narration-sd commented 4 years ago

The trick of it is that it isn't the plugin that changed -- the latest was 13 days ago.

It's something it doesn't get along with now, perhaps a library that summarily changed its interface.

You have to wonder where testing was on this one, don't you...

@roryashfordbentley Most welcome, but it was really just close reading of @lablancas' report, trying what he found and said 🙂

yaacovCR commented 4 years ago

Sorry you had to hit this. Must be very frustrating.

Must be something in graphql-tools-fork patch revision to 8.5.4.

That was about switching to eslint to avoid problems like #21170 .

Do you have a stack trace handy.

Hopeful I can dig into this soon.

narration-sd commented 4 years ago

@yaacovCR Thanks for coming in.

I have a feeling the problem is something like discovered here: https://github.com/apollographql/graphql-tools/issues/927

It's a matter there at least of a Promise being returned, rather than the schema itself -- so there needs to be async, or another formulation to resolve.

This will explain the obscure references in the error (where is schema..getDirectives() even called...) and will occur probably in some chain of libraries, perhaps back into Apollo, like the one you mentioned.

As an aside, I'd like someitime to understand why a change of Lint may be implicated here and in other recent upsets :)

Haven't more time to work with this, but imagine you'll have much more familiarity, so best fortune that it doesn't take too much.

At least we have a workaround, and maybe it will be easy for you to re-link whatever changed in gatsby-source-graphql itself, since 2.1.29

narration-sd commented 4 years ago

revised ^^comment perhaps usefully, if you are just getting by email

narration-sd commented 4 years ago

And, one last thing I did for understanding, which may turn out to help all around:

Ok, hope that streamlines your discovery, @yaacovCR , and at least it indicates another workaround persons here can likely use, if they want or need to stick with latest gatsby-source-graphql: specify graphql.tools.fork at 8.4.0. Quite possibly some later releases might also work.

I suspect it also provokes something specific that can improve the testing: force a completely fresh npm install before running the process....no doubt anything in Gatsby should be doing this...

Cheers, Clive

p.s. also indicated, why the 'whole (Gatsby) world' hasn't yet run into this -- if you already had the older graphql.tools.fork dependency loaded in node_modules, it won't have updated to the problem...

yaacovCR commented 4 years ago

Should be fixed by graphql-tools-fork v8.5.5.

Apologies for regression. If you can verify this has been fixed with v8.5.5 after unpinning v8.5.4, should be able to close.

narration-sd commented 4 years ago

Giving it a shot, Yaacov, will report - thanks for your effort.

narration-sd commented 4 years ago

Ok -- vm etc. came up more rapidly than thought, having been sitting here not with code -- and, your fix looks to work!

I'd say it's good here. You may well want to wait for at least one more confirm before closing -- and get some rest, if I guess a timezone where you are!

Much appreciated, Yaacov Clive

narration-sd commented 4 years ago

Appreciate also the testing you put in -- should help future stay safe...

BURROO commented 4 years ago

Updating graphql-tools-fork to version 8.5.5 fixed the schema.getDirectives error. In my case I now get a new error, that didn't occur before and may or may not be related, but i couldn't find any clues of how to fix it:

 ERROR #11321  PLUGIN

"gatsby-source-graphql" threw an error while running the sourceNodes lifecycle:

fetcher is not a function

  73 | 
  74 |     if (!sdl) {
> 75 |       introspectionSchema = await introspectSchema(link);
     |                                   ^
  76 |       sdl = printSchema(introspectionSchema);
  77 |     } else {
  78 |       introspectionSchema = buildSchema(sdl);

EDIT: That was a different issue and is now resolved! The fix worked for me.

narration-sd commented 4 years ago

good job, @BURROO

Let me guess, you needed to do a complete removal of node_modules and package.lock.json, then an npm install to get everything up to date with itself. Though I understand the difficulties, npm is only kind of a surface package manager...

yaacovCR commented 4 years ago

@lablancas @roryashfordbentley how does this look with graphql-tools-fork v8.5.5 or higher?

vladar commented 4 years ago

Thank you for opening this, @lablancas

We're marking this issue as answered and closing it for now but please feel free to reopen this and comment if you would like to continue this discussion. We hope we managed to help and thank you for using Gatsby! 💜

vladar commented 4 years ago

And thanks a lot @yaacovCR for taking care of this!

lablancas commented 4 years ago

@lablancas @roryashfordbentley how does this look with graphql-tools-fork v8.5.5 or higher?

This problem is fixed for me. Thanks everyone.

jonrrivera commented 4 years ago

hey everybody, I keep getting the error

Screen Shot 2020-04-10 at 1 45 50 AM

despite trying versions: "gatsby-source-graphql": "2.1.28" "gatsby-source-graphql": "2.1.29" "gatsby-source-graphql": "2.1.32"

Please let me know if you need further context. thanks!

Hansanghyeon commented 4 years ago

package-lock.json

"gatsby-source-graphql": {
  "version": "2.3.2",
  "resolved": "https://registry.npmjs.org/gatsby-source-graphql/-/gatsby-source-graphql-2.3.2.tgz",
  "integrity": "sha512-RonjzodAAdMwh/idCokA6IqeH4eQGQPQD2vO9CDx3ocMiyvKRM5f7x617R1jxZM6lATjn1ndySmDvLvRqJz/Ig==",
  "requires": {
    "@babel/runtime": "^7.8.7",
    "apollo-link": "1.2.13",
    "apollo-link-http": "^1.5.16",
    "dataloader": "^2.0.0",
    "graphql": "^14.6.0",
    "graphql-tools-fork": "^8.9.6",
    "invariant": "^2.2.4",
    "node-fetch": "^1.7.3",
    "uuid": "^3.4.0"
  },
  "dependencies": {
    "node-fetch": {
      "version": "1.7.3",
      "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz",
      "integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==",
      "requires": {
        "encoding": "^0.1.11",
        "is-stream": "^1.0.1"
      }
    }
  }
},

Running 'gatsby build' with the above settings gives the same error as the image. If you run gatsby build after restoring to the following settings, no error will occur. What is the problem?

backup package-lock.json

"gatsby-source-graphql": {
  "version": "2.2.0",
  "resolved": "https://registry.npmjs.org/gatsby-source-graphql/-/gatsby-source-graphql-2.2.0.tgz",
  "integrity": "sha512-2Wk05rkOPUK4URVm3g0O8bcXKvRHAC0bNa3j7bguxv0DSa936Qr16kAm94t18EAe9TWDHH7x8Q4R1QJ2VDTEqQ==",
  "requires": {
    "@babel/runtime": "^7.8.7",
    "apollo-link": "1.2.13",
    "apollo-link-http": "^1.5.16",
    "graphql-tools-fork": "^8.9.6",
    "invariant": "^2.2.4",
    "node-fetch": "^1.7.3",
    "uuid": "^3.4.0"
  },
  "dependencies": {
    "node-fetch": {
      "version": "1.7.3",
      "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz",
      "integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==",
      "requires": {
        "encoding": "^0.1.11",
        "is-stream": "^1.0.1"
      }
    }
  }
},
ZLevine commented 4 years ago

I'm having the same issue as @jonrrivera and @Hansanghyeon

2.1.29 is the newest version that fixes the issue; installing 2.1.30 or later reintroduces the issue.

d-floutier commented 4 years ago

I have the same issue @jonrrivera and @Hansanghyeon

alnaranjo commented 4 years ago

I'm also experiencing this on version 2.3.2. I've confirmed that my API is reachable.

$ gatsby build
process.env.STAGE:  undefined
success open and validate gatsby-configs - 0.090s
success load plugins - 1.004s
success onPreInit - 0.032s
success delete html and css files from previous builds - 0.034s
success initialize cache - 0.030s
success copy gatsby files - 0.116s
success onPreBootstrap - 0.021s
success createSchemaCustomization - 0.011s

 ERROR #11321  PLUGIN

"gatsby-source-graphql" threw an error while running the sourceNodes lifecycle:

fetcher is not a function

  79 |
  80 |     if (!sdl) {
> 81 |       introspectionSchema = await introspectSchema(link);
     |                                   ^
  82 |       sdl = printSchema(introspectionSchema);
  83 |     } else {
  84 |       introspectionSchema = buildSchema(sdl);

File: ../../node_modules/gatsby-source-graphql/gatsby-node.js:81:35

  TypeError: fetcher is not a function

  - introspectSchema.js:14 introspectSchema
    [covid-19]/[gatsby-source-graphql]/[graphql-tools-fork]/dist/stitch/introspectSchema.js:14:12

  - gatsby-node.js:81 Object.exports.sourceNodes
    [covid-19]/[gatsby-source-graphql]/gatsby-node.js:81:35

not finished source and transform nodes - 0.288s
pgcath commented 4 years ago

Can confirm I am running into this as well.

Gatsby CLI version: 2.11.2 Gatsby version: 2.20.17 gatsby-source-graphql: 2.3.2

ERROR #11321 PLUGIN

"gatsby-source-graphql" threw an error while running the sourceNodes lifecycle:

fetcher is not a function

79 | 80 | if (!sdl) {

81 | introspectionSchema = await introspectSchema(link); | ^ 82 | sdl = printSchema(introspectionSchema); 83 | } else { 84 | introspectionSchema = buildSchema(sdl);

File: node_modules/gatsby-source-graphql/gatsby-node.js:81:35

marconucara commented 4 years ago

Same problem for me: gatsby-cli: 2.11.7 gatsby-source-graphql: 2.3.2

yaacovCR commented 4 years ago

I was briefly able to reproduce this exact error using gatsby/examples/using-gatsby-source-graphql, but then I deleted the cache and node-modules folders, and reran yarn install and yarn develop, and now I can't reproduce.

So...not quite sure what is causing this, but I am imagining it has to do with conflicts regarding versions?

Maybe you could try running:

yarn why graphql-tools-fork
yarn why apollo-link

That information might be of some help. I would also suggest opening a fresh issue to gain some more visibility.

marconucara commented 4 years ago

I removed cache and node modules without success. Here my yarn why responses:

=> Found "graphql-tools-fork@9.0.1"
info Has been hoisted to "graphql-tools-fork"
info Reasons this module exists
   - "workspace-aggregator-3af4dbbb-e46e-42e0-8d9d-6742ac70aacd" depends on it
   - Hoisted from "_project_#@colombo-it#website#graphql-tools-fork"
   - Hoisted from "_project_#@colombo-it#website#gatsby-plugin-graphql-codegen#@graphql-toolkit#common#graphql-tools-fork"
   - Hoisted from "_project_#@colombo-it#website#gatsby-plugin-graphql-codegen#@graphql-toolkit#url-loader#graphql-tools-fork"
   - Hoisted from "_project_#@colombo-it#website#gatsby-plugin-graphql-codegen#@graphql-toolkit#core#@graphql-toolkit#schema-merging#graphql-tools-fork"
info Disk size without dependencies: "2.22MB"
info Disk size with unique dependencies: "3.05MB"
info Disk size with transitive dependencies: "4.85MB"
info Number of shared dependencies: 19
=> Found "gatsby-source-graphql#graphql-tools-fork@8.10.0"
info This module exists because "_project_#@colombo-it#website#gatsby-source-graphql" depends on it.
info Disk size without dependencies: "5.81MB"
info Disk size with unique dependencies: "6.64MB"
info Disk size with transitive dependencies: "8.44MB"
info Number of shared dependencies: 19
=> Found "apollo-link@1.2.14"
info Has been hoisted to "apollo-link"
info Reasons this module exists
   - "workspace-aggregator-612cc2d0-299d-4055-bf09-f33868daf8dc" depends on it
   - Hoisted from "_project_#graphql-tools-fork#apollo-link"
   - Hoisted from "_project_#@colombo-it#cms#apollo-boost#apollo-link"
   - Hoisted from "_project_#graphql-tools-fork#apollo-link-http-common#apollo-link"
   - Hoisted from "_project_#@colombo-it#cms#apollo-boost#apollo-client#apollo-link"
   - Hoisted from "_project_#@colombo-it#cms#apollo-boost#apollo-link-error#apollo-link"
   - Hoisted from "_project_#@colombo-it#cms#apollo-boost#apollo-link-http#apollo-link"
   - Hoisted from "_project_#@colombo-it#bff-website#apollo-server-lambda#graphql-tools#apollo-link"
info Disk size without dependencies: "224KB"
info Disk size with unique dependencies: "1.31MB"
info Disk size with transitive dependencies: "1.65MB"
info Number of shared dependencies: 7
=> Found "gatsby-source-graphql#apollo-link@1.2.13"
info This module exists because "_project_#@colombo-it#website#gatsby-source-graphql" depends on it.
info Disk size without dependencies: "224KB"
info Disk size with unique dependencies: "1.31MB"
info Disk size with transitive dependencies: "1.65MB"
info Number of shared dependencies: 7
=> Found "gatsby-source-graphql#graphql-tools-fork#apollo-link@1.2.14"
info This module exists because "_project_#@colombo-it#website#gatsby-source-graphql#graphql-tools-fork" depends on it.
info Disk size without dependencies: "3.45MB"
info Disk size with unique dependencies: "4.54MB"
info Disk size with transitive dependencies: "4.88MB"
info Number of shared dependencies: 7
yaacovCR commented 4 years ago

Did you also have a lock file? And did you delete that?

I might suggest opening a new issue with a minimal reproduction.

I have a sneaking suspicion this is caused by you having two versions of ApolloLink, have no idea why though.

yaacovCR commented 4 years ago

See #23002

pgcath commented 4 years ago

That was it, @yaacovCR .

Thanks!