Skip to content

Commit

Permalink
docs: adds example of composed clients
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr committed Sep 29, 2023
1 parent e9e542c commit 58af4b0
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/modules/api-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,45 @@ export default graphQlApiHandler<string>(function (req, res, { client, ok }) {
});
```

```ts
// Combining external resources

const myHttpApiCreator = createExternalClient('http-entur', function (request) {
return {
myFunction: (a: string) => request(`/geocoder?q=${a}`),
};
});

const myGraphQL = createExternalClient(
'graphql-journeyPlanner3',
function (client) {
return {
myGraphQLQuery: (input: string) => client.query(`MY QUERY HERE ${input}`),
};
},
);

const combinedFactories = composeClientFactories(myHttpApiCreator, myGraphQL);

export const ssrHttpClientDecorator =
createWithExternalClientDecorator(combinedFactories);

// inside Page

// This will be removed to the client, only executed on SSR
export const getServerSideProps = ssrHttpClientDecorator(async function ({
client,
}) {
const result = await client.myFunction('Kongens gate');
const result2 = await client.myGraphQLQuery('Kongens gate');

return {
props: {
autocompleteFeatures: result,
data: result2,
},
};
});
```

See tests for more examples.

0 comments on commit 58af4b0

Please sign in to comment.