Skip to content

Commit

Permalink
fetch all primitive fields with fields function
Browse files Browse the repository at this point in the history
  • Loading branch information
aexol committed Sep 17, 2024
1 parent 6885c28 commit 5ab016c
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ Strongly Typed GraphQL from the team at [GraphQL Editor](https://graphqleditor.c

# How it works

![](images/zeus.webp)
![](Zeus.gif)

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.

## Features
⚡️ Validates queries and selectors
⚡️ Types mapped from your schema <br/>
⚡️ Fetch all primitive fields with one function <br/>
⚡️ Works with Apollo Client, React Query, Stucco Subscriptions _(\*more coming soon...)_<br/>
⚡️ Works with Subscriptions <br/>
⚡️ Infer complex response types <br/>
Expand Down
Binary file added Zeus.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/typescript-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-node",
"version": "1.2.5",
"version": "2.0.0",
"description": "",
"private": true,
"main": "index.js",
Expand Down
8 changes: 8 additions & 0 deletions examples/typescript-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ValueTypes,
$,
FromSelector,
fields,
} from './zeus/index.js';
import { typedGql } from './zeus/typedDocumentNode.js';

Expand Down Expand Up @@ -106,6 +107,13 @@ const run = async () => {
);
printQueryResult('ZeusCard', ZeusCard);

const bbb = await Gql('query')({
drawCard: {
...fields('Card'),
},
});
printQueryResult('scalarsSelector', bbb.drawCard);

const blalba = await Gql('query')({
drawChangeCard: {
__typename: true,
Expand Down
21 changes: 21 additions & 0 deletions examples/typescript-node/src/zeus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,27 @@ export const Gql = Chain(HOST, {

export const ZeusScalars = ZeusSelect<ScalarCoders>();

type ScalarsSelector<T> = {
[X in Required<{
[P in keyof T]: T[P] extends number | string | undefined | boolean ? P : never;
}>[keyof T]]: true;
};

export const fields = <T extends keyof ModelTypes>(k: T) => {
const t = ReturnTypes[k];
const o = Object.fromEntries(
Object.entries(t)
.filter(([, value]) => {
const isReturnType = ReturnTypes[value as string];
if (!isReturnType || (typeof isReturnType === 'string' && isReturnType.startsWith('scalar.'))) {
return true;
}
})
.map(([key]) => [key, true as const]),
);
return o as ScalarsSelector<ModelTypes[T]>;
};

export const decodeScalarsInResponse = <O extends Operations>({
response,
scalars,
Expand Down
21 changes: 21 additions & 0 deletions packages/graphql-zeus-core/TreeToTS/functions/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ export const Gql = Chain(HOST, {
export const ZeusScalars = ZeusSelect<ScalarCoders>();
type ScalarsSelector<T> = {
[X in Required<{
[P in keyof T]: T[P] extends number | string | undefined | boolean ? P : never;
}>[keyof T]]: true;
};
export const fields = <T extends keyof ModelTypes>(k: T) => {
const t = ReturnTypes[k];
const o = Object.fromEntries(
Object.entries(t)
.filter(([, value]) => {
const isReturnType = ReturnTypes[value as string];
if (!isReturnType || (typeof isReturnType === 'string' && isReturnType.startsWith('scalar.'))) {
return true;
}
})
.map(([key]) => [key, true as const]),
);
return o as ScalarsSelector<ModelTypes[T]>;
};
export const decodeScalarsInResponse = <O extends Operations>({
response,
scalars,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
apiSubscription,
HOST,
ScalarCoders,
ModelTypes,
HEADERS,
} from '@/TreeToTS/functions/new/mocks';
import {
Expand Down Expand Up @@ -144,3 +145,24 @@ export const Gql = Chain(HOST, {
});

export const ZeusScalars = ZeusSelect<ScalarCoders>();

type ScalarsSelector<T> = {
[X in Required<{
[P in keyof T]: T[P] extends number | string | undefined | boolean ? P : never;
}>[keyof T]]: true;
};

export const fields = <T extends keyof ModelTypes>(k: T) => {
const t = ReturnTypes[k];
const o = Object.fromEntries(
Object.entries(t)
.filter(([, value]) => {
const isReturnType = ReturnTypes[value as string];
if (!isReturnType || (typeof isReturnType === 'string' && isReturnType.startsWith('scalar.'))) {
return true;
}
})
.map(([key]) => [key, true as const]),
);
return o as ScalarsSelector<ModelTypes[T]>;
};
3 changes: 2 additions & 1 deletion packages/graphql-zeus-core/TreeToTS/functions/new/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const AllTypesProps = {
JSON: 'scalar.JSON' as const,
};

export const ReturnTypes = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ReturnTypes: Record<string, any> = {
Query: {
cardByStatus: 'Card',
drawCard: 'Card',
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-zeus-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-zeus-core",
"version": "5.4.5",
"version": "6.0.0",
"private": false,
"main": "./lib/index.js",
"author": "GraphQL Editor, Artur Czemiel",
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-zeus-jsonschema/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-zeus-jsonschema",
"version": "5.4.5",
"version": "6.0.0",
"private": false,
"main": "./lib/index.js",
"author": "GraphQL Editor, Artur Czemiel",
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql-zeus/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-zeus",
"version": "5.4.5",
"version": "6.0.0",
"private": false,
"scripts": {
"start": "ttsc --watch",
Expand All @@ -26,8 +26,8 @@
"dependencies": {
"config-maker": "^0.0.6",
"cross-fetch": "^3.0.4",
"graphql-zeus-core": "^5.4.5",
"graphql-zeus-jsonschema": "^5.4.5",
"graphql-zeus-core": "^6.0.0",
"graphql-zeus-jsonschema": "^6.0.0",
"yargs": "^16.1.1"
}
}

0 comments on commit 5ab016c

Please sign in to comment.