Skip to content

Commit ebab987

Browse files
author
Manu
committed
first commit: added service
0 parents  commit ebab987

16 files changed

+8700
-0
lines changed

.github/workflows/main.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
on: [push]
3+
jobs:
4+
build:
5+
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}
6+
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
node: ['12.x', '14.x']
11+
os: [ubuntu-latest, windows-latest, macOS-latest]
12+
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@v2
16+
17+
- name: Use Node ${{ matrix.node }}
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node }}
21+
22+
- name: Install deps and build (with cache)
23+
uses: bahmutov/npm-install@v1
24+
25+
- name: Lint
26+
run: yarn lint
27+
28+
- name: Test
29+
run: yarn test --ci --coverage --maxWorkers=2
30+
31+
- name: Build
32+
run: yarn build

.github/workflows/size.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: size
2+
on: [pull_request]
3+
jobs:
4+
size:
5+
runs-on: ubuntu-latest
6+
env:
7+
CI_JOB_NUMBER: 1
8+
steps:
9+
- uses: actions/checkout@v1
10+
- uses: andresz1/size-limit-action@v1
11+
with:
12+
github_token: ${{ secrets.GITHUB_TOKEN }}

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.log
2+
.DS_Store
3+
node_modules
4+
.cache
5+
dist
6+
7+
# ides
8+
.idea
9+
.vscode

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 manu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# graphql-react-client ![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
2+
3+
Apollo Client service for React applications
4+
5+
## Installation
6+
7+
`npm i -s graphql-react-client`
8+
or
9+
`yarn add graphql-react-client`
10+
11+
## Usage
12+
13+
```typescript
14+
import React from 'react';
15+
import ReactDOM from 'react-dom';
16+
import './index.less';
17+
import App from './App';
18+
import { ApolloClient, ApolloProvider } from '@apollo/client';
19+
import { gqlClient } from 'graphql-react-client';
20+
import authService from './shared/services/authService';
21+
22+
gqlClient.init({
23+
api: `${process.env.REACT_APP_API}`,
24+
uri: '/api/',
25+
publicUri: '/api/public'
26+
});
27+
28+
async function initScope(scope) {
29+
const sessionData: any = await authService.getSessionData();
30+
authService.login(sessionData);
31+
return sessionData?.mainRole === 'TYPE_CUSTOMER' ? 'customer' : scope;
32+
}
33+
let scope;
34+
initScope('privateScope').then(response => {
35+
scope = response;
36+
});
37+
38+
gqlClient.getClient(scope).then(async (client: ApolloClient<any>) => {
39+
ReactDOM.render(
40+
<ApolloProvider {...{ client }}>
41+
<App />
42+
</ApolloProvider>,
43+
document.getElementById('root')
44+
);
45+
});
46+
47+
```
48+
#### and that's it, try it.
49+
50+
## Use in the services.
51+
```typescript
52+
import { gqlClient } from 'graphql-react-client';
53+
54+
const GET = `
55+
query ($input: CoreGetInput!){
56+
CustomerGet(input: $input){
57+
id
58+
name
59+
}
60+
}
61+
`;
62+
63+
async get(input: any): Promise<CustomerType> {
64+
const response = await gqlClient.query('myScope', GET, { input });
65+
return response.data.CustomerGet;
66+
}
67+
```

example/.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.cache
3+
dist

example/index.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<title>Playground</title>
8+
</head>
9+
10+
<body>
11+
<div id="root"></div>
12+
<script src="./index.tsx"></script>
13+
</body>
14+
</html>

example/index.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'react-app-polyfill/ie11';
2+
import * as React from 'react';
3+
import * as ReactDOM from 'react-dom';
4+
import { Thing } from '../.';
5+
6+
const App = () => {
7+
return (
8+
<div>
9+
<Thing />
10+
</div>
11+
);
12+
};
13+
14+
ReactDOM.render(<App />, document.getElementById('root'));

example/package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"start": "parcel index.html",
8+
"build": "parcel build index.html"
9+
},
10+
"dependencies": {
11+
"react-app-polyfill": "^1.0.0"
12+
},
13+
"alias": {
14+
"react": "../node_modules/react",
15+
"react-dom": "../node_modules/react-dom/profiling",
16+
"scheduler/tracing": "../node_modules/scheduler/tracing-profiling"
17+
},
18+
"devDependencies": {
19+
"@types/react": "^16.9.11",
20+
"@types/react-dom": "^16.8.4",
21+
"parcel": "^1.12.3",
22+
"typescript": "^3.4.5"
23+
}
24+
}

example/tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"allowSyntheticDefaultImports": false,
4+
"target": "es5",
5+
"module": "commonjs",
6+
"jsx": "react",
7+
"moduleResolution": "node",
8+
"noImplicitAny": false,
9+
"noUnusedLocals": false,
10+
"noUnusedParameters": false,
11+
"removeComments": true,
12+
"strictNullChecks": true,
13+
"preserveConstEnums": true,
14+
"sourceMap": true,
15+
"lib": ["es2015", "es2016", "dom"],
16+
"types": ["node"]
17+
}
18+
}

package.json

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"version": "0.1.0",
3+
"license": "MIT",
4+
"main": "dist/index.js",
5+
"typings": "dist/index.d.ts",
6+
"files": [
7+
"dist",
8+
"src"
9+
],
10+
"engines": {
11+
"node": ">=10"
12+
},
13+
"keywords": [
14+
"graphql",
15+
"react",
16+
"apollo",
17+
"lightweight",
18+
"fast",
19+
"typescript"
20+
],
21+
"repository": {
22+
"type": "git",
23+
"url": "https://github.com/manu2manu/graphql-react-client.git"
24+
},
25+
"homepage": "https://github.com/manu2manu/graphql-react-client",
26+
"scripts": {
27+
"start": "tsdx watch",
28+
"build": "tsdx build",
29+
"test": "tsdx test --passWithNoTests",
30+
"lint": "tsdx lint",
31+
"prepare": "tsdx build",
32+
"size": "size-limit",
33+
"analyze": "size-limit --why"
34+
},
35+
"peerDependencies": {
36+
"react": ">=16"
37+
},
38+
"husky": {
39+
"hooks": {
40+
"pre-commit": "tsdx lint"
41+
}
42+
},
43+
"prettier": {
44+
"semi": true,
45+
"bracketSpacing": true,
46+
"trailingComma": "none",
47+
"singleQuote": true,
48+
"printWidth": 80,
49+
"tabWidth": 2,
50+
"arrowParens": "avoid",
51+
"endOfLine": "lf"
52+
},
53+
"name": "graphql-react-client",
54+
"author": "manu",
55+
"module": "dist/graphql-react-client.esm.js",
56+
"size-limit": [
57+
{
58+
"path": "dist/graphql-react-client.cjs.production.min.js",
59+
"limit": "10 KB"
60+
},
61+
{
62+
"path": "dist/graphql-react-client.esm.js",
63+
"limit": "10 KB"
64+
}
65+
],
66+
"devDependencies": {
67+
"@size-limit/preset-small-lib": "^6.0.3",
68+
"@types/apollo-upload-client": "^14.1.0",
69+
"@types/react": "^17.0.31",
70+
"@types/react-dom": "^17.0.10",
71+
"husky": "^7.0.4",
72+
"react": "^17.0.2",
73+
"react-dom": "^17.0.2",
74+
"size-limit": "^6.0.3",
75+
"tsdx": "^0.14.1",
76+
"tslib": "^2.3.1",
77+
"typescript": "^4.4.4"
78+
},
79+
"dependencies": {
80+
"@apollo/client": "^3.4.16",
81+
"apollo-upload-client": "^16.0.0",
82+
"graphql": "^15.6.1"
83+
}
84+
}

0 commit comments

Comments
 (0)