apollo: Can`t load .gql files

Version

v4.0.0-beta.5

Reproduction link

https://github.com/nuxt-community/apollo-module

Steps to reproduce

From official example.

<template>
  <div>
        ....
  </div>

</template>
<script>
import allCars from '~/apollo/queries/allCars'

export default {

}
</script>

And the query apollo/queries/allCars.gql

{
  allCars {
    id
    make
    model
    year
  }
}

Am i missing something?

What is expected ?

Should import the .gql file

What is actually happening?

This dependency was not found:

  • @/apollo/queries/allCars in ./node_modules/babel-loader/lib?{“babelrc”:false,“cacheDirectory”:true,“presets”:[[“/var/www/node_modules/babel-preset-vue-app/dist/index.common.js”,{“targets”:{“ie”:9,“uglify”:true}}]]}!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./pages/cars.vue

To install it, you can run: npm install --save @/apollo/queries/allCars

Additional comments?

Also including extension .gql does not work.

import allCars from '~/apollo/queries/allCars.gql'

My nuxt.config.js

modules: [
    '@nuxtjs/apollo',
  ],

apollo: {
    includeNodeModules: true,
    clientConfigs: {
      default: {
        httpEndpoint: 'http://my-api.com/graphql',
      }
    }
  }
<div align="right">This bug report is available on Nuxt community (#c106)</div>

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 20 (8 by maintainers)

Most upvoted comments

@robsontenorio not everybody is using *.gql files so its not part of the module. I will add note thanks for bringing this up

Ok, the problem is: an additional step is required.

yarn add graphql-tag

The apollo module was not supposed to manage this dependency?

Hello, Thanks for your help. I was using nuxt.config.js this way https://github.com/nuxt/nuxt.js/blob/dev/examples/vue-apollo/nuxt.config.js For some reason, it does not work. I replaced with your version of nuxt.config and it is working now. Thanks

@dohomi no lucky with relative folder, and .gql extension on import statement as suggested.

Step by step

Create a nuxt app with yarn.

yarn create nuxt-app my-app

Options: image

On my-app folder, update nuxt version to 1.4.1 and add apollo module

yarn add nuxt @nuxtjs/apollo

On nuxt.config.js

modules: [
    '@nuxtjs/apollo',
  ],
  
  apollo: {
    includeNodeModules: true,
    clientConfigs: {
      default: {
        httpEndpoint: 'http://my-api.com/graphql',
      }
    }
  },

Create apollo/queries/allCars.gql

{
  allCars {
    id
    make
    model
    year
  }
}

Create pages/cars.vue

<template>
  <div>
    ...
  </div>
</template>

<script>
import allCars from '../apollo/queries/allCars.gql'
// or
import allCars from '~/apollo/queries/allCars'


export default {

}
</script>

Note

With create-nuxt-app the nuxt.config.js has this default entry. Maybe something here is causing problem on gql loader?

build: {
    /*
    ** You can extend webpack config here
    */
    extend (config, ctx) {
      // Run ESLint on save
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        })
      }
    }
  }