转换apollo-datasource-rest响应结构以适应已定义的远程模式 [英] Transform apollo-datasource-rest response structure to adapt to the already defined remote schema

查看:183
本文介绍了转换apollo-datasource-rest响应结构以适应已定义的远程模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经有了远程模式.通过使用apollo-datasource-rest来访问REST API,我们可以获得Apollo数据源响应. json响应的格式与架构不匹配.任务是转换json响应以匹配模式,并根据用户查询返回结果.如何实现呢?

We have remote schema already. We get Apollo datasource response by hitting the REST APIs using apollo-datasource-rest. The format of the json response does not match the schema. The task is to transform the json response to match the schema and return the result against the user query. How to achieve this?

例如 REST API响应:

E.g REST API response:

{
    "members": {
        "name": "john",
        "street_address": "10 Barley St."
    }
}

我们的远程graphql模式:

Our remote graphql schema:

type Users {
    username: String!
    street: String!
}

任何想法,帮助或源代码都将受到高度赞赏. 再次感谢.

Any ideas or help or source code is highly appreciated. Thanks again.

推荐答案

您可以将REST API响应的字段转换为GraphQL resolver中的GraphQL模式.或者,将这些转换操作委托给模型层.这是一种通用方法.

You can transform the fields of REST API response to GraphQL Schema in GraphQL resolver. Or, delegate these transform operations to the model layer. This is a general approach.

如果您认为手动转换解析器或模型层中的字段太冗长了.

If you think it's too verbose to transform the fields in resolvers or model layer manually.

您可以使用诸如 type-graphql 之类的库来执行此操作.它结合了您的模型层和GraphQL模式层.它将模型的字段映射到GraphQL模式字段.

You can use some library like type-graphql to do this. It combines your model layer and the GraphQL schema layer. It maps the fields of model to GraphQL schema fields.

这篇关于转换apollo-datasource-rest响应结构以适应已定义的远程模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆