我正在请求 Salesforce 架构数据。 它有一个格式
res = {
Id: { name, type, label },
IsDeleted: { type, name, label },
MasterRecordId: { type, name, label },
...and so on
}
因此,字段是动态的。
在querys.js我试图描述它
export const GET_SALESFORCE_FIELDS = gql`
query SalesforceFields {
salesforceFields @rest(endpoint: "schemaservice", type: "SalesforceFields", path: "/fields") {
// What should be here??
}
}
`;
`
我如何描述动态部分? 我没有任何架构文件或解析器。 只有querys.js(用于使用useQuery的进一步请求)和client.js(定义了新的ApolloClient)
"@apollo/client": "^3.0.0-rc.10",
"apollo-link-rest": "^0.8.0-beta.0",
@ssuvorov——你确认网络调用成功了吗?
如果是,那么仅基于您的示例响应:我会执行以下操作:
export const GET_SALESFORCE_FIELDS = gql`
query SalesforceFields {
salesforceFields @rest(endpoint: "schemaservice", type: "SalesforceFields", path: "/fields") {
Id {
type, name, label
}
IsDeleted {
type, name, label
}
# …remaining fields
}
}
`;
-- 现在,这可能不起作用,因为 GraphQL 期望每个类型都有一个名称,因此您可能需要更改此名称:
export const GET_SALESFORCE_FIELDS = gql`
query SalesforceFields {
salesforceFields @rest(endpoint: "schemaservice", type: "SalesforceFields", path: "/fields") {
- Id {
+ Id @type(name: "FieldDescriptor") {
type, name, label
}
- IsDeleted {
+ IsDeleted @type(name: "FieldDescriptor") {
type, name, label
}
# …remaining fields
}
}
`;
(另一种方法是使用 TypePatcher - 文档中记录)
如果每个条目都是相同的对象模式,您甚至可以使用 Fragment 来帮助您:
export const GET_SALESFORCE_FIELDS = gql`
query SalesforceFields {
salesforceFields @rest(endpoint: "schemaservice", type: "SalesforceFields", path: "/fields") {
Id @type(name: "FieldDescriptor") {
- type, name, label
+ ...FieldFrag
}
IsDeleted @type(name: "FieldDescriptor") {
- type, name, label
+ ...FieldFrag
}
# …remaining fields
}
+ fragment FieldFrag on FieldDescriptor {
+ type
+ name
+ label
+ }
}`;
如果您以通用方式使用 Salesforce,并且您不知道将有多少列,那么它会变得更复杂一些,您需要使用类型修补程序来更改列的形状应用程序接口。
我不使用 Salesforce——但如果是我,我会考虑询问我的 Salesforce 代表他们是否有官方的 GraphQL 产品,或者他们是否有一个幸运的 3rd 方产品来获得它。
一个快速的谷歌搜索显示它存在: https :
@fbartho 非常感谢您的回复。 不幸的是,它有 500 多个字段。 好吧,我会尝试与 SF 团队进行澄清。
如果您需要处理响应以使其更容易一些,您可以使用 typenamePatcher,它会给您一个钩子来完全重塑响应。
更好/更通用的模式实际上可能如下所示:
type MyResponse (
columns: [Column!]!
}
type Column {
name: String!
type: String!
label: String!
}
@fbartho感谢您的帮助。 我就是这样解决的。
// queries.js
export const GET_SALESFORCE_FIELDS = gql`
query SalesforceFields {
salesforceFields @rest(endpoint: "schemaservice", type: "SalesforceFieldsPayload", path: "/fields") {
items @type(name: "Salesforce")
}
}
`;
// client.js
const restLink = new RestLink({
...,
typePatcher: {
SalesforceFieldsPayload: (
data,
outerType,
patchDeeper
) => {
if (data != null) {
data.items = Object.keys(data).map(field => ({ __typename: "Salesforce", ...data[field] }));
}
return data;
}
}
});