Skip to content

Commit

Permalink
feat: support gql tada closes #206
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Mar 1, 2024
1 parent 0010e15 commit 0957b1b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-mayflies-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'villus': major
---

feat: add support for gql.tada
21 changes: 19 additions & 2 deletions packages/shared/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import type { TypedDocumentNode, DocumentTypeDecoration } from '@graphql-typed-document-node/core';
import { DocumentNode } from 'graphql';

export interface DocumentDecoration<
Result = {
[key: string]: any;
},
Variables = {
[key: string]: any;
},
> {
/** Type to support `@graphql-typed-document-node/core`
* @internal
*/
__apiType?: (variables: Variables) => Result;
/** Type to support `TypedQueryDocumentNode` from `graphql`
* @internal
*/
__ensureTypesOfVariablesAndResultMatching?: (variables: Variables) => Result;
}

export interface GraphQLResponse<TData> {
data: TData;
errors: any;
Expand All @@ -20,7 +37,7 @@ export interface ParsedResponse<TData> {
}

export interface Operation<TData, TVars> {
query: string | DocumentNode | TypedDocumentNode<TData, TVars> | DocumentTypeDecoration<TData, TVars>;
query: string | DocumentNode | DocumentDecoration<TData, TVars>;
variables?: TVars;
}

Expand Down
6 changes: 2 additions & 4 deletions packages/shared/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type { TypedDocumentNode, DocumentTypeDecoration } from '@graphql-typed-document-node/core';
import { DocumentNode, print } from 'graphql';
import { DocumentDecoration } from './types';

/**
* Normalizes a query string or object to a string.
*/
export function normalizeQuery(
query: string | DocumentNode | TypedDocumentNode | DocumentTypeDecoration<any, any>,
): string | null {
export function normalizeQuery(query: string | DocumentNode | DocumentDecoration<any, any>): string | null {
if (typeof query === 'string') {
return query;
}
Expand Down
16 changes: 11 additions & 5 deletions packages/villus/test/useQuery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import { server } from './mocks/server';
import flushPromises from 'flush-promises';
import waitForExpect from 'wait-for-expect';
import { mount } from './helpers/mount';
import { useClient, useQuery, cache as cachePlugin, fetch as fetchPlugin, CombinedError } from '../src/index';
import {
useClient,
useQuery,
cache as cachePlugin,
fetch as fetchPlugin,
CombinedError,
DocumentDecoration,
} from '../src/index';
import {
PostsQuery,
QueryWithGqlError,
Expand All @@ -17,15 +24,14 @@ import {
PostsQueryWithDescription,
} from './mocks/queries';
import { graphql } from 'msw';
import { DocumentTypeDecoration } from '@graphql-typed-document-node/core';

interface Post {
id: number;
title: string;
}

class TypedDocumentString<TResult, TVariables> extends String implements DocumentTypeDecoration<TResult, TVariables> {
__apiType?: DocumentTypeDecoration<TResult, TVariables>['__apiType'];
class TypedDocumentString<TResult, TVariables> extends String implements DocumentDecoration<TResult, TVariables> {
__apiType?: DocumentDecoration<TResult, TVariables>['__apiType'];

constructor(
private value: string,
Expand All @@ -34,7 +40,7 @@ class TypedDocumentString<TResult, TVariables> extends String implements Documen
super(value);
}

toString(): string & DocumentTypeDecoration<TResult, TVariables> {
toString(): string & DocumentDecoration<TResult, TVariables> {
return this.value;
}
}
Expand Down

0 comments on commit 0957b1b

Please sign in to comment.