Skip to content

Commit

Permalink
chore: workaround broken apollo-client import mess
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jun 24, 2022
1 parent 0d5a52c commit ff3c071
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
14 changes: 9 additions & 5 deletions packages/links/src/AwaitVariablesLink.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApolloLink, FetchResult, NextLink, Operation } from '@apollo/client/link/core';
import { Observable } from '@apollo/client/utilities';
import * as apolloImport from '@apollo/client';

const apollo: typeof apolloImport = (apolloImport as any)?.default ?? apolloImport;

function getFinalPromise(object: any): Promise<any> {
return Promise.resolve(object).then(resolvedObject => {
Expand All @@ -23,9 +24,12 @@ function getFinalPromise(object: any): Promise<any> {
});
}

export class AwaitVariablesLink extends ApolloLink {
request(operation: Operation, forward: NextLink): Observable<FetchResult> {
return new Observable(observer => {
export class AwaitVariablesLink extends apollo.ApolloLink {
request(
operation: apolloImport.Operation,
forward: apolloImport.NextLink
): apolloImport.Observable<apolloImport.FetchResult> {
return new apollo.Observable(observer => {
let subscription: any;
getFinalPromise(operation.variables)
.then(resolvedVariables => {
Expand Down
6 changes: 4 additions & 2 deletions packages/links/src/createServerHttpLink.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { concat } from '@apollo/client/link/core';
import * as apolloImport from '@apollo/client';
import { createUploadLink, formDataAppendFile, isExtractableFile } from 'apollo-upload-client';
import FormData from 'form-data';
import fetch from 'node-fetch';

import { AwaitVariablesLink } from './AwaitVariablesLink.js';

const apollo: typeof apolloImport = (apolloImport as any)?.default ?? apolloImport;

export const createServerHttpLink = (options: any) =>
concat(
apollo.concat(
new AwaitVariablesLink(),
createUploadLink({
...options,
Expand Down
14 changes: 7 additions & 7 deletions packages/links/src/linkToExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { toPromise } from '@apollo/client/core';
import { ApolloLink, execute } from '@apollo/client/link/core';
import { Observable } from '@apollo/client/utilities';
import * as apolloImport from '@apollo/client';

import {
Executor,
Expand All @@ -10,11 +8,13 @@ import {
getOperationASTFromRequest,
} from '@graphql-tools/utils';

export function linkToExecutor(link: ApolloLink): Executor {
const apollo: typeof apolloImport = (apolloImport as any)?.default ?? apolloImport;

export function linkToExecutor(link: apolloImport.ApolloLink): Executor {
return function executorFromLink<TReturn, TArgs extends Record<string, any>, TContext>(
request: ExecutionRequest<TArgs, TContext>
) {
const observable = execute(link, {
const observable = apollo.execute(link, {
query: request.document,
operationName: request.operationName,
variables: request.variables,
Expand All @@ -24,11 +24,11 @@ export function linkToExecutor(link: ApolloLink): Executor {
clientAwareness: {},
},
extensions: request.extensions,
}) as Observable<ExecutionResult<TReturn>>;
}) as apolloImport.Observable<ExecutionResult<TReturn>>;
const operationAst = getOperationASTFromRequest(request);
if (operationAst.operation === 'subscription') {
return observableToAsyncIterable<ExecutionResult<TReturn>>(observable);
}
return toPromise(observable);
return apollo.toPromise(observable);
};
}

0 comments on commit ff3c071

Please sign in to comment.