javascript - Nuxt : choose client config other than default

标签 javascript vue.js nuxt.js apollo vue-apollo

我正在使用 Nuxt 和 Nuxt-Apollo创建我的 Vue 应用程序。我的 nuxt.config.js 文件中有以下 apollo 配置:

apollo: {
    clientConfigs: {
      default: {
        httpEndpoint: 'http://localhost:8000/graphql/'
      },
      stage: {
        httpEndpoint: 'https://example-stage.com/graphql/'
      }
      prod: {
        httpEndpoint: 'https://example.com/graphql/'
      }
    }
  }

如何指向 stageprod 配置。每次我运行该应用程序时,它都指向 default 配置。必须有我可以设置它的地方。

最佳答案

假设您尝试访问多个客户端,而不仅仅是用于生产和开发的不同客户端,这可能会有所帮助,因为我在当前项目中使用的就是这种方法。

    apollo: {
      includeNodeModules: true, // optional, default: false (this includes graphql-tag for node_modules folder)
      authenticationType: 'Basic', // optional, default: 'Bearer'
      errorHandler: '~/apollo/customErrorHandler',
      clientConfigs: {
      default:
         {
           httpEndpoint:
             'https://me.something.com/api/graphql/query?token=******',
           httpLinkOptions: {
             credentials: 'same-origin'
           }
         },
        //  '~/apollo/clientConfig.js',
      otherClient: {
        httpEndpoint:
          'https://second-endpoint-gql.herokuapp.com/v1/graphql',
        httpLinkOptions: {
          credentials: 'same-origin'
        }
      }
    }
  },

现在您需要做的就是像往常一样进行查询,但区别在于 vue 组件。

/gql/allCars.gql

{
  allCars {
    id
    make
    model
    year
  }
}

对默认值的调用将像往常一样进行:

<script>
import allcars from '~/gql/users'
export default {
  apollo: {
    allcars: {
      prefetch: true,
      query: allcars
    }
  },
  filters: {
    charIndex (i) {
      return String.fromCharCode(97 + i)
    }
  },
  head: {
    title: ....
  },
  data () {
    return {
      ...
    }
  }
}
</script>

调用您需要添加 $client 的次要端点:

<script>
import allcars from '~/gql/users'
export default {
  apollo: {
    $client: 'otherClient',
    allcars: {
      prefetch: true,
      query: allcars
    }
  },
  filters: {
    charIndex (i) {
      return String.fromCharCode(97 + i)
    }
  },
  head: {
    title: ....
  },
  data () {
    return {
      ...
    }
  }
}
</script>

apollo 调试器似乎只查询 apollo 配置中端点列表中的最后一个端点,在我的例子中是“otherClient”。

引用我上面整合的方法:Vue multiple client

关于javascript - Nuxt : choose client config other than default,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57650714/

相关文章:

javascript - 如何模糊 HTML5 视频标签的特定区域?

javascript - 如何使用 NUXT.js 为我的传单 map 设置自定义标记图标

vue.js - 在 nuxt 生成后更改了 css

node.js - 在 Nuxt JS 中设置 Cache-Control header 适用于本地主机但不适用于生产

vue.js - 如何从nuxt.js页头的node_modules文件夹中引用js文件

javascript - 原型(prototype)和非原型(prototype)方法?

javascript - 将Vue组件绑定(bind)到vue实例

javascript - 如何从嵌套数组中拼接数组 - javascript

javascript - Vue.js : v-bind:class if an array exist and that array include something

vue.js - Vuejs v-show 没有按预期工作