Skip to content

Commit

Permalink
Make callsToGraphQL and callsFromGraphQL preserve type (fixes fac…
Browse files Browse the repository at this point in the history
  • Loading branch information
denvned committed Aug 13, 2016
1 parent bb38793 commit d45f27d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/query/__tests__/RelayQueryRoot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('RelayQueryRoot', () => {
expect(me.getIdentifyingArg()).toEqual(undefined);

expect(usernames.getIdentifyingArg()).toEqual(
{name: 'names', value: 'mroch'}
{name: 'names', type: '[String!]!', value: 'mroch'}
);

expect(getNode(Relay.QL`
Expand Down
12 changes: 8 additions & 4 deletions src/query/__tests__/callsToGraphQL-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,43 @@ describe('callsToGraphQL', function() {
it('converts array calls with null values', () => {
const relayCalls = [{
name: 'size',
type: 'Int',
value: null,
}];
const graphqlCalls = [RelayTestUtils.createCall('size', null)];
const graphqlCalls = [RelayTestUtils.createCall('size', null, 'Int')];
expect(callsFromGraphQL(graphqlCalls)).toEqual(relayCalls);
expect(callsToGraphQL(relayCalls)).toEqual(graphqlCalls);
});

it('converts array calls without values', () => {
const relayCalls = [{
name: 'size',
type: '[Int]',
value: [],
}];
const graphqlCalls = [RelayTestUtils.createCall('size', [])];
const graphqlCalls = [RelayTestUtils.createCall('size', [], '[Int]')];
expect(callsFromGraphQL(graphqlCalls)).toEqual(relayCalls);
expect(callsToGraphQL(relayCalls)).toEqual(graphqlCalls);
});

it('converts calls with array values', () => {
const relayCalls = [{
name: 'size',
type: '[Int]',
value: [32, 64],
}];
const graphqlCalls = [RelayTestUtils.createCall('size', [32, 64])];
const graphqlCalls = [RelayTestUtils.createCall('size', [32, 64], '[Int]')];
expect(callsFromGraphQL(graphqlCalls)).toEqual(relayCalls);
expect(callsToGraphQL(relayCalls)).toEqual(graphqlCalls);
});

it('converts singular calls with null values', () => {
const relayCalls = [{
name: 'size',
type: 'Int',
value: 32,
}];
const graphqlCalls = [RelayTestUtils.createCall('size', 32)];
const graphqlCalls = [RelayTestUtils.createCall('size', 32, 'Int')];
expect(callsFromGraphQL(graphqlCalls)).toEqual(relayCalls);
expect(callsToGraphQL(relayCalls)).toEqual(graphqlCalls);
});
Expand Down
10 changes: 9 additions & 1 deletion src/query/callsFromGraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const invariant = require('invariant');

type CallOrDirective = {
name: string,
metadata?: {
type?: ?string,
},
value: ?ConcreteValue,
};

Expand Down Expand Up @@ -53,7 +56,12 @@ function callsFromGraphQL(
value = getCallValue(value, variables);
}
}
orderedCalls.push({name: callOrDirective.name, value});
const {metadata, name} = callOrDirective;
const orderedCall: Call = {name, value};
if (metadata && metadata.type) {
orderedCall.type = metadata.type;
}
orderedCalls.push(orderedCall);
}
return orderedCalls;
}
Expand Down
4 changes: 2 additions & 2 deletions src/query/callsToGraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ const QueryBuilder = require('QueryBuilder');
* Convert from plain object `{name, value}` calls to GraphQL call nodes.
*/
function callsToGraphQL(calls: Array<Call>): Array<ConcreteCall> {
return calls.map(({name, value}) => {
return calls.map(({name, type, value}) => {
let concreteValue = null;
if (Array.isArray(value)) {
concreteValue = value.map(QueryBuilder.createCallValue);
} else if (value != null) {
concreteValue = QueryBuilder.createCallValue(value);
}
return QueryBuilder.createCall(name, concreteValue);
return QueryBuilder.createCall(name, concreteValue, type);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/tools/__mocks__/RelayTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ const RelayTestUtils = {
return reference;
},

createCall(name, value) {
createCall(name, value, type) {
const QueryBuilder = require('QueryBuilder');

if (Array.isArray(value)) {
value = value.map(QueryBuilder.createCallValue);
} else if (value != null) {
value = QueryBuilder.createCallValue(value);
}
return QueryBuilder.createCall(name, value);
return QueryBuilder.createCall(name, value, type);
},

createContainerFragment(fragment) {
Expand Down

0 comments on commit d45f27d

Please sign in to comment.