Skip to content

Commit

Permalink
Merge pull request #221 from FormidableLabs/fix/always-parse-doc
Browse files Browse the repository at this point in the history
Change GraphQLRequest to always contain a parsed document
  • Loading branch information
kitten authored Apr 5, 2019
2 parents e58fc9e + 3dba2ca commit 4eada3b
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 339 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"@types/fast-json-stable-stringify": "^2.0.0",
"create-react-context": "^0.2.3",
"fast-json-stable-stringify": "^2.0.0",
"graphql-tag": "^2.10.1",
"prop-types": "^15.6.0",
"wonka": "^2.0.1"
}
Expand Down
17 changes: 12 additions & 5 deletions src/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parse, print } from 'graphql';
import { print } from 'graphql';
import gql from 'graphql-tag';

/** NOTE: Testing in this file is designed to test both the client and it's interaction with default Exchanges */

Expand All @@ -24,7 +25,13 @@ describe('createClient', () => {

const query = {
key: 1,
query: `{ todos { id } }`,
query: gql`
{
todos {
id
}
}
`,
variables: { example: 1234 },
};

Expand Down Expand Up @@ -66,7 +73,7 @@ describe('executeQuery', () => {
);

const receivedQuery = receivedOps[0].query;
expect(print(receivedQuery)).toBe(print(parse(query.query)));
expect(print(receivedQuery)).toBe(print(query.query));
});

it('passes variables type to exchange', () => {
Expand Down Expand Up @@ -105,7 +112,7 @@ describe('executeMutation', () => {
);

const receivedQuery = receivedOps[0].query;
expect(print(receivedQuery)).toBe(print(parse(query.query)));
expect(print(receivedQuery)).toBe(print(query.query));
});

it('passes variables type to exchange', () => {
Expand Down Expand Up @@ -144,7 +151,7 @@ describe('executeSubscription', () => {
);

const receivedQuery = receivedOps[0].query;
expect(print(receivedQuery)).toBe(print(parse(query.query)));
expect(print(receivedQuery)).toBe(print(query.query));
});

it('passes variables type to exchange', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { parse } from 'graphql';

import {
filter,
makeSubject,
Expand Down Expand Up @@ -101,7 +99,7 @@ export class Client {
opts?: Partial<OperationContext>
): Operation => ({
key,
query: typeof query === 'string' ? parse(query) : query,
query,
variables,
operationName: type,
context: this.createOperationContext(opts),
Expand Down
13 changes: 8 additions & 5 deletions src/components/Mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ jest.mock('../context', () => {
});

import { mount } from 'enzyme';
import { print } from 'graphql';
import gql from 'graphql-tag';
import React from 'react';
import { fromValue } from 'wonka';

// @ts-ignore - client is exclusively from mock
import { client } from '../context';
import { Mutation } from './Mutation';

const props = {
query: 'examplequery',
query: 'mutation Example { example }',
};

let childProps: any;

const childMock = (c: any) => {
Expand Down Expand Up @@ -59,10 +63,9 @@ describe('execute mutation', () => {
it('calls executeMutation with query', () => {
mountWrapper(props);
childProps.executeMutation();
expect(client.executeMutation.mock.calls[0][0]).toHaveProperty(
'query',
props.query
);

const call = client.executeMutation.mock.calls[0][0];
expect(print(call.query)).toBe(print(gql([props.query])));
});

it('calls executeMutation with variables', () => {
Expand Down
7 changes: 4 additions & 3 deletions src/components/Mutation.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { DocumentNode } from 'graphql';
import React, { Component, FC, ReactNode } from 'react';
import { pipe, toPromise } from 'wonka';
import { Client } from '../client';
import { Consumer } from '../context';
import { GraphQLRequest, OperationResult } from '../types';
import { Omit, OperationResult } from '../types';
import { CombinedError, createRequest } from '../utils';

interface MutationHandlerProps {
client: Client;
query: GraphQLRequest['query'];
query: string | DocumentNode;
children: (arg: MutationChildProps) => ReactNode;
}

Expand Down Expand Up @@ -66,7 +67,7 @@ class MutationHandler extends Component<
};
}

type MutationProps = Exclude<MutationHandlerProps, 'client'>;
type MutationProps = Omit<MutationHandlerProps, 'client'>;

export const Mutation: FC<MutationProps> = props => (
<Consumer>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { client } from '../context';
import { Query } from './Query';

const props = {
query: 'examplequery',
query: '{ example }',
};
let childProps: any;

Expand Down Expand Up @@ -64,7 +64,7 @@ describe('on change', () => {
const wrapper = mountWrapper(props);

// @ts-ignore
wrapper.setProps({ ...props, query: 'new query' });
wrapper.setProps({ ...props, query: '{ newQuery }' });
expect(client.executeQuery).toBeCalledTimes(2);
});
});
Expand Down
7 changes: 5 additions & 2 deletions src/components/Query.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { DocumentNode } from 'graphql';
import React, { Component, FC, ReactNode } from 'react';
import { pipe, subscribe } from 'wonka';
import { Client } from '../client';
import { Consumer } from '../context';
import { GraphQLRequest, OperationContext, RequestPolicy } from '../types';
import { Omit, OperationContext, RequestPolicy } from '../types';
import { CombinedError, createRequest, noop } from '../utils';

interface QueryHandlerProps extends Omit<GraphQLRequest, 'key'> {
interface QueryHandlerProps {
query: string | DocumentNode;
variables?: object;
client: Client;
requestPolicy?: RequestPolicy;
children: (arg: QueryHandlerState) => ReactNode;
Expand Down
9 changes: 6 additions & 3 deletions src/components/Subscription.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { DocumentNode } from 'graphql';
import React, { Component, FC, ReactNode } from 'react';
import { pipe, subscribe } from 'wonka';
import { Client } from '../client';
import { Consumer } from '../context';
import { GraphQLRequest } from '../types';
import { Omit } from '../types';
import { CombinedError, createRequest, noop } from '../utils';

interface SubscriptionHandlerProps extends GraphQLRequest {
interface SubscriptionHandlerProps {
query: string | DocumentNode;
variables?: object;
client: Client;
handler?: (prev: any | void, data: any) => any;
children: (arg: SubscriptionHandlerState) => ReactNode;
Expand Down Expand Up @@ -70,7 +73,7 @@ class SubscriptionHandler extends Component<
}
}

type SubscriptionProps = Exclude<SubscriptionHandlerProps, 'client'>;
type SubscriptionProps = Omit<SubscriptionHandlerProps, 'client'>;

export const Subscription: FC<SubscriptionProps> = props => (
<Consumer>
Expand Down
Loading

0 comments on commit 4eada3b

Please sign in to comment.