You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
excerpt: "Learn to implement and use protected GraphQL operations with IO apps."
6
6
createdAt: "2025-06-30T14:00:00.000Z"
7
-
updatedAt: "2025-06-30T14:00:00.000Z"
7
+
updatedAt: "2025-09-30T11:00:00.000Z"
8
8
---
9
-
Authenticated GraphQL requests are crucial for securing access to your VTEX IO app's data and functionalities. Authentication ensures that only authorized users or applications can interact with your GraphQL endpoints, protecting sensitive information and preventing unauthorized actions. Besides authentication, app developers can implement authorization to control who can access specific resources and what operations they can perform.
9
+
Authenticated GraphQL requests are crucial for securing access to your VTEX IO app's data and functions. Authentication ensures that only authorized users or applications can interact with your GraphQL endpoints, protecting sensitive information and preventing unauthorized actions. In addition to authentication, app developers can implement authorization to control who can access specific resources and what operations they can perform.
10
10
11
-
GraphQL authorization in VTEX IO apps works with auth tokens and [directives](https://graphql.org/learn/schema/#directives) within your GraphQL schema, enabling fine-grained access control based on [License Manager resources](https://help.vtex.com/en/tutorial/license-manager-resources--3q6ztrC8YynQf6rdc6euk3). The app identifies the requester by the [auth token](https://developers.vtex.com/docs/guides/app-authentication-using-auth-tokens) in [apps](https://developers.vtex.com/docs/guides/vtex-io-documentation-what-is-a-vtex-app) or the [user token](https://developers.vtex.com/docs/guides/api-authentication-using-user-tokens) in API calls, which will have [roles](https://help.vtex.com/en/tutorial/roles--7HKK5Uau2H6wxE1rH5oRbc) or [policies](https://developers.vtex.com/docs/guides/vtex-io-documentation-policies) associated with VTEX's resources.
11
+
GraphQL authorization in VTEX IO apps works with auth tokens and [directives](https://graphql.org/learn/schema/#directives) within your GraphQL schema, enabling fine-grained access control based on [License Manager resources](https://help.vtex.com/en/tutorial/license-manager-resources--3q6ztrC8YynQf6rdc6euk3). The app identifies the requester by the [auth token](https://developers.vtex.com/docs/guides/app-authentication-using-auth-tokens) in [apps](https://developers.vtex.com/docs/guides/vtex-io-documentation-what-is-a-vtex-app) or the [user token](https://developers.vtex.com/docs/guides/api-authentication-using-user-tokens) in API calls. These tokens have [roles](https://help.vtex.com/en/tutorial/roles--7HKK5Uau2H6wxE1rH5oRbc) or [policies](https://developers.vtex.com/docs/guides/vtex-io-documentation-policies) associated with VTEX resources.
12
12
13
13
In this guide, you will learn how to:
14
14
15
-
1. Implement protected operations with GraphQL APIs in apps using authorization.
16
-
2. Make requests in apps with the proper authorization.
17
-
3. Make external requests with the appropriate authorization.
15
+
1. Implement protected GraphQL API operations in apps using authorization.
16
+
2. Make authorized requests from apps.
17
+
3. Make external requests with appropriate authorization.
18
+
19
+
> ℹ️ Only VTEX apps, Admin users, and [application keys](https://developers.vtex.com/docs/guides/api-authentication-using-api-keys) can be authorized to make GraphQL requests, as they must be associated with a role or policy that grants access to a License Manager resource. Store users don't have this type of access.
18
20
19
-
> ℹ️ Only VTEX apps, Admin users, and [application keys](https://developers.vtex.com/docs/guides/api-authentication-using-api-keys) can be authorized in GraphQL requests, since they must be associated with a role or policy granting access to a License Manager resource. Store users don't have this type of access.
20
21
## Implementing GraphQL API authorization in apps
21
22
22
-
By default, GraphQL APIs in VTEX apps don't require authorization. To enable authorization, you must add the `@auth` directive to the specific fields or operations in your GraphQL schema that require protection. This directive ensures that only requesters with access to the corresponding [License Manager resource](https://help.vtex.com/en/tutorial/license-manager-resources--3q6ztrC8YynQf6rdc6euk3) are allowed to execute the operation.
23
+
The `@auth` directive has different implementation methods depending on the builder version.
24
+
25
+
For the builder version `1.x`, the directive is optional. If a query or mutation doesn't have the directive, it's considered public by default. For the builder version `2.x`, the directive is mandatory.
26
+
27
+
To enable authorization, you must add the `@auth` directive to the specific fields or operations in your GraphQL schema that need protection. This directive ensures that only requesters with access to the corresponding [License Manager resource](https://help.vtex.com/en/tutorial/license-manager-resources--3q6ztrC8YynQf6rdc6euk3) are allowed to execute the operation.
23
28
24
29
The `@auth` directive uses the following arguments:
25
30
26
-
-`productCode`: The numeric code that identifies the product in License Manager (for example, `"66"` for **VTEX IO**).
27
-
-`resourceCode`: The License Manager resource identifier (for example, `"workspace-read-write"` for **Workspace CRUD**).
The authorization scope of the query or mutation. It has two possible values:
44
+
<ul>
45
+
<li><code>PUBLIC</code>: The query is public, so it doesn't require any authorization. This scope is mostly used for shoppers browsing a store.</li>
46
+
<li><code>PRIVATE</code>: The query is private, which requires authorization for a specific License Manager resource and product. This scope should be used for scenarios other than store browsing by a shopper, such as in the Admin.</li>
47
+
</ul>
48
+
</td>
49
+
<td>Works only on version <code>2.x</code>, and is mandatory.</td>
50
+
</tr>
51
+
<tr>
52
+
<td><code>productCode</code></td>
53
+
<td>The numeric code that identifies the product in License Manager (for example, <code>"66"</code> for <b>VTEX IO</b>). See the product codes in the <b>Product ID</b> column of the table in <ahref="https://help.vtex.com/en/tutorial/license-manager-resources--3q6ztrC8YynQf6rdc6euk3"target="_blank">License Manager resources</a>.</td>
54
+
<td>Works on versions <code>1.x</code> and <code>2.x</code>. For <code>2.x</code>, this argument is mandatory if scope is set to <code>PRIVATE</code>.</td>
55
+
</tr>
56
+
<tr>
57
+
<td><code>resourceCode</code></td>
58
+
<td>The License Manager resource identifier (for example, <code>"workspace-read-write"</code> for <b>Workspace CRUD</b>). See the resource codes in the <b>Key</b> column of the table in <ahref="https://help.vtex.com/en/tutorial/license-manager-resources--3q6ztrC8YynQf6rdc6euk3"target="_blank">License Manager resources</a>.</td>
59
+
<td>Works on versions <code>1.x</code> and <code>2.x</code>. For <code>2.x</code>, this argument is mandatory if scope is set to <code>PRIVATE</code>.</td>
IfadevelopertriestobuildanappwiththeGraphQLbuilder `2.x`, the [VTEXIOCLI](https://developers.vtex.com/docs/guides/vtex-io-documentation-vtex-io-cli-installation-and-command-reference) willshowanerrormessageinthefollowingcases:
102
+
103
+
- Anyqueryormutationlacksthe `@auth` directive.
104
+
- The `scope` argumentismissinginthedirective.
105
+
- When `scope` is `PRIVATE` inthedirectiveandthe `productCode` or `resourceCode` argumentsaremissing.
@@ -68,17 +136,17 @@ For details about token types, see [App authentication using auth tokens](https:
68
136
69
137
> ⚠️ Use the user token (Admin) whenever possible to properly identify the requester. The app token (`ctx.authToken`) should only be used when the user can't access the requested service.
70
138
71
-
## Making requests to protected GraphQL API apps
139
+
## Making requests to protected GraphQL APIs of apps
72
140
73
-
When making external requests to a GraphQL API app, declare the [user token](https://developers.vtex.com/docs/guides/api-authentication-using-user-tokens) in the `VtexIdClientAutCookie` HTTP header. The requester must have a role that includes the resource and product defined in the endpoint's `@auth` directive.
141
+
When making external requests to a GraphQL API of an app, include the [user token](https://developers.vtex.com/docs/guides/api-authentication-using-user-tokens) in the `VtexIdclientAutCookie` HTTP header. The requester must have a role that includes the resource and product defined in the endpoint's `@auth` directive.
Copy file name to clipboardExpand all lines: docs/guides/vtex-io/Reference/concepts/vtex-io-documentation-builders/vtex-io-documentation-graphql-builder.md
The `graphql` builder handles the GraphQL app [schema](https://graphql.org/learn/schema/) and serves as an interface between frontend applications and backend [services](https://developers.vtex.com/docs/guides/vtex-io-documentation-service). This guide provides a comprehensive overview of how to use this builder effectively. For more information, check [Using GraphQL to retrieve data from Master Data](https://developers.vtex.com/docs/guides/services-6-graphql-retrieving-data-from-master-data) and the [official GraphQL documentation](https://graphql.com/learn/what-is-graphql/).
11
+
The `graphql` builder handles the GraphQL app [schema](https://graphql.org/learn/schema/) and serves as an interface between frontend applications and backend [services](https://developers.vtex.com/docs/guides/vtex-io-documentation-service). This guide provides a comprehensive overview of how to use this builder effectively. For more information, see [Developing a GraphQL API in service apps](https://developers.vtex.com/docs/guides/developing-a-graphql-api-in-service-apps) and the [official GraphQL documentation](https://graphql.com/learn/what-is-graphql/).
12
12
13
13
> ℹ️ The `graphql` builder is used only when developing an app's GraphQL API. You don't need this builder to consume GraphQL APIs in IO apps.
14
14
15
+
## Versioning
16
+
17
+
The `graphql` builder is available in the following versions:
18
+
19
+
| Builder version | Mandatory `@auth` directive\*| Status |
20
+
| :- | :- | :- |
21
+
|`1.x`| No | Unavailable for new apps and new major versions of existing apps. Available for linking apps and publishing new minor and patch [versions](https://developers.vtex.com/docs/guides/vtex-io-documentation-releasing-a-new-app-version#understanding-app-versioning) of existing apps until January 7, 2026. |
22
+
|`2.x`| Yes | Active |
23
+
24
+
\* The `@auth` directive adds an authorization step to GraphQL queries and mutations. For more details, see [GraphQL authorization in IO apps](https://developers.vtex.com/docs/guides/graphql-authorization-in-io-apps).
25
+
26
+
> ⚠️ Apps using deprecated builder versions can still run on the VTEX platform, but developers can't build or create new app versions using these builder versions.
27
+
15
28
## Folder structure
16
29
17
-
The `graphql` builder uses a `graphql` folder in the app’s root folder. This folder contains `.graphql` files with the schema definitions. Developers can structure the whole schema in a single file or split it into multiple files and subfolders.
30
+
The `graphql` builder uses a `graphql` folder in the app's root folder. This folder contains `.graphql` files with the schema definitions. Developers can structure the whole schema in a single file or split it into multiple files and subfolders.
18
31
19
-
Our recommendation is to:
32
+
We recommend you to:
20
33
21
34
- Define only the endpoints ([queries](https://graphql.org/learn/queries/), [mutations](https://graphql.org/learn/mutations/), and [subscriptions](https://graphql.org/learn/subscriptions/)) in `schema.graphql`.
22
35
- Handle [directives](https://graphql.org/learn/schema/#directives) separately in `directives.graphql`.
@@ -35,13 +48,15 @@ graphql
35
48
36
49
To develop an app using the `graphql` builder, follow the steps below:
37
50
38
-
1.**Start with a template:** Download an app template such as [`graphql-example`](https://github.com/vtex-apps/graphql-example) or create a new project using the [`vtex init CLI command`](https://developers.vtex.com/docs/guides/vtex-io-documentation-vtex-io-cli-usage#starting-a-new-project) and choose the `graphql-example` option.
39
-
2.**Add the schema files**: Add the `.graphql` files with the schema definitions, including the endpoints (queries, mutations, and subscriptions), directives, and other types you want to define.
40
-
3.**Add the API implementation logic**: Add the code that implements and instantiates the resolver functions. There's an example available in [Using GraphQL to retrieve data from Master Data](https://developers.vtex.com/docs/guides/services-6-graphql-retrieving-data-from-master-data).
41
-
1.**Create the resolvers**: Create the resolver functions that will run when your GraphQL endpoints are called. The functions must have the same names as the endpoints defined in the schema. If you're developing a Node service, you should add the TypeScript code (`.ts` files) in the `node/resolvers` folder.
42
-
2.**Instantiate the resolvers**: Add the code to instantiate the resolvers. If you're developing a Node service, in the `Service` class of the `node/index.ts` file, you should add a `graphql` field with the resolvers for each endpoint implemented along with the directives.
51
+
1.**Start with a template:** Download an app template such as [`graphql-example`](https://github.com/vtex-apps/graphql-example) or create a new project using the [`vtex init` CLI command](https://developers.vtex.com/docs/guides/vtex-io-documentation-vtex-io-cli-usage#starting-a-new-project) and select the `graphql-example` option.
52
+
2.**Add the schema files:** Add the `.graphql` files with the schema definitions, including the endpoints (queries, mutations, and subscriptions), directives, and other custom types.
53
+
3.**Add the API implementation logic**: Add the code that implements and instantiates the resolver functions.
54
+
1.**Create the resolvers**: Create the resolver functions that will run when your GraphQL endpoints are called. The functions must have the same names as the endpoints defined in the schema. If you're developing a Node service, add the TypeScript code (`.ts` files) in the `node/resolvers` folder.
55
+
2.**Instantiate the resolvers**: Add the code to instantiate the resolvers. If you're developing a Node service, in the `node/index.ts` file, within the `Service` class, you should add a `graphql` field that includes the resolvers for each implemented endpoint and the directives.
43
56
4.**Testing**: [Link the app](https://developers.vtex.com/docs/guides/vtex-io-documentation-linking-an-app) to a development workspace for testing. Test the endpoints with the [GraphQL IDE](https://developers.vtex.com/docs/guides/graphql-ide).
44
57
58
+
For a more detailed tutorial on implementing an app with this builder, see [Developing a GraphQL API in service apps](https://developers.vtex.com/docs/guides/developing-a-graphql-api-in-service-apps).
59
+
45
60
## Use case examples
46
61
47
62
Below are some app examples that use the `graphql` builder:
0 commit comments