Skip to content

Commit 246a772

Browse files
committed
GraphQL beta ph2
1 parent a105926 commit 246a772

File tree

5 files changed

+2776
-49
lines changed

5 files changed

+2776
-49
lines changed

README.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,16 @@ Once you have downloaded the project, add your Contentstack API Key, Delivery To
4949

5050
## Step 6: Install Apollo framework
5151
Begin by including packages that are essential for building the Apollo app. Install the following modules using the npm install command. Refer the [Installation](https://www.apollographql.com/docs/ios/installation.html#installing-framework) doc for more information.
52-
- [Apollo-client](https://www.npmjs.com/package/apollo-client)
53-
- [Apollo-cache-inmemory](https://www.npmjs.com/package/apollo-cache-inmemory)
54-
- [Apollo-link-http](https://www.npmjs.com/package/apollo-link-http)
55-
- [Graphql-tag](https://www.npmjs.com/package/graphql-tag)
52+
- [Apollo-client](https://www.npmjs.com/package/@apollo/client)
5653

5754
## Step 7: Add modules in server file to invoke Apollo
5855
In order to invoke Apollo as part of the JavaScript and for routing and templating purpose, add the express and express-nunjucks modules to the server file, app.js.
5956

60-
Add the ‘apollo-client’ and ‘apollo-cache-inmemory’ parameters in ‘apollo-client’ instance.
61-
6257
```
6358
const express = require('express');
64-
const expressNunjucks = require('express-nunjucks')
65-
const app = express();
66-
var { ApolloClient } = require('apollo-client');
67-
var { InMemoryCache } = require('apollo-cache-inmemory');
68-
var { HttpLink } = require('apollo-link-http');
69-
var gql = require("graphql-tag");
59+
const expressNunjucks = require('express-nunjucks');
60+
const app = express()
61+
var { ApolloClient, InMemoryCache, HttpLink,from, gql } = require('@apollo/client');
7062
var fetch = require('node-fetch');
7163
```
7264

@@ -76,13 +68,16 @@ Create a single shared instance of 'Apollo-link' and point it at your GraphQL se
7668
```
7769
const cache = new InMemoryCache();
7870
const link = new HttpLink({
79-
uri:'https://graphql.contentstack.com/stacks/blt44d915c18f115370?access_token=cs551d666a332e455a34174bd0&environment=production',
80-
fetch
81-
})
71+
uri: 'https://graphql.contentstack.com/stacks/<API_KEY>?environment=<ENVIRONMENT_NAME>',
72+
headers: {
73+
access_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
74+
},
75+
fetch
76+
});
8277
const client = new ApolloClient({
78+
link: from([link]),
8379
cache,
84-
link
85-
})
80+
});
8681
```
8782

8883

@@ -102,10 +97,22 @@ To fetch all entries of ‘Product’ content type, add the following code snipp
10297
```
10398
client
10499
.query({
105-
query: gql`query { all_product{
106-
title
107-
description
108-
} }`,
100+
query: gql`query {
101+
all_product(locale: "en-us") {
102+
items {
103+
title
104+
description
105+
price
106+
featured_imageConnection(limit: 10) {
107+
edges {
108+
node {
109+
url
110+
}
111+
}
112+
}
113+
}
114+
}
115+
}`,
109116
.then(result =>
110117
res.render('./index', result)
111118
//console.log(result.data.all_product.items)

app.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
const express = require('express');
22
const expressNunjucks = require('express-nunjucks');
33
const app = express()
4-
var { ApolloClient } = require('apollo-client');
5-
var { InMemoryCache } = require('apollo-cache-inmemory');
6-
var { HttpLink } = require('apollo-link-http');
7-
var gql = require("graphql-tag");
4+
var { ApolloClient, InMemoryCache, HttpLink,from, gql } = require('@apollo/client');
85
var fetch = require('node-fetch');
96
const port = process.env.PORT || 8000
7+
const GRAPHQL_ENDPOINT =
8+
'https://graphql.contentstack.com/stacks/<API_KEY>?environment=<ENVIRONMENT_NAME>';
109

1110
const cache = new InMemoryCache();
1211
const link = new HttpLink({
13-
uri: "https://graphql.contentstack.com/stacks/blt292960b854e5170e?access_token=csf77a123fda5cc627a0363a49&environment=development",
14-
fetch
15-
})
12+
uri: GRAPHQL_ENDPOINT,
13+
fetch: fetch,
14+
headers: {
15+
access_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
16+
},
17+
});
1618
const client = new ApolloClient({
19+
link: from([link]),
1720
cache,
18-
link
19-
})
21+
});
2022
app.use(express.static('views'))
2123
const njk = expressNunjucks(app, {
2224
watch: true,
@@ -30,20 +32,24 @@ app.get('/', (req, res) =>
3032
// ... above is the instantiation of the client object.
3133
client
3234
.query({
33-
query: gql`query { all_product{
34-
items{
35-
title
36-
description
37-
price
38-
featured_image {
39-
title
40-
url
41-
}
42-
}
43-
} }`
35+
query: gql`query {
36+
all_product(locale: "en-us") {
37+
items {
38+
title
39+
description
40+
price
41+
featured_imageConnection(limit: 10) {
42+
edges {
43+
node {
44+
url
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}`
4451
})
4552
.then(result =>{
46-
// console.log("endpoint>>>>>>>>>", JSON.stringify(result, null, 0))
4753
res.render('./home', result)
4854
})
4955
);

0 commit comments

Comments
 (0)