Documentation
Feedback
Guides
VTEX IO Apps

VTEX IO Apps
Functional Apps
Session Client
Official extension
Version: 1.0.5
Latest version: 1.0.5

The Session Client app provides React hooks and GraphQL queries for your components to read and update the VTEX Session cookie, responsible for saving data of a specific session of a user browsing in your store.

Installation

In your React app's manifest.json file, add the Session Client app to the dependency list:


_10
"dependencies": {
_10
"vtex.session-client": "1.x"
_10
}

You can have full TypeScript support running vtex setup --typings in your CLI afterwards.

Configuration

The Session Client's React hooks allow you to read and update the VTEX Session cookie as desired. On the other hand, the GraphQL query and mutation enable your app to fetch and change the current user session.

React hooks

To read the VTEX Session cookie:

To update the VTEX Session cookie:

useRenderSession hook

This hook is the fastest way to access session data, using the session response from render-session. One caveat: the session values are limited to a set of values. If you need fields that are not in this set, you can use useFullSession or useLazyFullSession.


_20
import React from 'react'
_20
import { useRenderSession } from 'vtex.session-client'
_20
_20
function MyComponent() {
_20
const { loading, session, error } = useRenderSession()
_20
_20
if (loading) {
_20
return \<\>Session is loading</>
_20
}
_20
_20
if (error) {
_20
return \<\>Session has errors</>
_20
}
_20
_20
console.log({ session })
_20
_20
return \<\>Session is ready</>
_20
}
_20
_20
export default MyComponent

useFullSession hook

It's not possible to return the session during Server Side Rendering since it is a private query.

Runs a GraphQL query on the client side to query the full user session.

Under the hood, it's a wrapper of React Apollo's useQuery passing the GraphQL session query. You can read more about the useQuery API here.


_20
import React from 'react'
_20
import { useFullSession } from 'vtex.session-client'
_20
_20
function MyComponent() {
_20
const { loading, data, error } = useFullSession()
_20
_20
if (loading) {
_20
return \<\>Session is loading</>
_20
}
_20
_20
if (error) {
_20
return \<\>Session has errors</>
_20
}
_20
_20
console.log({ session: data?.session })
_20
_20
return \<\>Session is ready</>
_20
}
_20
_20
export default MyComponent

It also accepts a GraphQL variable called items which is an array of strings. These strings should match attributes inside the Session object, and only those attributes will be then fetched and returned.

For example:


_10
useFullSession({
_10
variables: {
_10
items: ['store.channel', 'store.countryCode']
_10
}
_10
})

useLazyFullSession hook

The same as useFullSession, but it uses React Apollo's useLazyQuery hook instead. You can read more about useLazyQuery API here.


_12
import React from 'react'
_12
import { useLazyFullSession } from 'vtex.session-client'
_12
_12
function MyComponent() {
_12
const [getSession, session] = useLazyFullSession()
_12
_12
console.log({ session })
_12
_12
return <button onClick={() => getSession()}>Get session</button>
_12
}
_12
_12
export default MyComponent

It also accepts a GraphQL variable called items, which is an array of strings. These strings should match attributes inside the Session object, and only those attributes will be then fetched and returned.

For example:


_10
useLazyFullSession({
_10
variables: {
_10
items: ['store.channel', 'store.countryCode']
_10
}
_10
})

useUpdateSession hook

Updates the values of a session. Under the hood, it uses React Apollo's useMutation hook. You can read more about useMutation API here.

Unlike the useMutation hook, this one only returns the mutation function (called in the example below as updateSession) — it does not return the mutation result.

After calling the mutation function, the hook reloads the page, guaranteeing that the whole page data is updated to the new session parameters. This is extremely useful in pages where the content changes according to the session values, such as the search results.


_22
import React from 'react'
_22
import { useUpdateSession } from 'vtex.session-client'
_22
_22
function MyComponent() {
_22
const updateSession = useUpdateSession()
_22
_22
return (
_22
<button
_22
onClick={() =>
_22
updateSession({
_22
variables: {
_22
fields: { foo: 'bar', baz: 123 },
_22
},
_22
})
_22
}
_22
>
_22
Update session
_22
</button>
_22
)
_22
}
_22
_22
export default MyComponent

useUpdateSessionInline hook

Updates the values of a session. Under the hood, it uses React Apollo's useMutation hook. You can read more about useMutation API here.

Differently from the useUpdateSession, this hook will not reload the page after calling the mutation function.


_24
import React from 'react'
_24
import { useUpdateSessionInline } from 'vtex.session-client'
_24
_24
function MyComponent() {
_24
const [updateSession, updatedSession] = useUpdateSessionInline()
_24
_24
console.log({ updatedSession })
_24
_24
return (
_24
<button
_24
onClick={() =>
_24
updateSession({
_24
variables: {
_24
fields: { foo: 'bar', baz: 123 },
_24
},
_24
})
_24
}
_24
>
_24
Update session
_24
</button>
_24
)
_24
}
_24
_24
export default MyComponent

It also accepts a GraphQL variable called items, which is an array of strings. These strings should match attributes inside of the Session object, and only those attributes will be then fetched and returned.

For example:


_10
updateSession({
_10
variables: {
_10
fields: { foo: 'bar', baz: 123 },
_10
items: ['store.channel', 'store.countryCode']
_10
}
_10
})

GraphQL query and mutation

session query

Gets the current user session.


_12
query session($items: [String]) {
_12
session(items: $items) @context(provider: "vtex.session-client") {
_12
... on SessionSuccess {
_12
id
_12
namespaces
_12
}
_12
... on SessionError {
_12
type
_12
message
_12
}
_12
}
_12
}

updateSession mutation

Changes the current user session using the following variables: { "fields": { "foo": 123, "baz": "abc" } }


_13
mutation updateSession($fields: SessionFieldsJSONInput!, $items: [String]) {
_13
updateSession(fields: $fields, items: $items)
_13
@context(provider: "vtex.session-client") {
_13
... on SessionSuccess {
_13
id
_13
namespaces
_13
}
_13
... on SessionError {
_13
type
_13
message
_13
}
_13
}
_13
}

See also
VTEX App Store
VTEX IO Apps
Session Client Graphql API
VTEX IO Apps