Skip to content

Commit

Permalink
Readd proper inheritance pattern for Client constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed May 12, 2021
1 parent 6b6bfad commit ab0a0ac
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/__snapshots__/client.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createClient passes snapshot 1`] = `
exports[`createClient / Client passes snapshot 1`] = `
Client {
"constructor": [Function],
"createOperationContext": [Function],
"createRequestOperation": [Function],
"executeMutation": [Function],
Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ import {
import { gql } from './gql';
import { Exchange, Operation, OperationResult } from './types';
import { makeOperation } from './utils';
import { createClient } from './client';
import { Client, createClient } from './client';
import { queryOperation, subscriptionOperation } from './test-utils';

const url = 'https://hostname.com';

describe('createClient', () => {
it('passes snapshot', () => {
const c = createClient({
url,
});
describe('createClient / Client', () => {
it('creates an instance of Client', () => {
expect(createClient({ url }) instanceof Client).toBeTruthy();
expect(new Client({ url }) instanceof Client).toBeTruthy();
});

expect(c).toMatchSnapshot();
it('passes snapshot', () => {
const client = createClient({ url });
expect(client).toMatchSnapshot();
});
});

Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export interface Client {
}

export const Client: new (opts: ClientOptions) => Client = function Client(
this: Client | {},
opts: ClientOptions
) {
if (process.env.NODE_ENV !== 'production' && !opts.url) {
Expand Down Expand Up @@ -251,7 +252,9 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
return source;
};

const client = {
const instance: Client =
this instanceof Client ? this : Object.create(Client.prototype);
const client: Client = Object.assign(instance, {
url: opts.url,
fetchOptions: opts.fetchOptions,
fetch: opts.fetch,
Expand Down Expand Up @@ -385,7 +388,7 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
client.executeMutation(createRequest(query, variables), context)
);
},
} as Client;
} as Client);

let dispatchDebug: ExchangeInput['dispatchDebug'] = noop;
if (process.env.NODE_ENV !== 'production') {
Expand Down Expand Up @@ -417,7 +420,6 @@ export const Client: new (opts: ClientOptions) => Client = function Client(
// cancellations cascading up from components
pipe(results$, publish);

client.constructor = Client;
return client;
} as any;

Expand Down

0 comments on commit ab0a0ac

Please sign in to comment.