diff --git a/docs/guides/mustache.md b/docs/guides/mustache.md index 3094e25402..30dbec204e 100644 --- a/docs/guides/mustache.md +++ b/docs/guides/mustache.md @@ -17,7 +17,7 @@ To get a list of todos you will run the query `todo` defined below. query: Query } ``` -In this example, the `@upstream` directive is used to specify the baseURL as "https://jsonplaceholder.typicode.com". This baseURL will be used as the base URL for all HTTP requests defined within the schema. +In this example, the `@upstream` operator is used to specify the baseURL as "https://jsonplaceholder.typicode.com". This baseURL will be used as the base URL for all HTTP requests defined within the schema. ```graphql type Query { @@ -55,27 +55,29 @@ type Post { In this example `value.id` is used to access the id field of the current Post. When querying for comments for a specific post, the Mustache template `/posts/{{value.id}}/comments` dynamically incorporates the id into the URL. For instance, if you run a query for a post with ID 1, the resulting HTTP request path becomes `/posts/1/comments`, fetching comments associated with that post. -### Customization in Queries +### Dynamic Authentication Headers -Imagine a scenario where you need to fetch paginated data from an API +Suppose you have an API that requires authentication through a token. You can use Mustache templates to dynamically include the authentication token in the HTTP request headers ```graphql type Query { - paginatedPosts(page: Int!): [Post] @http(path: "/posts", query: [{key: "page", value: "`{{args.page}}`"}]) + protectedResource: [Resource] + @http( + path: "/protected/resource", + headers: [{key: "Authorization", value: "Bearer {{env.AUTH_TOKEN}}"}] + ) } -``` - -when you run the query `paginatedPosts` it accepts a crucial argument, page, enabling the selection of specific pages of posts.Utilizing a Mustache template `/posts?page={{args.page}}`, the query dynamically generates a URL structure. When calling `paginatedPosts(page: 2)`, for instance, this template dynamically forms the URL `/posts?page=2`. This crafted URL then instructs the API to provide the posts located on the second page. -The above example can also be constructed by dynamically incorpating Mustache templates directly into the path field. - -```graphql -type Query { - paginatedPosts(page: Int!): [Post] @http(path: "/posts?page={{args.page}}") +type Resource { + id: ID! + name: String! } ``` +In this example, the `protectedResource` query fetches data from an API endpoint that requires authentication. The `Authorization` header is included in the HTTP request with the value `Bearer {{env.AUTH_TOKEN}}`. + +Here, `env.AUTH_TOKEN` holds the authentication token. With Mustache templates, we dynamically insert the token into the header, ensuring proper authentication with the token from the environment. -When executing `paginatedPosts(page: 2)`it constructs a dynamic path: `/posts?page=2`. Here, the Mustache template `/posts?page={{args.page}}` inserts the provided page number directly into the path(in our case 2), guiding the API to retrieve posts from the specified page. This flexibility allows for dynamic path creation, tailored to the required page, through the use of Mustache templates. +This approach allows for secure and dynamic handling of authentication tokens in API requests, enhancing security and flexibility in API configurations. ### Dynamic input @@ -114,4 +116,19 @@ type Query { In this scenario, Mustache templates are employed to integrate environment variables( [env variables in tailcall](environment-variables.md) ), specifically `API_BASE_URL`, into the base URL across multiple API endpoints. By referencing `{{env.API_BASE_URL}}`, the schema standardizes the base URL for both `users` and `posts` endpoints, ensuring any modifications to the environment variable automatically reflect across all API calls involving these queries. This approach enhances maintainability by allowing global changes to the base URL through the environment variable, promoting consistency in configuration management. +### Places to Use Mustache Templates + +Mustache templates can be used in various places within Tailcall configurations, including: + +- URL paths and query parameters in [@http](operators/http.md) operators. +- Fields like method names and message payloads in [@grpc](operators/grpc.md) operators. +- Source URLs or file paths in [@link](operators/link.md) operators. +- Attributes of fields or nodes in [@modify](operators/modify.md) operators. +- Environment variables and predefined constants used throughout the configuration [(see more)](environment-variables.md) +- Configuration values for server settings, such as workers, port, cache control headers, and more in [@server](operators/server.md) operator. + +For a comprehensive understanding of where mustache templates can be used, refer to the [context](context.md) guide. + +### Conclusion + Using Mustache templates in Tailcall lets you create flexible and powerful API configurations. They are like tools that help developers build strong and adaptable systems by allowing dynamic adjustments in how APIs are set up. When used wisely and with smart thinking, Mustache templates make it easier to create strong and adaptable systems for your APIs.