Skip to content

Commit d7d26d8

Browse files
author
Manu
committed
refactor(service): updated public request method and update readme
1 parent 82e04a7 commit d7d26d8

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

README.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,32 @@ async function initScope(scope) {
3434
return mainRole === 'TYPE_CUSTOMER' ? 'customer' : scope;
3535
}
3636

37-
let scope;
38-
initScope('privateScope').then(response => {
39-
scope = response;
40-
});
41-
42-
gqlClient.getClient(scope).then(async (client: ApolloClient<any>) => {
43-
ReactDOM.render(
44-
<ApolloProvider {...{ client }}>
45-
<App />
46-
</ApolloProvider>,
47-
document.getElementById('root')
48-
);
37+
initScope('privateScope').then(scope => {
38+
gqlClient.getClient(scope).then((client: ApolloClient<any>) => {
39+
ReactDOM.render(
40+
<ApolloProvider {...{ client }}>
41+
<App />
42+
</ApolloProvider>,
43+
document.getElementById('root')
44+
);
45+
});
4946
});
5047
```
5148

49+
## Example of urls:
50+
51+
### Private scope:
52+
53+
`https://myurl.com/api/privateScope`
54+
55+
### Customer scope:
56+
57+
`https://myurl.com/api/customer`
58+
59+
### Public scope:
60+
61+
`https://myurl.com/api/public`
62+
5263
#### and that's it, try it.
5364

5465
## Use in the services.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"graphql",
1515
"react",
1616
"apollo",
17+
"client",
1718
"lightweight",
1819
"fast",
1920
"typescript"

src/graphqlService.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export type DataInitType = {
1515
publicUri: string;
1616
};
1717

18+
type ClientFunction = (client: ApolloClient<any>) => void;
19+
1820
export class GraphqlService {
1921
_client: any = undefined;
2022
_token: string | undefined = undefined;
@@ -110,27 +112,31 @@ export class GraphqlService {
110112
*
111113
* @param callback
112114
*/
113-
publicRequest(callback: any) {
115+
publicRequest(callback: ClientFunction) {
114116
return new Promise((resolve, reject) => {
115117
const client = this.createPublicClient();
116118
resolve(callback(client));
117119
});
118120
}
119121

120-
/**
121-
*
122-
* @param schema schema name
123-
* @param variables variables to filter, etc
124-
*/
125-
publicQuery(schema: string, variables: any) {
122+
private execPublic = (schema: string, variables: any, mutate = true) => {
126123
return this.publicRequest((client: ApolloClient<any>) => {
127-
return client.query({
128-
query: gql`
124+
return (client as any)[mutate ? 'mutate' : 'query']({
125+
[mutate ? 'mutation' : 'query']: gql`
129126
${schema}
130127
`,
131128
variables
132129
});
133130
});
131+
};
132+
133+
/**
134+
*
135+
* @param schema schema name
136+
* @param variables variables to filter, etc
137+
*/
138+
publicQuery(schema: string, variables: any) {
139+
return this.execPublic(schema, variables, false);
134140
}
135141

136142
/**
@@ -139,14 +145,7 @@ export class GraphqlService {
139145
* @param variables variables to mutate
140146
*/
141147
publicMutate(schema: string, variables: any) {
142-
return this.publicRequest((client: ApolloClient<any>) => {
143-
return client.mutate({
144-
mutation: gql`
145-
${schema}
146-
`,
147-
variables
148-
});
149-
});
148+
return this.execPublic(schema, variables);
150149
}
151150

152151
/**
@@ -203,15 +202,15 @@ export class GraphqlService {
203202
* @returns a client graphql
204203
*/
205204
async getClient(scope = 'websiteBackend') {
206-
return this.client ? this.client : this.createPrivateClient(scope);
205+
return this.client ?? this.createPrivateClient(scope);
207206
}
208207

209208
/**
210209
*
211210
* @param scope should be public, webApp, etc
212211
* @param callback a client graphql
213212
*/
214-
async request(scope: string, callback: any) {
213+
async request(scope: string, callback: ClientFunction) {
215214
const client = await this.getClient(scope);
216215
return callback(client);
217216
}

0 commit comments

Comments
 (0)