2

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

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

我怎样才能指向stageprod配置。每次我运行应用程序时,它都指向default配置。必须有一个地方我可以设置它。

4

1 回答 1

6

假设您尝试访问多个客户端,而不仅仅是产品和开发的不同客户端,这可能会有所帮助,就像我在当前项目中使用的一样。

    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>

毫无价值的是,阿波罗调试器似乎只查询阿波罗配置中端点列表中的最后一个端点,在我的例子中是“其他客户端”。

参考我上面的集成是怎么来的:Vue 多客户端

于 2019-11-19T16:36:47.407 回答