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) - promisified requests #405

Merged
merged 13 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
56 changes: 55 additions & 1 deletion src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,66 @@ describe('exchange args', () => {
expect(typeof exchangeMock.mock.calls[0][0].forward).toBe('function');
});

it('recieves client', () => {
it('receives client', () => {
// @ts-ignore
expect(exchangeMock.mock.calls[0][0]).toHaveProperty('client', client);
});
});

describe('promisified arguments', () => {
it('query', () => {
client.query({
query: gql`
{
todos {
id
}
}
`,
variables: { example: 1234 },
context: {},
});

const received = receivedOps[0];
expect(print(received.query)).toEqual(print(query.query));
expect(received.key).toBeDefined();
expect(received.variables).toEqual({ example: 1234 });
expect(received.operationName).toEqual('query');
expect(received.context).toEqual({
url: 'https://hostname.com',
requestPolicy: 'cache-and-network',
fetchOptions: undefined,
fetch: undefined,
});
});

it('mutation', () => {
client.mutation({
query: gql`
{
todos {
id
}
}
`,
variables: { example: 1234 },
context: {},
});

const received = receivedOps[0];
expect(print(received.query)).toEqual(print(query.query));
expect(received.key).toBeDefined();
expect(received.variables).toEqual({ example: 1234 });
expect(received.operationName).toEqual('mutation');
expect(received.context).toEqual({
url: 'https://hostname.com',
requestPolicy: 'cache-and-network',
fetchOptions: undefined,
fetch: undefined,
});
});
});

describe('executeQuery', () => {
it('passes query string exchange', () => {
pipe(
Expand Down
36 changes: 35 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
interval,
fromValue,
switchMap,
toPromise,
} from 'wonka';

import {
Expand All @@ -30,7 +31,8 @@ import {
RequestPolicy,
} from './types';

import { toSuspenseSource } from './utils';
import { createRequest, toSuspenseSource } from './utils';
import { DocumentNode } from 'graphql';

/** Options for configuring the URQL [client]{@link Client}. */
export interface ClientOptions {
Expand All @@ -52,6 +54,12 @@ interface ActiveOperations {
[operationKey: string]: number;
}

interface PromisfiedOperation {
context?: Partial<OperationContext>;
query: string | DocumentNode;
variables?;
}

export const createClient = (opts: ClientOptions) => new Client(opts);

/** The URQL application-wide client library. Each execute method starts a GraphQL request and returns a stream of results. */
Expand Down Expand Up @@ -196,6 +204,19 @@ export class Client {
}
};

query({
query,
variables,
context,
}: PromisfiedOperation): Promise<OperationResult> {
const request = createRequest(query, variables);
return pipe(
this.executeQuery(request, context),
take(1),
toPromise
);
}

executeQuery = (
query: GraphQLRequest,
opts?: Partial<OperationContext>
Expand All @@ -222,6 +243,19 @@ export class Client {
return this.executeRequestOperation(operation);
};

mutation({
query,
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
variables,
context,
}: PromisfiedOperation): Promise<OperationResult> {
const request = createRequest(query, variables);
return pipe(
this.executeMutation(request, context),
take(1),
toPromise
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
);
}

executeMutation = (
query: GraphQLRequest,
opts?: Partial<OperationContext>
Expand Down