From cd884e10f674bb6f37df5a1de047a636257ab093 Mon Sep 17 00:00:00 2001 From: Donnie Flood Date: Fri, 21 Aug 2020 16:42:04 -0600 Subject: [PATCH] remove generated code for subscriptions until we figure out what is needed --- .gitignore | 1 + integration/graphql-types.tsx | 84 +++++++++++++++++++++++++++++++++++ integration/queries.graphql | 6 +++ integration/schema.graphql | 6 +++ src/index.ts | 12 +++-- 5 files changed, 105 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index d31378a..ab8cbfb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.idea/ /build/ /node_modules/ +*.tgz \ No newline at end of file diff --git a/integration/graphql-types.tsx b/integration/graphql-types.tsx index 729b2d7..70b239f 100644 --- a/integration/graphql-types.tsx +++ b/integration/graphql-types.tsx @@ -83,6 +83,17 @@ export type SaveAuthorResult = { export type SearchResult = Author | Book; +export type Subscription = { + __typename?: 'Subscription'; + authorSaved: Author; + bookSaved?: Maybe; +}; + + +export type SubscriptionBookSavedArgs = { + startsWith?: Maybe; +}; + export enum Working { Yes = 'YES', No = 'NO' @@ -129,6 +140,17 @@ export type CurrentAuthorQuery = ( )> } ); +export type AuthorSavedUpdateSubscriptionVariables = {}; + + +export type AuthorSavedUpdateSubscription = ( + { __typename?: 'Subscription' } + & { authorSaved: ( + { __typename?: 'Author' } + & Pick + ) } +); + export const GetAuthorSummariesDocument = gql` query GetAuthorSummaries { @@ -211,6 +233,31 @@ export function withCurrentAuthor(operationOptions?: A }); }; export type CurrentAuthorQueryResult = ApolloReactCommon.QueryResult; +export const AuthorSavedUpdateDocument = gql` + subscription AuthorSavedUpdate { + authorSaved { + name + } +} + `; +export type AuthorSavedUpdateComponentProps = Omit, 'subscription'>; + + export const AuthorSavedUpdateComponent = (props: AuthorSavedUpdateComponentProps) => ( + subscription={AuthorSavedUpdateDocument} {...props} /> + ); + +export type AuthorSavedUpdateProps = ApolloReactHoc.DataProps & TChildProps; +export function withAuthorSavedUpdate(operationOptions?: ApolloReactHoc.OperationOption< + TProps, + AuthorSavedUpdateSubscription, + AuthorSavedUpdateSubscriptionVariables, + AuthorSavedUpdateProps>) { + return ApolloReactHoc.withSubscription>(AuthorSavedUpdateDocument, { + alias: 'authorSavedUpdate', + ...operationOptions + }); +}; +export type AuthorSavedUpdateSubscriptionResult = ApolloReactCommon.SubscriptionResult; export interface AuthorOptions { __typename?: "Author"; name?: Author["name"]; @@ -363,6 +410,43 @@ function maybeNewOrNullSaveAuthorResult( } } +export interface SubscriptionOptions { + __typename?: "Subscription"; + authorSaved?: AuthorOptions; + bookSaved?: BookOptions; +} + +export function newSubscription(options: SubscriptionOptions = {}, cache: Record = {}): 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): 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, +): Subscription | null { + if (!value) { + return null; + } else if (value.__typename) { + return value as Subscription; + } else { + return newSubscription(value, cache); + } +} + let nextFactoryIds: Record = {}; export function resetFactoryIds() { diff --git a/integration/queries.graphql b/integration/queries.graphql index 4d93090..3b5ac90 100644 --- a/integration/queries.graphql +++ b/integration/queries.graphql @@ -18,3 +18,9 @@ query CurrentAuthor { name } } + +subscription AuthorSavedUpdate { + authorSaved { + name + } +} diff --git a/integration/schema.graphql b/integration/schema.graphql index 83f2615..b089e4e 100644 --- a/integration/schema.graphql +++ b/integration/schema.graphql @@ -23,6 +23,7 @@ union SearchResult = Author | Book schema { query: Query mutation: Mutation + subscription: Subscription } type Query { @@ -36,6 +37,11 @@ type Mutation { saveAuthor(input: AuthorInput!): SaveAuthorResult! } +type Subscription { + authorSaved: Author! + bookSaved(startsWith: String): Book +} + type SaveAuthorResult { author: Author! } diff --git a/src/index.ts b/src/index.ts index 015ab82..5463820 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)); } @@ -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]; @@ -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") {