4

I do import graphQL query as:

import {getStudents} from'./../graphql/Queries.graphql'

Queries.graphql file:

query getStudents($schoolID: Int!){
  allStudents(schoolId: $schoolID){
    pickUpLat
    pickUpLng
  }
}

but when I try to pass the query to the component as:

export default graphql(getStudents, {
  options: (props) => ({ variables: { schoolID: props.schoolID } })
})( StudentsMap );

I get this error:

Uncaught Error: Argument of undefined passed to parser was not a valid GraphQL DocumentNode. You may need to use 'graphql-tag' or another method to convert your operation into a document
    at invariant (browser.js:38)
    at parser (react-apollo.browser.umd.js:199)
    at graphql (react-apollo.browser.umd.js:1061)
    at Object../src/map/StudentsMap.js (StudentsMap.js:96)
    at __webpack_require__ (bootstrap b06dc52180087582baaa:19)
    at Object../src/App.js (App.css?9a66:26)
    at __webpack_require__ (bootstrap b06dc52180087582baaa:19)
    at Object../src/index.js (index.css?f255:26)
    at __webpack_require__ (bootstrap b06dc52180087582baaa:19)
    at Object.0 (registerServiceWorker.js:117)

However, if I define the query in the same file of component (not importing it) as:

const getStudents= gql`
query getStudents($schoolID: Int){
  allStudents(schoolId: $schoolID){
    pickUpLat
    pickUpLng
  }
}`

then it does work normally, any idea why I get that error when try to import the query?

Here is my webpack.config.js settings: (based on this reference)

var webpack = require('webpack');

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

It seems that Loading queries with Webpack fails? or what it could be?

simo
  • 23,342
  • 38
  • 121
  • 218
  • 1
    According to your code you are not transforming the imported "plain-text" into a graphql with the gql`...` function, or is this just a code omission? – Roland Studer May 21 '18 at 14:39
  • I mean one time gesStudents is `{query}` (when you import it) the other time it is `gql\`{query}\`` – Roland Studer May 21 '18 at 14:40
  • 1
    This should be handled with `Webpack preprocessing with graphql-tag/loader` as they say here: https://github.com/apollographql/graphql-tag – simo May 22 '18 at 07:53

0 Answers0