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

printRelayQuery - print children before generating variables #211

Merged
merged 1 commit into from
Sep 1, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions scripts/jest/testschema.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type Root {
checkinSearchQuery(query: CheckinSearchInput): CheckinSearchResult
defaultSettings: Settings,
settings(environment: Environment): Settings
me: User
story: Story
Expand Down Expand Up @@ -513,4 +514,5 @@ enum Environment {

type Settings {
notificationSounds: Boolean
notifications(environment: Environment): Boolean
}
101 changes: 68 additions & 33 deletions scripts/jest/testschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "defaultSettings",
"description": null,
"args": [],
"type": {
"kind": "OBJECT",
"name": "Settings",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "settings",
"description": null,
Expand Down Expand Up @@ -257,6 +269,16 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "SCALAR",
"name": "String",
"description": null,
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "CheckinSearchResult",
Expand All @@ -280,39 +302,6 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "SCALAR",
"name": "String",
"description": null,
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "ENUM",
"name": "Environment",
"description": null,
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": [
{
"name": "WEB",
"description": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "MOBILE",
"description": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "Settings",
Expand All @@ -329,6 +318,29 @@
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "notifications",
"description": null,
"args": [
{
"name": "environment",
"description": null,
"type": {
"kind": "ENUM",
"name": "Environment",
"ofType": null
},
"defaultValue": null
}
],
"type": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
Expand All @@ -346,6 +358,29 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "ENUM",
"name": "Environment",
"description": null,
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": [
{
"name": "WEB",
"description": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "MOBILE",
"description": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "User",
Expand Down
34 changes: 34 additions & 0 deletions src/__forks__/traversal/__tests__/printRelayQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,40 @@ describe('printRelayQuery', () => {
expect(variables).toEqual({});
});

it('prints object call values', () => {
var enumValue = 'WEB';
var fragment = Relay.QL`
fragment on Settings {
notifications(environment: $env)
}
`;
var query = getNode(Relay.QL`
query {
defaultSettings {
${fragment},
}
}
`, {
env: enumValue,
});
var fragmentID = getNode(fragment, {env: enumValue}).getFragmentID();
var alias = generateRQLFieldAlias('notifications.environment(WEB)');
var {text, variables} = printRelayQuery(query);
expect(trimQuery(text)).toEqual(trimQuery(`
query UnknownFile($environment_0:Environment) {
defaultSettings {
...${fragmentID}
}
}
fragment ${fragmentID} on Settings {
${alias}:notifications(environment:$environment_0)
}
`));
expect(variables).toEqual({
environment_0: enumValue,
});
});

it('prints inline fragments as references', () => {
// these fragments have different types and cannot be flattened
var nestedFragment = Relay.QL`fragment on User{name}`;
Expand Down
10 changes: 4 additions & 6 deletions src/__forks__/traversal/printRelayQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,18 @@ function printRoot(
rootFieldString += '(' + rootArgString + ')';
}
}
var children = printChildren(node, printerState);

var argStrings = null;
forEachObject(printerState.variableMap, (variable, variableID) => {
if (variable) {
argStrings = argStrings || [];
argStrings.push('$' + variableID + ':' + variable.type);
}
argStrings = argStrings || [];
argStrings.push('$' + variableID + ':' + variable.type);
});
if (argStrings) {
queryString += '(' + argStrings.join(',') + ')';
}

return 'query ' + queryString + '{' +
rootFieldString + printChildren(node, printerState) + '}';
return 'query ' + queryString + '{' + rootFieldString + children + '}';
}

function printMutation(
Expand Down