Skip to content

remove generated code for subscriptions until we figure out what is n… #4

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
/build/
/node_modules/
*.tgz
84 changes: 84 additions & 0 deletions integration/graphql-types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ export type SaveAuthorResult = {

export type SearchResult = Author | Book;

export type Subscription = {
__typename?: 'Subscription';
authorSaved: Author;
bookSaved?: Maybe<Book>;
};


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

export enum Working {
Yes = 'YES',
No = 'NO'
Expand Down Expand Up @@ -129,6 +140,17 @@ export type CurrentAuthorQuery = (
)> }
);

export type AuthorSavedUpdateSubscriptionVariables = {};


export type AuthorSavedUpdateSubscription = (
{ __typename?: 'Subscription' }
& { authorSaved: (
{ __typename?: 'Author' }
& Pick<Author, 'name'>
) }
);


export const GetAuthorSummariesDocument = gql`
query GetAuthorSummaries {
Expand Down Expand Up @@ -211,6 +233,31 @@ export function withCurrentAuthor<TProps, TChildProps = {}>(operationOptions?: A
});
};
export type CurrentAuthorQueryResult = ApolloReactCommon.QueryResult<CurrentAuthorQuery, CurrentAuthorQueryVariables>;
export const AuthorSavedUpdateDocument = gql`
subscription AuthorSavedUpdate {
authorSaved {
name
}
}
`;
export type AuthorSavedUpdateComponentProps = Omit<ApolloReactComponents.SubscriptionComponentOptions<AuthorSavedUpdateSubscription, AuthorSavedUpdateSubscriptionVariables>, 'subscription'>;

export const AuthorSavedUpdateComponent = (props: AuthorSavedUpdateComponentProps) => (
<ApolloReactComponents.Subscription<AuthorSavedUpdateSubscription, AuthorSavedUpdateSubscriptionVariables> subscription={AuthorSavedUpdateDocument} {...props} />
);

export type AuthorSavedUpdateProps<TChildProps = {}> = ApolloReactHoc.DataProps<AuthorSavedUpdateSubscription, AuthorSavedUpdateSubscriptionVariables> & TChildProps;
export function withAuthorSavedUpdate<TProps, TChildProps = {}>(operationOptions?: ApolloReactHoc.OperationOption<
TProps,
AuthorSavedUpdateSubscription,
AuthorSavedUpdateSubscriptionVariables,
AuthorSavedUpdateProps<TChildProps>>) {
return ApolloReactHoc.withSubscription<TProps, AuthorSavedUpdateSubscription, AuthorSavedUpdateSubscriptionVariables, AuthorSavedUpdateProps<TChildProps>>(AuthorSavedUpdateDocument, {
alias: 'authorSavedUpdate',
...operationOptions
});
};
export type AuthorSavedUpdateSubscriptionResult = ApolloReactCommon.SubscriptionResult<AuthorSavedUpdateSubscription>;
export interface AuthorOptions {
__typename?: "Author";
name?: Author["name"];
Expand Down Expand Up @@ -363,6 +410,43 @@ function maybeNewOrNullSaveAuthorResult(
}
}

export interface SubscriptionOptions {
__typename?: "Subscription";
authorSaved?: AuthorOptions;
bookSaved?: BookOptions;
}

export function newSubscription(options: SubscriptionOptions = {}, cache: Record<string, any> = {}): Subscription {
const o = (cache["Subscription"] = {} as Subscription);
o.__typename = "Subscription";
o.authorSaved = maybeNewAuthor(options.authorSaved, cache);
o.bookSaved = maybeNewOrNullBook(options.bookSaved, cache);
return o;
}

function maybeNewSubscription(value: SubscriptionOptions | undefined, cache: Record<string, any>): Subscription {
if (value === undefined) {
return (cache["Subscription"] as Subscription) ?? newSubscription({}, cache);
} else if (value.__typename) {
return value as Subscription;
} else {
return newSubscription(value, cache);
}
}

function maybeNewOrNullSubscription(
value: SubscriptionOptions | undefined | null,
cache: Record<string, any>,
): Subscription | null {
if (!value) {
return null;
} else if (value.__typename) {
return value as Subscription;
} else {
return newSubscription(value, cache);
}
}

let nextFactoryIds: Record<string, number> = {};

export function resetFactoryIds() {
Expand Down
6 changes: 6 additions & 0 deletions integration/queries.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ query CurrentAuthor {
name
}
}

subscription AuthorSavedUpdate {
authorSaved {
name
}
}
6 changes: 6 additions & 0 deletions integration/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ union SearchResult = Author | Book
schema {
query: Query
mutation: Mutation
subscription: Subscription
}

type Query {
Expand All @@ -36,6 +37,11 @@ type Mutation {
saveAuthor(input: AuthorInput!): SaveAuthorResult!
}

type Subscription {
authorSaved: Author!
bookSaved(startsWith: String): Book
}

type SaveAuthorResult {
author: Author!
}
Expand Down
12 changes: 8 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import PluginOutput = Types.PluginOutput;
/** Generates `newQueryResponse({ ... })` factory functions in our `graphql-types` codegen output. */
export const plugin: PluginFunction = async (schema, documents) => {
const factories: Code[] = [];
documents.forEach(d => {
documents.forEach((d) => {
if (d.document) {
d.document.definitions.forEach(d => {
d.document.definitions.forEach((d) => {
if (d.kind === "OperationDefinition" && d.name) {
factories.push(newOperationFactory(schema, d));
}
Expand All @@ -30,11 +30,15 @@ function newOperationFactory(schema: GraphQLSchema, def: OperationDefinitionNode
const name = def.name?.value;
const hasVariables = (def.variableDefinitions?.length || 0) > 0;
const operation = `${def.operation.charAt(0).toUpperCase()}${def.operation.slice(1)}`;
// TODO skip Subscription operations until we figure out how to support
if (operation === "Subscription") {
return code``;
}
const rootType = operation === "Query" ? schema.getQueryType() : schema.getMutationType();

return code`
interface ${name}DataOptions {
${def.selectionSet.selections.map(s => {
${def.selectionSet.selections.map((s) => {
if (s.kind === "Field") {
const name = s.name.value;
const field = rootType?.getFields()[name];
Expand All @@ -55,7 +59,7 @@ function newOperationFactory(schema: GraphQLSchema, def: OperationDefinitionNode
export function new${name}Data(data: ${name}DataOptions) {
return {
__typename: "${operation}" as const,
${def.selectionSet.selections.map(s => {
${def.selectionSet.selections.map((s) => {
// This is the top-level Mutation/Query result, so usually/basically always has a single
// field like `saveAuthor: AuthorResult!` where we can use the existing `newAuthorResult` factory.
if (s.kind === "Field") {
Expand Down