Documentation
Feedback
Guides
Learning Center

Learning Center
Learning Center
Connecting to a Service
View in Portuguese
This content was migrated from the VTEX Learning Center and is no longer being actively maintained. For the most accurate and up-to-date information, please check the official documentation.

Introduction

An admin app ends up losing its meaning if it cannot consume data. So, in the last step of the course, we will learn how to connect your admin app to a service and then use it in the front-end of the application.

Activity

  1. Start by adding the required builders for service:

/manifest.json


_11
{
_11
...
_11
"builders": {
_11
"react": "3.x",
_11
"messages": "1.x",
_11
"docs": "0.x",
_11
"admin": "0.x",
_11
+ "node": "6.x",
_11
+ "graphql": "1.x"
_11
}
_11
}

  1. Create a graphql/ folder and add a simple schema.graphql file with a single query:

/graphql/schema.graphql


_10
type Query {
_10
helloworld: String
_10
}

  1. Create a node/ folder and add an index.ts file to it that will instantiate our service:

/node/index.ts


_11
import { Service } from '@vtex/api'
_11
_11
export default new Service({
_11
graphql: {
_11
resolvers: {
_11
Query: {
_11
helloworld: () => `Service number: ${Math.random()}`,
_11
},
_11
},
_11
},
_11
})

Our service will then, in the query helloworld, return a random number.

  1. In the /node folder so that you can develop well locally, you will need apackage.json, you can add a simple one:

/node/package.json


_21
{
_21
"dependencies": {
_21
"co-body": "^6.0.0",
_21
"ramda": "^0.25.0"
_21
},
_21
"devDependencies": {
_21
"@types/co-body": "^0.0.3",
_21
"@types/jest": "^24.0.18",
_21
"@types/node": "^12.0.0",
_21
"@types/ramda": "types/npm-ramda#dist",
_21
"@vtex/api": "6.36.2",
_21
"@vtex/test-tools": "^1.0.0",
_21
"tslint": "^5.14.0",
_21
"tslint-config-vtex": "^2.1.0",
_21
"typescript": "3.8.3"
_21
},
_21
"scripts": {
_21
"lint": "tsc --noEmit --pretty && tslint -c tslint.json --fix './**/*.ts'"
_21
},
_21
"version": "0.0.7"
_21
}

  1. In the react/ folder we will need to define a query to be able to use the resolver that we defined in the service. To do this, create a graphql/ folder inside the react/ folder and in this folder, create a helloworld.gql with:

/react/graphql/helloworld.gql:


_10
query hello {
_10
helloworld
_10
}

  1. Finally, we need to add this query to our component in adminExample.ts

_20
import React, { FC } from 'react'
_20
import { Layout, PageBlock } from 'vtex.styleguide'
_20
+import { useQuery } from 'react-apollo'
_20
_20
+import helloworld from './graphql/helloworld.gql'
_20
_20
const AdminExample: FC = () => {
_20
+ const { data } = useQuery(helloworld)
_20
_20
return (
_20
<Layout>
_20
<PageBlock title="Title" subtitle="Some explanation." variation="full">
_20
<h1>Hello, World!</h1>
_20
+ <p>{data?.helloworld}</p>
_20
</PageBlock>
_20
</Layout>
_20
)
_20
}
_20
_20
export default AdminExample

The result should be:

{"base64":"  ","img":{"width":898,"height":630,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":37073,"url":"https://user-images.githubusercontent.com/18701182/92937440-b79f7400-f421-11ea-9e92-a24ef710e83e.png"}}

With this you have a service connected to your admin application and can now connect to external services (learn more about our services course 🚀).

Answer sheet

See answer sheet for adminExample.tsx
adminExample.tsx

_20
import React, { FC } from 'react'
_20
import { Layout, PageBlock } from 'vtex.styleguide'
_20
import { useQuery } from 'react-apollo'
_20
_20
import helloworld from './graphql/helloworld.gql'
_20
_20
const AdminExample: FC = () => {
_20
const { data } = useQuery(helloworld)
_20
_20
return (
_20
<Layout>
_20
<PageBlock title="Titulo" subtitle="Alguma explicação." variation="full">
_20
<h1>Hello, World!</h1>
_20
<p>{data?.helloworld}</p>
_20
</PageBlock>
_20
</Layout>
_20
)
_20
}
_20
_20
export default AdminExample

See answer sheet for helloworld.gql
helloworld.gql

_10
query hello {
_10
helloworld
_10
}

See answer sheet for index.ts
index.ts

_11
import { Service } from '@vtex/api'
_11
_11
export default new Service({
_11
graphql: {
_11
resolvers: {
_11
Query: {
_11
helloworld: () => `Service number: ${Math.random()}`,
_11
},
_11
},
_11
},
_11
})

See answer sheet for manifest.json
manifest.json

_21
{
_21
"vendor": "appliancetheme",
_21
"name": "admin-sandbox",
_21
"version": "0.0.0",
_21
"title": "Admin Sandbox",
_21
"description": "Admin Sandbox",
_21
"builders": {
_21
"react": "3.x",
_21
"messages": "1.x",
_21
"docs": "0.x",
_21
"admin": "0.x",
_21
"node": "6.x",
_21
"graphql": "1.x"
_21
},
_21
"dependencies": {
_21
"vtex.styleguide": "9.x"
_21
},
_21
"registries": ["smartcheckout"],
_21
"policies": [],
_21
"$schema": "https://raw.githubusercontent.com/vtex/node-vtex-api/master/gen/manifest.schema"
_21
}

See answer sheet for package.json
package.json

_21
{
_21
"dependencies": {
_21
"co-body": "^6.0.0",
_21
"ramda": "^0.25.0"
_21
},
_21
"devDependencies": {
_21
"@types/co-body": "^0.0.3",
_21
"@types/jest": "^24.0.18",
_21
"@types/node": "^12.0.0",
_21
"@types/ramda": "types/npm-ramda#dist",
_21
"@vtex/api": "6.36.2",
_21
"@vtex/test-tools": "^1.0.0",
_21
"tslint": "^5.14.0",
_21
"tslint-config-vtex": "^2.1.0",
_21
"typescript": "3.8.3"
_21
},
_21
"scripts": {
_21
"lint": "tsc --noEmit --pretty && tslint -c tslint.json --fix './**/*.ts'"
_21
},
_21
"version": "0.0.7"
_21
}

See answer sheet for schema.graphql
schema.graphql

_10
type Query {
_10
helloworld: String
_10
}

On this page