Problem: Build a Vendor-Agnostic API Client Factory
Problem statement
You’re integrating with multiple third-party APIs. Depending on the config, your app needs to use either a REST, GraphQL, or SOAP client implementation. You want to hide this complexity behind a factory that takes a string, type, and returns the corresponding client. The system should remain open to adding more client types in the future, without requiring edits to multiple places.
Goal
Implement createApiClient(type) that returns an instance of RestClient, GraphQLClient, or SoapClient, based on the type string passed.
Constraints
Do not use conditionals (
if,switch, ternaries) to select which class to instantiate.Only the factory knows how to select and instantiate the client.
Assume only those 3 client types exist for now.
Sample output
The examples below illustrate what the output should look like:
const graphqlClient = createApiClient('graphql');console.log(graphqlClient.fetch('/users'));/* Expected output:GraphQL querying /users */const restClient = createApiClient('rest');console.log(restClient.fetch('/posts'));/* Expected output:REST fetching /posts */const soapClient = createApiClient('soap');console.log(soapClient.fetch('/orders'));/* Expected output:SOAP invoking /orders */
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Build a Vendor-Agnostic API Client Factory
Problem statement
You’re integrating with multiple third-party APIs. Depending on the config, your app needs to use either a REST, GraphQL, or SOAP client implementation. You want to hide this complexity behind a factory that takes a string, type, and returns the corresponding client. The system should remain open to adding more client types in the future, without requiring edits to multiple places.
Goal
Implement createApiClient(type) that returns an instance of RestClient, GraphQLClient, or SoapClient, based on the type string passed.
Constraints
Do not use conditionals (
if,switch, ternaries) to select which class to instantiate.Only the factory knows how to select and instantiate the client.
Assume only those 3 client types exist for now.
Sample output
The examples below illustrate what the output should look like:
const graphqlClient = createApiClient('graphql');console.log(graphqlClient.fetch('/users'));/* Expected output:GraphQL querying /users */const restClient = createApiClient('rest');console.log(restClient.fetch('/posts'));/* Expected output:REST fetching /posts */const soapClient = createApiClient('soap');console.log(soapClient.fetch('/orders'));/* Expected output:SOAP invoking /orders */
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.