Skip to content

Commit 68cdc5b

Browse files
authored
Minor examples improvements (#9148)
1 parent 7285b48 commit 68cdc5b

28 files changed

+39
-8
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-env', { targets: { node: process.versions.node.split('.')[0] } }],
4+
'@babel/preset-typescript',
5+
],
6+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
transform: { '^.+\\.ts': 'babel-jest' },
3+
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
4+
};

examples/typescript-graphql-request/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"private": true,
55
"devDependencies": {
66
"@graphql-codegen/cli": "3.2.2",
7-
"@graphql-codegen/gql-tag-operations-preset": "2.1.0"
7+
"@graphql-codegen/gql-tag-operations-preset": "2.1.0",
8+
"babel-jest": "28.1.3",
9+
"jest": "28.1.3"
810
},
911
"dependencies": {
1012
"graphql": "16.6.0",
@@ -15,7 +17,7 @@
1517
"codegen": "graphql-codegen --config codegen.ts",
1618
"build": "tsc",
1719
"dev": "ts-node src/main.ts",
18-
"test:end2end": "node dist/main.js"
20+
"test:end2end": "yarn jest"
1921
},
2022
"type": "commonjs",
2123
"bob": false
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getPeople } from './main';
2+
3+
describe('TypeScript GraphQL Request tests', () => {
4+
it('works without variables', async () => {
5+
const result = await getPeople();
6+
expect(result?.map(o => o?.node?.name)).toContain('Luke Skywalker');
7+
});
8+
9+
it('returns first 3 entries', async () => {
10+
const result = await getPeople(3);
11+
expect(result).toHaveLength(3);
12+
});
13+
});

examples/typescript-graphql-request/src/main.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-console */
22
import { GraphQLClient } from 'graphql-request';
33
import { graphql } from './gql';
4+
import { AllPeopleQueryQuery } from './gql/graphql';
45

56
const AllPeopleQueryDocument = graphql(/* GraphQL */ `
67
query AllPeopleQuery {
@@ -36,10 +37,15 @@ const apiUrl = 'https://swapi-graphql.netlify.app/.netlify/functions/index';
3637

3738
const client = new GraphQLClient(apiUrl);
3839

39-
client.request(AllPeopleQueryDocument).then(res => {
40-
console.log(res?.allPeople?.edges);
41-
});
40+
export const getPeople = async (first?: number) => {
41+
let res: AllPeopleQueryQuery;
42+
if (first) {
43+
res = await client.request(AllPeopleWithVariablesQueryDocument, { first });
44+
} else {
45+
res = await client.request(AllPeopleQueryDocument);
46+
}
47+
return res?.allPeople?.edges;
48+
};
4249

43-
client.request(AllPeopleWithVariablesQueryDocument, { first: 10 }).then(res => {
44-
console.log(res?.allPeople?.edges);
45-
});
50+
getPeople().then(res => console.log(res));
51+
getPeople(10).then(res => console.log(res));

0 commit comments

Comments
 (0)