简体   繁体   English

apollo-link-rest:使用动态字段进行查询作为响应

[英]apollo-link-rest: query with dynamic fields in response

I'm requesting a Salesforce schema data.我正在请求 Salesforce 模式数据。 It has a format它有一个格式

res = {
  Id: { name, type, label },
  IsDeleted: { type, name, label },
  MasterRecordId: { type, name, label },
 ...and so on
}

So, fields are dynamic.因此,字段是动态的。 In queries.js I'm trying to describe it在 query.js 我试图描述它

export const GET_SALESFORCE_FIELDS = gql`
  query SalesforceFields {
    salesforceFields @rest(endpoint: "schemaservice", type: "SalesforceFields", path: "/fields") {
       // What should be here??
   }
  }
`;
`

How can I describe the dynamic part?如何描述动态部分? I don't have any schema files or resolvers.我没有任何架构文件或解析器。 Only queries.js (for further requests with useQuery) and client.js (where the new ApolloClient is defined)只有 queries.js(用于使用 useQuery 的进一步请求)和 client.js(定义了new ApolloClient

"@apollo/client": "^3.0.0-rc.10",
"apollo-link-rest": "^0.8.0-beta.0"

Solved in this way:以这种方式解决:

// 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;
    }
  }
});

A reference to GitHub: https://github.com/apollographql/apollo-link-rest/issues/258参考 GitHub: https://github.com/apollographql/apollo-link-rest/issues/258

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发送带有apollo-link-rest的纯文本请求正文 - Send plain text request body with apollo-link-rest Apollo客户端:apollo-link-rest是否可以解决端点之间的关系? - Apollo Client: can apollo-link-rest resolve relations between endpoints? 我应该如何使用Apollo Client和Link Rest查询和匹配GraphQL中相同响应的数据? - How should I query and match data from the same response in GraphQL with Apollo Client and Link Rest? Apollo 客户端 graphql 查询 reactJS 中的动态字段 - Apollo client graphql query with dynamic fields in reactJS 用java动态查询graphql apollo - dynamic query graphql apollo with java 如何为每个查询设置Apollo链接URI-初始uri停留,我应该使用动态uri(角度8) - How to set Apollo link URI per query - initial uri stays, should I use dynamic uri (angular 8) 对 Apollo 突变的响应可以省略字段吗? - Can a response to an Apollo mutation omit fields? 阿波罗客户端:带有@client 查询字段的codegen 打字稿 - apollo client:codegen typescript with @client query fields Graphql 响应不是来自 Apollo 客户端查询 - Graphql response not coming from Apollo client query GraphQL 在查询中标记动态表名 (apollo) - GraphQL Tag dynamic table name in query (apollo)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM