Skip to content

Commit 5ab016c

Browse files
committed
fetch all primitive fields with fields function
1 parent 6885c28 commit 5ab016c

File tree

11 files changed

+82
-8
lines changed

11 files changed

+82
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ Strongly Typed GraphQL from the team at [GraphQL Editor](https://graphqleditor.c
66

77
# How it works
88

9-
![](images/zeus.webp)
9+
![](Zeus.gif)
1010

1111
GraphQL Zeus is the absolute best way to interact with your GraphQL endpoints in a type-safe way. Zeus uses your schema to generate Typescript types and strongly typed clients to unlock the power, efficiency, productivity and safety of Typescript on your GraphQL requests.
1212

1313
## Features
1414
⚡️ Validates queries and selectors
1515
⚡️ Types mapped from your schema <br/>
16+
⚡️ Fetch all primitive fields with one function <br/>
1617
⚡️ Works with Apollo Client, React Query, Stucco Subscriptions _(\*more coming soon...)_<br/>
1718
⚡️ Works with Subscriptions <br/>
1819
⚡️ Infer complex response types <br/>

Zeus.gif

9.85 MB
Loading

examples/typescript-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-node",
3-
"version": "1.2.5",
3+
"version": "2.0.0",
44
"description": "",
55
"private": true,
66
"main": "index.js",

examples/typescript-node/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ValueTypes,
1313
$,
1414
FromSelector,
15+
fields,
1516
} from './zeus/index.js';
1617
import { typedGql } from './zeus/typedDocumentNode.js';
1718

@@ -106,6 +107,13 @@ const run = async () => {
106107
);
107108
printQueryResult('ZeusCard', ZeusCard);
108109

110+
const bbb = await Gql('query')({
111+
drawCard: {
112+
...fields('Card'),
113+
},
114+
});
115+
printQueryResult('scalarsSelector', bbb.drawCard);
116+
109117
const blalba = await Gql('query')({
110118
drawChangeCard: {
111119
__typename: true,

examples/typescript-node/src/zeus/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,27 @@ export const Gql = Chain(HOST, {
279279

280280
export const ZeusScalars = ZeusSelect<ScalarCoders>();
281281

282+
type ScalarsSelector<T> = {
283+
[X in Required<{
284+
[P in keyof T]: T[P] extends number | string | undefined | boolean ? P : never;
285+
}>[keyof T]]: true;
286+
};
287+
288+
export const fields = <T extends keyof ModelTypes>(k: T) => {
289+
const t = ReturnTypes[k];
290+
const o = Object.fromEntries(
291+
Object.entries(t)
292+
.filter(([, value]) => {
293+
const isReturnType = ReturnTypes[value as string];
294+
if (!isReturnType || (typeof isReturnType === 'string' && isReturnType.startsWith('scalar.'))) {
295+
return true;
296+
}
297+
})
298+
.map(([key]) => [key, true as const]),
299+
);
300+
return o as ScalarsSelector<ModelTypes[T]>;
301+
};
302+
282303
export const decodeScalarsInResponse = <O extends Operations>({
283304
response,
284305
scalars,

packages/graphql-zeus-core/TreeToTS/functions/generated.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,27 @@ export const Gql = Chain(HOST, {
238238
239239
export const ZeusScalars = ZeusSelect<ScalarCoders>();
240240
241+
type ScalarsSelector<T> = {
242+
[X in Required<{
243+
[P in keyof T]: T[P] extends number | string | undefined | boolean ? P : never;
244+
}>[keyof T]]: true;
245+
};
246+
247+
export const fields = <T extends keyof ModelTypes>(k: T) => {
248+
const t = ReturnTypes[k];
249+
const o = Object.fromEntries(
250+
Object.entries(t)
251+
.filter(([, value]) => {
252+
const isReturnType = ReturnTypes[value as string];
253+
if (!isReturnType || (typeof isReturnType === 'string' && isReturnType.startsWith('scalar.'))) {
254+
return true;
255+
}
256+
})
257+
.map(([key]) => [key, true as const]),
258+
);
259+
return o as ScalarsSelector<ModelTypes[T]>;
260+
};
261+
241262
export const decodeScalarsInResponse = <O extends Operations>({
242263
response,
243264
scalars,

packages/graphql-zeus-core/TreeToTS/functions/new/clientFunctions.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
apiSubscription,
1111
HOST,
1212
ScalarCoders,
13+
ModelTypes,
1314
HEADERS,
1415
} from '@/TreeToTS/functions/new/mocks';
1516
import {
@@ -144,3 +145,24 @@ export const Gql = Chain(HOST, {
144145
});
145146

146147
export const ZeusScalars = ZeusSelect<ScalarCoders>();
148+
149+
type ScalarsSelector<T> = {
150+
[X in Required<{
151+
[P in keyof T]: T[P] extends number | string | undefined | boolean ? P : never;
152+
}>[keyof T]]: true;
153+
};
154+
155+
export const fields = <T extends keyof ModelTypes>(k: T) => {
156+
const t = ReturnTypes[k];
157+
const o = Object.fromEntries(
158+
Object.entries(t)
159+
.filter(([, value]) => {
160+
const isReturnType = ReturnTypes[value as string];
161+
if (!isReturnType || (typeof isReturnType === 'string' && isReturnType.startsWith('scalar.'))) {
162+
return true;
163+
}
164+
})
165+
.map(([key]) => [key, true as const]),
166+
);
167+
return o as ScalarsSelector<ModelTypes[T]>;
168+
};

packages/graphql-zeus-core/TreeToTS/functions/new/mocks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export const AllTypesProps = {
3030
JSON: 'scalar.JSON' as const,
3131
};
3232

33-
export const ReturnTypes = {
33+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
34+
export const ReturnTypes: Record<string, any> = {
3435
Query: {
3536
cardByStatus: 'Card',
3637
drawCard: 'Card',

packages/graphql-zeus-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-zeus-core",
3-
"version": "5.4.5",
3+
"version": "6.0.0",
44
"private": false,
55
"main": "./lib/index.js",
66
"author": "GraphQL Editor, Artur Czemiel",

packages/graphql-zeus-jsonschema/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-zeus-jsonschema",
3-
"version": "5.4.5",
3+
"version": "6.0.0",
44
"private": false,
55
"main": "./lib/index.js",
66
"author": "GraphQL Editor, Artur Czemiel",

0 commit comments

Comments
 (0)