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

SyntaxError: Unexpected identifier #587

Open
akoskm opened this issue Mar 26, 2022 · 9 comments
Open

SyntaxError: Unexpected identifier #587

akoskm opened this issue Mar 26, 2022 · 9 comments

Comments

@akoskm
Copy link

akoskm commented Mar 26, 2022

I'm trying to import a .graphql file in a file called main.ts.

I set up the webpack loader according to the readme file https://github.com/apollographql/graphql-tag#webpack-loading-and-preprocessing

Here's how the file is imported:

import ProjectsQuery from '../graphql/query/ProjectsQuery.graphql';

and the error I'm getting:

/Users/akoskm/Projects/sprcalc-electron/src/graphql/query/ProjectsQuery.gql:1
query ProjectsQuery {
      ^^^^^^^^^^^^^

SyntaxError: Unexpected identifier
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1066:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1140:10)
    at Object.require.extensions.<computed> [as .js] (/Users/akoskm/Projects/sprcalc-electron/node_modules/ts-node/src/index.ts:1361:43)
    at Module.load (node:internal/modules/cjs/loader:982:32)
    at Module._load (node:internal/modules/cjs/loader:823:12)
    at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
    at Module.require (node:internal/modules/cjs/loader:1006:19)
    at require (node:internal/modules/cjs/helpers:93:18)
ERROR in /Users/akoskm/Projects/sprcalc-electron/src/main/main.ts

The relevant part of my webpack configuration:

export default {
  externals: [...Object.keys(externals || {})],

  stats: 'errors-only',

  module: {
    rules: [
      {
        test: /\.(graphql|gql)$/,
        exclude: /node_modules/,
        loader: 'graphql-tag/loader',
      },
      {
        test: /\.[jt]sx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'ts-loader',
        },
      },
    ],
  },

  output: {
    path: webpackPaths.srcPath,
    // https://github.com/webpack/webpack/issues/1114
    library: {
      type: 'commonjs2',
    },
  },

  /**
   * Determine the array of extensions that should be used to resolve modules.
   */
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.ts', '.tsx', '.graphql', '.gql'],
    modules: [webpackPaths.srcPath, 'node_modules'],
  },

  plugins: [
    new webpack.EnvironmentPlugin({
      NODE_ENV: 'production',
    }),

    new webpack.ProvidePlugin({
      $: 'jquery',
      'window.$': 'jquery',
      jquery: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery',
      draw2d: 'draw2d',
    }),
  ],
};

and here's the query:

query ProjectsQuery {
  projects {
    id
    name
    description
    project_phases {
      id
      description
      start_date
      completion_date
    }
  }
}
@Isomorpheus
Copy link

@akoskm Were you able to solve this problem?

@akoskm
Copy link
Author

akoskm commented Jan 18, 2023

@Isomorpheus no, I didn't work on this. This is still an open issue in our project.

I will update you once we have a solution.

@juancampuzano
Copy link

@akoskm Were you able to solve this problem?

@akoskm
Copy link
Author

akoskm commented May 12, 2023

@juancampuzano see my comment above.

@BhavinPatel04
Copy link

I was facing the same issue and I solved it by using this library - https://github.com/evenchange4/graphql.macro

@jerelmiller
Copy link
Member

Hey @akoskm 👋

Would you be able to create a minimal, runnable reproduction of this issue? The webpack loader typically works well, so it would be helpful to understand if this issue is related to the way you've set up your project, or if there is some other change we need to be aware of in the library itself. Thanks!

@HoseinGhanbari
Copy link

I'm facing this issue as well. If i put the .graphql file content directly inside the template literal tag gql there will be no problem. But as soon as I put them in a separate .graphql file the error SyntaxError: Unexpected identifier will be shown.

@HoseinGhanbari
Copy link

I was facing the same issue and I solved it by using this library - https://github.com/evenchange4/graphql.macro

Could you explain how were you able to solve the problem by using graphql.macro ?

@BhavinPatel04

@HoseinGhanbari
Copy link

HoseinGhanbari commented Nov 16, 2023

Fortunately, a solution found by using graphql-import-node

https://github.com/ardatan/graphql-import-node

  1. this is how api-extensions.graphql.ts look like where you are importing .graphql
import gql from 'graphql-tag';
import 'graphql-import-node';

import q1 from "./q1-customizations.graphql";

export const commonApiExtensions = gql`
  ${q1}
`;
  1. this is how dev mode npm script can be write
ts-node -r graphql-import-node/register ./src/index.ts

OR

node-dev -r graphql-import-node/register ./src/index.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants