Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions not compatible with apollo server #33047

Closed
2 tasks done
mariusc23 opened this issue Sep 4, 2021 · 7 comments
Closed
2 tasks done

Functions not compatible with apollo server #33047

mariusc23 opened this issue Sep 4, 2021 · 7 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. topic: DX Developer Experience (e.g. Fast Refresh, i18n, SSR, page creation, starters) topic: webpack/babel Webpack or babel type: bug An issue or pull request relating to a bug in Gatsby

Comments

@mariusc23
Copy link

mariusc23 commented Sep 4, 2021

Preliminary Checks

Description

I wanted to set up a small graphql endpoint for the frontend to use (for forms), but I couldn't get apollo-server-micro to work. This is the error I got:

Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm.

Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.

https://yarnpkg.com/en/docs/selective-version-resolutions

Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.

This happens when initializing the ApolloServer instance. If I run the file using node.js directly it does not throw an error on the same line, so I think Gatsby may be doing some loader magic that breaks things that need the graphql module, such as apollo server. I think this magic should be dispelled for Gatsby Functions.

I have verified that indeed there is only one graphql module in my node_modules using yarn why graphql and manually checking.

Reproduction Link

https://codesandbox.io/s/quirky-thompson-llnhk?file=/src/api/graphql.js

Steps to Reproduce

  1. Install apollo-server-micro and micro
  2. Create a src/api/graphql.js with a basic graphql server (see example below)
  3. Attempt to load /api/graphql in browser
import { ApolloServer, gql } from "apollo-server-micro"

const typeDefs = gql`
  type Query {
    sayHello: String
  }
`

const resolvers = {
  Query: {
    sayHello(parent, args, context) {
      return "Hello World!"
    }
  }
}

// this is the line that throws
const apolloServer = new ApolloServer({ typeDefs, resolvers, debug: true })

export default async function graphqlEndpoint(req, res) {
  await apolloServer.start()
  const handler = apolloServer.createHandler()

  return handler(req, res)
}

Expected Result

The graphql endpoint would show

Actual Result

Error when executing function "/.../src/api/graphql.ts":

Cannot use GraphQLSchema "[object GraphQLSchema]" from another module or realm. Ensure that there is only one instance of "graphql" in the node_modules directory. If different versions of "graphql" are the dependencies of other relied on modules, use "resolutions" to ensure only one version is installed. https://yarnpkg.com/en/docs/selective-version-resolutions Duplicate "graphql" modules cannot be used at the same time since different versions may have different capabilities and behavior. The data from one version used in the function from another could produce confusing and spurious results.

Environment

System:
    OS: macOS 11.5.2
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.15.5 - ~/.nvm/versions/node/v14.15.5/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.22.0 - ~/.nvm/versions/node/v14.15.5/bin/npm
  Languages:
    Python: 2.7.16 - /Users/marius/.pyenv/shims/python
  Browsers:
    Chrome: 93.0.4577.63
    Firefox: 89.0.2
    Safari: 14.1.2
  npmPackages:
    gatsby: ^3.13.0 => 3.13.0 
    gatsby-awesome-pagination: ^0.3.8 => 0.3.8 
    gatsby-plugin-advanced-sitemap: ^2.0.0 => 2.0.0 
    gatsby-plugin-catch-links: ^3.13.0 => 3.13.0 
    gatsby-plugin-drift: ^1.0.0 => 1.0.0 
    gatsby-plugin-feed: ^3.13.0 => 3.13.0 
    gatsby-plugin-force-trailing-slashes: ^1.0.5 => 1.0.5 
    gatsby-plugin-google-analytics: ^3.13.0 => 3.13.0 
    gatsby-plugin-image: ^1.13.0 => 1.13.0 
    gatsby-plugin-manifest: ^3.13.0 => 3.13.0 
    gatsby-plugin-mdx: ^2.13.0 => 2.13.0 
    gatsby-plugin-netlify-cms: ^5.13.0 => 5.13.0 
    gatsby-plugin-offline: ^4.13.0 => 4.13.0 
    gatsby-plugin-postcss: ^4.13.0 => 4.13.0 
    gatsby-plugin-react-helmet: ^4.13.0 => 4.13.0 
    gatsby-plugin-root-import: ^2.0.7 => 2.0.7 
    gatsby-plugin-sharp: ^3.13.0 => 3.13.0 
    gatsby-plugin-typegen: ^2.2.4 => 2.2.4 
    gatsby-plugin-typescript: ^3.13.0 => 3.13.0 
    gatsby-plugin-webpack-bundle-analyser-v2: ^1.1.25 => 1.1.25 
    gatsby-remark-images: ^5.10.0 => 5.10.0 
    gatsby-source-filesystem: ^3.13.0 => 3.13.0 
    gatsby-transform-carbonara: ^1.0.0 => 1.0.0 
    gatsby-transformer-remark: ^4.10.0 => 4.10.0 
    gatsby-transformer-sharp: ^3.13.0 => 3.13.0

Config Flags

No response

@mariusc23 mariusc23 added the type: bug An issue or pull request relating to a bug in Gatsby label Sep 4, 2021
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Sep 4, 2021
@mariusc23
Copy link
Author

mariusc23 commented Sep 7, 2021

Finally had some time to do some digging into this and it seems like an issue with webpack and the fact that apollo-server doesn't publish ESM versions yet, but the graphql module does: apollographql/apollo-server#4983 and apollographql/apollo-server#5627

It seems like if gatsby wants to support apollo-server in functions there needs to be a way to customize the functions webpack config. I verified if I manually define extensions: ['ts', 'mjs', 'js', '...'] in the functions webpack config that it works. Is there a plan to support customizing the webpack config?

@LekoArts LekoArts added topic: DX Developer Experience (e.g. Fast Refresh, i18n, SSR, page creation, starters) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Sep 8, 2021
@LekoArts
Copy link
Contributor

LekoArts commented Sep 8, 2021

The comments apollographql/apollo-server#4983 (comment) & apollographql/apollo-server#5627 (comment) seem to suggest though that this would (also) be fixed with adding an alias. Gatsby doesn't support ESM right now so you can't use pure ESM packages out of the box just yet.

@mariusc23
Copy link
Author

mariusc23 commented Sep 8, 2021

Yeah, alias should work too. Just need a way to configure the functions webpack config. The onCreateWebpackConfig hook seems to be only for non-functions webpack.

@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Sep 29, 2021
@mariusc23
Copy link
Author

This is still an issue since there's no way to configure the functions webpack config AFAIK. Could file a ticket for that or rename this one if preferred?

@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Sep 30, 2021
@graysonhicks graysonhicks added topic: webpack/babel Webpack or babel type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement. and removed type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement. labels Sep 30, 2021
@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Oct 20, 2021
@github-actions
Copy link

Hey again!

It’s been 60 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it.
Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to comment on this issue or create a new one if you need anything else.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community! 💪💜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. topic: DX Developer Experience (e.g. Fast Refresh, i18n, SSR, page creation, starters) topic: webpack/babel Webpack or babel type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

3 participants