RelayModernGraphQLTag:期望一个请求,得到了:

问题描述 投票:0回答:1

使用新的react-relay临时版时出现此错误:

invariant.js:40 Uncaught Invariant Violation: RelayModernGraphQLTag: Expected a request, got `{"default":{"kind":"Request","fragment":{"kind":"Fragment","name":"FlowsQuery","type":"Query","metadata":null,"argumentDefinitions":[],"selections":[{"kind":"LinkedField","alias":null,"name":"flows","storageKey":null,"args":null,"concreteType":"Flow","plural":true,"selections":[{"kind":"ScalarField","alias":null,"name":"id","args":null,"storageKey":null},{"kind":"FragmentSpread","name":"Flow_flow","args":null}]}]},"operation":{"kind":"Operation","name":"FlowsQuery","argumentDefinitions":[],"selections":[{"kind":"LinkedField","alias":null,"name":"flows","storageKey":null,"args":null,"concreteType":"Flow","plural":true,"selections":[{"kind":"ScalarField","alias":null,"name":"id","args":null,"storageKey":null},{"kind":"ScalarField","alias":null,"name":"title","args":null,"storageKey":null},{"kind":"ScalarField","alias":null,"name":"description","args":null,"storageKey":null}]}]},"params":{"operationKind":"query","name":"FlowsQuery","id":null,"text":"query FlowsQuery {\n  flows {\n    id\n    ...Flow_flow\n  }\n}\n\nfragment Flow_flow on Flow {\n  id\n  title\n  description\n}\n","metadata":{}},"hash":"d6b715a97b84f420e60e112ce758e768"}}`.

我的代码:Component.tsx

  const { flows }: FlowsQueryResponse = usePreloadedQuery<FlowsQuery>(
    graphql`
      query FlowsQuery {
        flows {
          id
          # Include child fragment
          ...Flow_flow
        }
      }
    `,
    props.prepared.flowsQuery
  )

Routes.js

const routes = [{
    path: '/',
    exact: true,
    /**
     * A lazy reference to the component for the home route. Note that we intentionally don't
     * use React.lazy here: that would start loading the component only when it's rendered.
     * By using a custom alternative we can start loading the code instantly. This is
     * especially useful with nested routes, where React.lazy would not fetch the
     * component until its parents code/data had loaded. Nested route support isn't
     * implemented in our mini-router yet, but one can imagine iterating over all
     * the matched route entries and calling .load() on each of their components.
     */
    component: JSResource('Flows', () => import('./Flows/Flows')),
    /**
     * A function to prepare the data for the `component` in parallel with loading
     * that component code. The actual data to fetch is defined by the component
     * itself - here we just reference a description of the data - the generated
     * query.
     */
    prepare: params => {
      console.log('params', { params })
      const FlowsQuery = require('./__generated__/FlowsQuery.graphql')
      return {
        flowsQuery: preloadQuery(
          RelayEnvironment,
          FlowsQuery,
          {},
          // The fetchPolicy allows us to specify whether to render from cached
          // data if possible (store-or-network) or only fetch from network
          // (network-only).
          { fetchPolicy: 'store-or-network' }
        ),
      }
    },
  }]
typescript react-relay
1个回答
0
投票

以某种方式,打字稿编译器或Webpack加载器不理解在​​生成的'FlowsQuery.graphql'文件中定义的导出默认值。

因此更改此行

const FlowsQuery = require('./__generated__/FlowsQuery.graphql')

收件人

const FlowsQuery = require('./__generated__/FlowsQuery.graphql').default

已解决!

© www.soinside.com 2019 - 2024. All rights reserved.