Comparing version
@@ -936,2 +936,13 @@ import { Kind, OperationTypeNode, DocumentNode } from '@0no-co/graphql.web'; | ||
: never; | ||
type omitFragmentRefsRec<Data> = Data extends readonly (infer Value)[] | ||
? readonly omitFragmentRefsRec<Value>[] | ||
: Data extends null | ||
? null | ||
: Data extends undefined | ||
? undefined | ||
: Data extends {} | ||
? { | ||
[Key in Exclude<keyof Data, $tada.fragmentRefs>]: omitFragmentRefsRec<Data[Key]>; | ||
} | ||
: Data; | ||
type makeUndefinedFragmentRef<FragmentName extends string> = { | ||
@@ -1598,2 +1609,14 @@ [$tada.fragmentRefs]: { | ||
: Data; | ||
type fragmentRefsOfFragmentsRec<Fragments extends readonly any[]> = Fragments extends readonly [ | ||
infer Fragment, | ||
...infer Rest, | ||
] | ||
? obj<makeFragmentRef<Fragment> & fragmentRefsOfFragmentsRec<Rest>> | ||
: {}; | ||
type resultOfFragmentsRec<Fragments extends readonly any[]> = Fragments extends readonly [ | ||
infer Fragment, | ||
...infer Rest, | ||
] | ||
? ResultOf<Fragment> & resultOfFragmentsRec<Rest> | ||
: {}; | ||
type fragmentOfTypeRec<Document extends makeDefinitionDecoration> = | ||
@@ -1604,2 +1627,3 @@ | readonly fragmentOfTypeRec<Document>[] | ||
| null; | ||
type resultOfTypeRec<Data> = readonly resultOfTypeRec<Data>[] | Data | undefined | null; | ||
/** Unmasks a fragment mask for a given fragment document and data. | ||
@@ -1662,2 +1686,84 @@ * | ||
: mirrorFragmentTypeRec<Fragment, ResultOf<Document>>; | ||
/** For testing, masks fragment data for given data and fragments. | ||
* | ||
* @param _fragments - A list of GraphQL documents of fragments, created using {@link graphql}. | ||
* @param data - The combined result data of the fragments, which can be wrapped in arrays. | ||
* @returns The masked data of the fragments. | ||
* | ||
* @remarks | ||
* When creating test data, you may define data for fragments that’s unmasked, making it | ||
* unusable in parent fragments or queries that require masked data. | ||
* | ||
* This means that you may have to use {@link maskFragments} to mask your data first | ||
* for TypeScript to not report an error. | ||
* | ||
* @example | ||
* ``` | ||
* import { FragmentOf, ResultOf, graphql, maskFragments } from 'gql.tada'; | ||
* | ||
* const bookFragment = graphql(` | ||
* fragment BookComponent on Book { | ||
* id | ||
* title | ||
* } | ||
* `); | ||
* | ||
* const data = maskFragments([bookFragment], { id: 'id', title: 'book' }); | ||
* ``` | ||
* | ||
* @see {@link readFragment} for how to read from fragment masks (i.e. the reverse) | ||
*/ | ||
declare function maskFragments< | ||
const Fragments extends readonly [...makeDefinitionDecoration[]], | ||
const Data extends resultOfTypeRec<resultOfFragmentsRec<Fragments>>, | ||
>( | ||
_fragments: Fragments, | ||
data: Data | ||
): resultOfTypeRec<resultOfFragmentsRec<Fragments>> extends Data | ||
? never | ||
: mirrorFragmentTypeRec<Data, fragmentRefsOfFragmentsRec<Fragments>>; | ||
/** For testing, converts document data without fragment refs to their result type. | ||
* | ||
* @param _document - A GraphQL document, created using {@link graphql}. | ||
* @param data - The result data of the GraphQL document with optional fragment refs. | ||
* @returns The masked result data of the document. | ||
* | ||
* @remarks | ||
* When creating test data, you may define data for documents that’s unmasked, but | ||
* need to cast the data to match the result type of your document. | ||
* | ||
* This means that you may have to use {@link unsafe_readResult} to cast | ||
* them to the result type, instead of doing `as any as ResultOf<typeof document>`. | ||
* | ||
* This function is inherently unsafe, since it doesn't check that your document | ||
* actually contains the masked fragment data! | ||
* | ||
* @example | ||
* ``` | ||
* import { FragmentOf, ResultOf, graphql, unsafe_readResult } from 'gql.tada'; | ||
* | ||
* const bookFragment = graphql(` | ||
* fragment BookComponent on Book { | ||
* id | ||
* title | ||
* } | ||
* `); | ||
* | ||
* const query = graphql(` | ||
* query { | ||
* book { | ||
* ...BookComponent | ||
* } | ||
* } | ||
* `, [bookFragment]); | ||
* | ||
* const data = unsafe_readResult(query, { book: { id: 'id', title: 'book' } }); | ||
* ``` | ||
* | ||
* @see {@link readFragment} for how to read from fragment masks (i.e. the reverse) | ||
*/ | ||
declare function unsafe_readResult< | ||
const Document extends DocumentDecoration<any, any>, | ||
const Data extends omitFragmentRefsRec<ResultOf<Document>>, | ||
>(_document: Document, data: Data): ResultOf<Document>; | ||
declare const graphql: GraphQLTadaAPI<schemaOfConfig<setupSchema>>; | ||
@@ -1674,2 +1780,3 @@ | ||
initGraphQLTada, | ||
maskFragments, | ||
parse, | ||
@@ -1679,2 +1786,3 @@ type parseDocument, | ||
type setupSchema, | ||
unsafe_readResult, | ||
}; |
@@ -8,10 +8,10 @@ Object.defineProperty(exports, "__esModule", { | ||
function initGraphQLTada() { | ||
return function graphql(e, i) { | ||
return function graphql(e, n) { | ||
var a = r.parse(e).definitions; | ||
var n = new Set; | ||
for (var t of i || []) { | ||
for (var d of t.definitions) { | ||
if (d.kind === r.Kind.FRAGMENT_DEFINITION && !n.has(d)) { | ||
a.push(d); | ||
n.add(d); | ||
var i = new Set; | ||
for (var t of n || []) { | ||
for (var s of t.definitions) { | ||
if (s.kind === r.Kind.FRAGMENT_DEFINITION && !i.has(s)) { | ||
a.push(s); | ||
i.add(s); | ||
} | ||
@@ -36,2 +36,6 @@ } | ||
exports.maskFragments = function maskFragments(r, e) { | ||
return e; | ||
}; | ||
exports.parse = function parse(e) { | ||
@@ -44,2 +48,6 @@ return r.parse(e); | ||
}; | ||
exports.unsafe_readResult = function unsafe_readResult(r, e) { | ||
return e; | ||
}; | ||
//# sourceMappingURL=gql-tada.js.map |
{ | ||
"name": "gql.tada", | ||
"description": "The spec-compliant & magical GraphQL query language engine in the TypeScript type system", | ||
"version": "1.1.1-canary-caa94dc0f10b22d8bf6d418eb23e14f1a586b416", | ||
"version": "1.2.0-canary-5ec863cff8c6951ca027e62b08949098f862085e", | ||
"author": "0no.co <hi@0no.co>", | ||
@@ -6,0 +6,0 @@ "source": "./src/index.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
98356
13.04%1852
6.93%