From 58af4b046542be5ce0eedd0f1d07f0aa12b377e1 Mon Sep 17 00:00:00 2001 From: Mikael Brevik Date: Fri, 29 Sep 2023 14:24:27 +0200 Subject: [PATCH] docs: adds example of composed clients --- src/modules/api-server/README.md | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/modules/api-server/README.md b/src/modules/api-server/README.md index df6dfde7..94c6f3df 100644 --- a/src/modules/api-server/README.md +++ b/src/modules/api-server/README.md @@ -64,4 +64,45 @@ export default graphQlApiHandler(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.