Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gql-tag-operations): support module augmentation + babel plugin #6492

Merged
merged 16 commits into from
Oct 7, 2021
Merged
6 changes: 6 additions & 0 deletions .changeset/lucky-rabbits-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/gql-tag-operations': minor
'@graphql-codegen/gql-tag-operations-preset': minor
---

feat: support module augumentation for extending the types of gql functions from existing packages via the `augmentedModuleName` config option.
6 changes: 6 additions & 0 deletions dev-test/codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,9 @@ generates:
schema: ./dev-test/gql-tag-operations/schema.graphql
documents: './dev-test/gql-tag-operations/src/**/*.ts'
preset: gql-tag-operations-preset
./dev-test/gql-tag-operations-urql/gql:
schema: ./dev-test/gql-tag-operations-urql/schema.graphql
documents: './dev-test/gql-tag-operations-urql/src/**/*.ts'
preset: gql-tag-operations-preset
presetConfig:
augmentedModuleName: '@urql/core'
189 changes: 189 additions & 0 deletions dev-test/gql-tag-operations-urql/gql/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/* eslint-disable */
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
export type Maybe<T> = T | null;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
Date: any;
Url: any;
};

export type Meta = {
__typename?: 'Meta';
count?: Maybe<Scalars['Int']>;
};

export type Mutation = {
__typename?: 'Mutation';
createTweet?: Maybe<Tweet>;
deleteTweet?: Maybe<Tweet>;
markTweetRead?: Maybe<Scalars['Boolean']>;
};

export type MutationCreateTweetArgs = {
body?: Maybe<Scalars['String']>;
};

export type MutationDeleteTweetArgs = {
id: Scalars['ID'];
};

export type MutationMarkTweetReadArgs = {
id: Scalars['ID'];
};

export type Notification = {
__typename?: 'Notification';
date?: Maybe<Scalars['Date']>;
id?: Maybe<Scalars['ID']>;
type?: Maybe<Scalars['String']>;
};

export type Query = {
__typename?: 'Query';
Notifications?: Maybe<Array<Maybe<Notification>>>;
NotificationsMeta?: Maybe<Meta>;
Tweet?: Maybe<Tweet>;
Tweets?: Maybe<Array<Maybe<Tweet>>>;
TweetsMeta?: Maybe<Meta>;
User?: Maybe<User>;
};

export type QueryNotificationsArgs = {
limit?: Maybe<Scalars['Int']>;
};

export type QueryTweetArgs = {
id: Scalars['ID'];
};

export type QueryTweetsArgs = {
limit?: Maybe<Scalars['Int']>;
skip?: Maybe<Scalars['Int']>;
sort_field?: Maybe<Scalars['String']>;
sort_order?: Maybe<Scalars['String']>;
};

export type QueryUserArgs = {
id: Scalars['ID'];
};

export type Stat = {
__typename?: 'Stat';
likes?: Maybe<Scalars['Int']>;
responses?: Maybe<Scalars['Int']>;
retweets?: Maybe<Scalars['Int']>;
views?: Maybe<Scalars['Int']>;
};

export type Tweet = {
__typename?: 'Tweet';
Author?: Maybe<User>;
Stats?: Maybe<Stat>;
body?: Maybe<Scalars['String']>;
date?: Maybe<Scalars['Date']>;
id: Scalars['ID'];
};

export type User = {
__typename?: 'User';
avatar_url?: Maybe<Scalars['Url']>;
first_name?: Maybe<Scalars['String']>;
full_name?: Maybe<Scalars['String']>;
id: Scalars['ID'];
last_name?: Maybe<Scalars['String']>;
/** @deprecated Field no longer supported */
name?: Maybe<Scalars['String']>;
username?: Maybe<Scalars['String']>;
};

export type FooQueryVariables = Exact<{ [key: string]: never }>;

export type FooQuery = {
__typename?: 'Query';
Tweets?: Array<{ __typename?: 'Tweet'; id: string } | null | undefined> | null | undefined;
};

export type LelFragment = { __typename?: 'Tweet'; id: string; body?: string | null | undefined };

export type BarQueryVariables = Exact<{ [key: string]: never }>;

export type BarQuery = {
__typename?: 'Query';
Tweets?:
| Array<{ __typename?: 'Tweet'; id: string; body?: string | null | undefined } | null | undefined>
| null
| undefined;
};

export const LelFragmentDoc = {
kind: 'Document',
definitions: [
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'Lel' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Tweet' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
{ kind: 'Field', name: { kind: 'Name', value: 'body' } },
],
},
},
],
} as unknown as DocumentNode<LelFragment, unknown>;
export const FooDocument = {
kind: 'Document',
definitions: [
{
kind: 'OperationDefinition',
operation: 'query',
name: { kind: 'Name', value: 'Foo' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: 'Tweets' },
selectionSet: {
kind: 'SelectionSet',
selections: [{ kind: 'Field', name: { kind: 'Name', value: 'id' } }],
},
},
],
},
},
],
} as unknown as DocumentNode<FooQuery, FooQueryVariables>;
export const BarDocument = {
kind: 'Document',
definitions: [
{
kind: 'OperationDefinition',
operation: 'query',
name: { kind: 'Name', value: 'Bar' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
name: { kind: 'Name', value: 'Tweets' },
selectionSet: {
kind: 'SelectionSet',
selections: [{ kind: 'FragmentSpread', name: { kind: 'Name', value: 'Lel' } }],
},
},
],
},
},
...LelFragmentDoc.definitions,
],
} as unknown as DocumentNode<BarQuery, BarQueryVariables>;
22 changes: 22 additions & 0 deletions dev-test/gql-tag-operations-urql/gql/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable */
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';

declare module '@urql/core' {
export function gql(
source: '\n query Foo {\n Tweets {\n id\n }\n }\n'
): typeof import('./graphql').FooDocument;
export function gql(
source: '\n fragment Lel on Tweet {\n id\n body\n }\n'
): typeof import('./graphql').LelFragmentDoc;
export function gql(
source: '\n query Bar {\n Tweets {\n ...Lel\n }\n }\n'
): typeof import('./graphql').BarDocument;
export function gql(source: string): unknown;

export type DocumentType<TDocumentNode extends DocumentNode<any, any>> = TDocumentNode extends DocumentNode<
infer TType,
any
>
? TType
: never;
}
52 changes: 52 additions & 0 deletions dev-test/gql-tag-operations-urql/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
scalar Url
scalar Date

type Tweet {
id: ID!
body: String
date: Date
Author: User
Stats: Stat
}

type User {
id: ID!
username: String
first_name: String
last_name: String
full_name: String
name: String @deprecated
avatar_url: Url
}

type Stat {
views: Int
likes: Int
retweets: Int
responses: Int
}

type Notification {
id: ID
date: Date
type: String
}

type Meta {
count: Int
}

type Query {
Tweet(id: ID!): Tweet
Tweets(limit: Int, skip: Int, sort_field: String, sort_order: String): [Tweet]
TweetsMeta: Meta
User(id: ID!): User
Notifications(limit: Int): [Notification]
NotificationsMeta: Meta
}

type Mutation {
createTweet(body: String): Tweet
deleteTweet(id: ID!): Tweet
markTweetRead(id: ID!): Boolean
}
25 changes: 25 additions & 0 deletions dev-test/gql-tag-operations-urql/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { gql } from 'urql';

const FooQuery = gql(/* GraphQL */ `
query Foo {
Tweets {
id
}
}
`);

const LelFragment = gql(/* GraphQL */ `
fragment Lel on Tweet {
id
body
}
`);

const BarQuery = gql(/* GraphQL */ `
query Bar {
Tweets {
...Lel
}
}
`);
10 changes: 5 additions & 5 deletions dev-test/test-schema/resolvers-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export type DirectiveResolverFn<TResult = {}, TParent = {}, TContext = {}, TArgs
export type ResolversTypes = {
Boolean: ResolverTypeWrapper<Scalars['Boolean']>;
Int: ResolverTypeWrapper<Scalars['Int']>;
Query: ResolverTypeWrapper<{}>;
QueryRoot: ResolverTypeWrapper<QueryRoot>;
Query: ResolverTypeWrapper<Query>;
QueryRoot: ResolverTypeWrapper<{}>;
String: ResolverTypeWrapper<Scalars['String']>;
SubscriptionRoot: ResolverTypeWrapper<{}>;
User: ResolverTypeWrapper<User>;
Expand All @@ -128,8 +128,8 @@ export type ResolversTypes = {
export type ResolversParentTypes = {
Boolean: Scalars['Boolean'];
Int: Scalars['Int'];
Query: {};
QueryRoot: QueryRoot;
Query: Query;
QueryRoot: {};
String: Scalars['String'];
SubscriptionRoot: {};
User: User;
Expand All @@ -140,6 +140,7 @@ export type QueryResolvers<
ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']
> = {
someDummyField?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type QueryRootResolvers<
Expand All @@ -154,7 +155,6 @@ export type QueryRootResolvers<
ContextType,
RequireFields<QueryRootUserByIdArgs, 'id'>
>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type SubscriptionRootResolvers<
Expand Down
Loading