我在项目中使用GraphQL和graphql-relay
,我在不同类型中使用了几个连接定义,并且围绕类型重复了一些连接定义。
我处理连接定义的第一种方法是:
import { connectionDefinitions } from 'graphql-relay';
import { CustomerType } from './Customer/types';
const { connectionType: CustomerConnectionType } = connectionDefinitions( { nodeType: CustomerType });
每次我需要连接时,它都放在不同的GraphQL类型中。
在一些问题之后,我发现我不能在整个项目中多次定义连接定义,导致Schema must contain unique named types but contains multiple types named CustomerType
。
因此,我决定构建一个中心位置来定义所有连接定义,并将其导出到名为relaydefs.js
的文件中需要它的所有GraphQL类型:
import { connectionDefinitions } from 'graphql-relay';
import { CustomerType } from './Customer/types';
import { ItemType } from './Item/types';
import { StockType } from './Stock/types';
import { PriceListType } from './PriceItem/types';
export const { connectionType: CustomerConnectionType } = connectionDefinitions( { nodeType: CustomerType });
export const { connectionType: ItemConnectionType } = connectionDefinitions( { nodeType: ItemType });
export const { connectionType: StockConnectionType } = connectionDefinitions( { nodeType: StockType });
export const { connectionType: PriceListConnectionType } = connectionDefinitions( { nodeType: PriceListType });
在每个模块上发出:
import { CustomerConnectionType } from './relaydefs';
嗯,导入无效,因为只导入了第一个项目(所有其他项目都检索undefined
)。
我确定这是一个纯粹的javascript导入问题,而不是relay或GraphQL,但我需要帮助:
a)如何正确导入每个定义连接类型?
b)在我有多种类型的连接类型的情况下,这是一个好方法吗?有更好的解决方案吗?