Transform apollo-datasource-rest response structure to adapt to the already defined remote schema

688 Views Asked by At

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?

E.g REST API response:

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

Our remote graphql schema:

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

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

1

There are 1 best solutions below

0
On BEST ANSWER

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.

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.