Skip to content

Commit

Permalink
Simplify node creation API
Browse files Browse the repository at this point in the history
Summary: In fa5e1181 I changed the node building
API to make it a bit cleaner:

- `RelayQuery.Node.buildField` -> `RelayQuery.Field.build`
- `RelayQuery.Node.buildFragment` -> `RelayQuery.Fragment.build`
- `RelayQuery.Node.buildMutation` -> `RelayQuery.Mutation.build`
- `RelayQuery.Node.buildRoot` -> `RelayQuery.Root.build`

This is the corresponding change for `RelayQuery.Node.create*`:

- `RelayQuery.Node.createFragment` -> `RelayQuery.Fragment.create`
- `RelayQuery.Node.createQuery` -> `RelayQuery.Root.create`
- `RelayQuery.Node.create` -> all but one call site (in `RelayTestUtils`)
  removed in favor of explicit `RelayQuery.*.create`
- `fromGraphQL.Node` (one of the few callers to `RelayQuery.Node.create`)
  removed

Reviewed By: @josephsavona

Differential Revision: D2473617
  • Loading branch information
wincent authored and facebook-github-bot-7 committed Sep 29, 2015
1 parent b6e8246 commit 5e77de5
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 90 deletions.
34 changes: 27 additions & 7 deletions src/container/RelayContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ function createContainerComponent(
);
return null;
}
var fragment = RelayQuery.Node.create(
fragmentReference.defer(),
RelayMetaRoute.get(this.context.route.name),
var fragment = getDeferredFragment(
fragmentReference,
this.context,
this.state.variables
);
invariant(
Expand Down Expand Up @@ -418,9 +418,9 @@ function createContainerComponent(
componentName,
componentName
);
var fragment = RelayQuery.Node.create(
fragmentReference.defer(),
RelayMetaRoute.get(this.context.route.name),
var fragment = getDeferredFragment(
fragmentReference,
this.context,
this.state.variables
);
invariant(
Expand Down Expand Up @@ -813,7 +813,7 @@ function createContainerComponent(
if (prepareVariables) {
variables = prepareVariables(variables, metaRoute);
}
return RelayQuery.Node.createFragment(
return RelayQuery.Fragment.create(
fragment,
metaRoute,
variables
Expand Down Expand Up @@ -929,6 +929,26 @@ function buildContainerFragment(
return fragment;
}

function getDeferredFragment(
fragmentReference: RelayFragmentReference,
context: Object,
variables: Variables
): RelayQuery.Fragment {
var route = RelayMetaRoute.get(context.route.name);
var concreteFragment = fragmentReference.getFragment(variables);
var concreteVariables = fragmentReference.getVariables(route, variables);
return RelayQuery.Fragment.create(
concreteFragment,
route,
concreteVariables,
{
isDeferred: true,
isContainerFragment: fragmentReference.isContainerFragment(),
isTypeConditional: fragmentReference.isTypeConditional(),
}
);
}

/**
* Creates a lazy Relay container. The actual container is created the first
* time a container is being constructed by React's rendering engine.
Expand Down
2 changes: 1 addition & 1 deletion src/container/getRelayQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function getRelayQueries(
queryName
);
if (concreteQuery) {
var rootQuery = RelayQuery.Node.createQuery(
var rootQuery = RelayQuery.Root.create(
concreteQuery,
RelayMetaRoute.get(route.name),
route.params
Expand Down
2 changes: 1 addition & 1 deletion src/mutation/RelayMutationQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ var RelayMutationQuery = {
switch (config.type) {
case RelayMutationType.REQUIRED_CHILDREN:
children = children.concat(config.children.map(child =>
RelayQuery.Node.create(
RelayQuery.Fragment.create(
child,
RelayMetaRoute.get('$buildQuery'),
{}
Expand Down
8 changes: 4 additions & 4 deletions src/mutation/__tests__/RelayMutationQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var intersectRelayQuery = require('intersectRelayQuery');
var inferRelayFieldsFromData = require('inferRelayFieldsFromData');

describe('RelayMutationQuery', () => {
function getNodeChildren(query) {
return fromGraphQL.Node(query).getChildren();
function getNodeChildren(fragment) {
return fromGraphQL.Fragment(fragment).getChildren();
}
function getNodeWithoutSource(...args) {
var filterCallback = RelayConnectionInterface.EDGES_HAVE_SOURCE_FIELD ?
Expand Down Expand Up @@ -153,14 +153,14 @@ describe('RelayMutationQuery', () => {
}
`);
var trackedNodes = {
'123': fromGraphQL.Node(Relay.QL`
'123': fromGraphQL.Fragment(Relay.QL`
fragment on Story {
message {
text
}
}
`),
'456': fromGraphQL.Node(Relay.QL`
'456': fromGraphQL.Fragment(Relay.QL`
fragment on Story {
actors {
name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ describe('RelayDefaultNetworkLayer', () => {

beforeEach(() => {
route = RelayMetaRoute.get('$fetchRelayQuery');
queryA = RelayQuery.Node.create(
queryA = RelayQuery.Root.create(
Relay.QL`query{node(id:"123"){id}}`, route, {}
);
queryB = RelayQuery.Node.create(
queryB = RelayQuery.Root.create(
Relay.QL`query{node(id:"456"){id}}`, route, {}
);
requestA = new RelayQueryRequest(queryA);
Expand Down
91 changes: 55 additions & 36 deletions src/query/RelayQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,42 +119,6 @@ class RelayQueryNode {
return node;
}

static createFragment(
concreteNode: ConcreteQueryObject,
route: RelayMetaRoute,
variables: Variables,
metadata?: ?FragmentMetadata
): RelayQueryFragment {
invariant(
GraphQL.isFragment(concreteNode),
'RelayQuery.createQuery(): Expected a concrete query fragment, got: %s',
concreteNode
);
return createMemoizedFragment(
concreteNode,
route,
variables,
metadata || DEFAULT_FRAGMENT_METADATA
);
}

static createQuery(
concreteNode: ConcreteQueryObject,
route: RelayMetaRoute,
variables: Variables
): RelayQueryRoot {
invariant(
GraphQL.isQuery(concreteNode),
'RelayQuery.createQuery(): Expected a concrete query, got: %s',
concreteNode
);
return new RelayQueryRoot(
concreteNode,
route,
variables
);
}

/**
* @private
*
Expand Down Expand Up @@ -390,6 +354,23 @@ class RelayQueryRoot extends RelayQueryNode {
return root;
}

static create(
concreteNode: ConcreteQueryObject,
route: RelayMetaRoute,
variables: Variables
): RelayQueryRoot {
invariant(
GraphQL.isQuery(concreteNode),
'RelayQueryRoot.create(): Expected a concrete query, got: %s',
concreteNode
);
return new RelayQueryRoot(
concreteNode,
route,
variables
);
}

constructor(
concreteNode: ConcreteQueryObject,
route: RelayMetaRoute,
Expand Down Expand Up @@ -622,6 +603,24 @@ class RelayQueryMutation extends RelayQueryOperation {
* Represents a GraphQL subscription.
*/
class RelayQuerySubscription extends RelayQueryOperation {
static create(
concreteNode: ConcreteQueryObject,
route: RelayMetaRoute,
variables: Variables
): RelayQuerySubscription {
invariant(
GraphQL.isSubscription(concreteNode),
'RelayQuerySubscription.create(): ' +
'Expected a concrete subscription, got: %s',
concreteNode
);
return new RelayQuerySubscription(
concreteNode,
route,
variables
);
}

getPublishedPayloadType(): string {
return this.getResponseType();
}
Expand Down Expand Up @@ -688,6 +687,26 @@ class RelayQueryFragment extends RelayQueryNode {
return fragment;
}

static create(
concreteNode: ConcreteQueryObject,
route: RelayMetaRoute,
variables: Variables,
metadata?: ?FragmentMetadata
): RelayQueryFragment {
invariant(
GraphQL.isFragment(concreteNode),
'RelayQueryFragment.create(): ' +
'Expected a concrete query fragment, got: %s',
concreteNode
);
return createMemoizedFragment(
concreteNode,
route,
variables,
metadata || DEFAULT_FRAGMENT_METADATA
);
}

constructor(
concreteNode: ConcreteQueryObject,
route: RelayMetaRoute,
Expand Down
8 changes: 4 additions & 4 deletions src/query/__tests__/RelayQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ describe('RelayQuery', () => {
var route1 = RelayMetaRoute.get('route1');
var route2 = RelayMetaRoute.get('route2');

var node1 = RelayQuery.Node.create(fragment, route1, variables);
var node2 = RelayQuery.Node.create(fragment, route2, variables);
var node1 = RelayQuery.Fragment.create(fragment, route1, variables);
var node2 = RelayQuery.Fragment.create(fragment, route2, variables);
expect(node1.isEquivalent(node2)).toBe(false);
});

Expand All @@ -228,8 +228,8 @@ describe('RelayQuery', () => {
var variables = {a: false};
var route = RelayMetaRoute.get('route1');

var node1 = RelayQuery.Node.create(fragment, route, variables);
var node2 = RelayQuery.Node.create(fragment, route, variables);
var node1 = RelayQuery.Fragment.create(fragment, route, variables);
var node2 = RelayQuery.Fragment.create(fragment, route, variables);
expect(node1.isEquivalent(node2)).toBe(true);
});
});
Expand Down
13 changes: 8 additions & 5 deletions src/query/__tests__/RelayQueryFragment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ describe('RelayQueryFragment', () => {
var node = Relay.QL`fragment on Node{id}`;
var route = RelayMetaRoute.get('Foo');
var variables = {};
var fragment = RelayQuery.Node.create(node, route, variables);
var fragment = RelayQuery.Fragment.create(node, route, variables);
var fragmentID = generateRQLFieldAlias('_RelayQueryFragment0.Foo.{}');
expect(fragment.getFragmentID()).toBe(fragmentID);

route = RelayMetaRoute.get('Bar');
fragment = RelayQuery.Node.create(node, route, variables);
fragment = RelayQuery.Fragment.create(node, route, variables);
fragmentID = generateRQLFieldAlias('_RelayQueryFragment0.Bar.{}');
expect(fragment.getFragmentID()).toBe(fragmentID);

variables = {foo: 'bar'};
fragment = RelayQuery.Node.create(node, route, variables);
fragment = RelayQuery.Fragment.create(node, route, variables);
fragmentID = generateRQLFieldAlias('_RelayQueryFragment0.Bar.{foo:"bar"}');
expect(fragment.getFragmentID()).toBe(fragmentID);
});
Expand All @@ -147,8 +147,11 @@ describe('RelayQueryFragment', () => {
var node = Relay.QL`fragment on Node{id}`;
var route = RelayMetaRoute.get('Foo');
var variables = {};
var fragment1 = RelayQuery.Node.create(node, route, variables);
var fragment2 = RelayQuery.Node.create(node, route, variables);

// Explicitly using `new` operator here in order to get a non-memoized
// fragment (`RelayQuery.Fragment.create` memoizes).
var fragment1 = new RelayQuery.Fragment(node, route, variables);
var fragment2 = new RelayQuery.Fragment(node, route, variables);
var fragmentID = generateRQLFieldAlias('_RelayQueryFragment0.Foo.{}');

expect(fragment1).not.toBe(fragment2);
Expand Down
33 changes: 19 additions & 14 deletions src/query/__tests__/toGraphQL-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
var RelayTestUtils = require('RelayTestUtils');
RelayTestUtils.unmockRelay();

var GraphQL = require('GraphQL');
var Relay = require('Relay');
var fromGraphQL = require('fromGraphQL');
var splitDeferredRelayQueries = require('splitDeferredRelayQueries');
Expand All @@ -28,9 +29,12 @@ describe('toGraphQL', function() {
jest.addMatchers({
toConvert(query) {
var expected = JSON.stringify(query.toJSON());
var actual = JSON.stringify(
this.actual(fromGraphQL.Node(query)).toJSON()
var node = this.actual(
query instanceof GraphQL.Query ?
fromGraphQL.Query(query) :
fromGraphQL.Fragment(query)
);
var actual = JSON.stringify(node);
var not = this.isNot ? 'not ' : ' ';
this.message = () =>
'Expected ' + expected + not +
Expand Down Expand Up @@ -173,31 +177,32 @@ describe('toGraphQL', function() {
}
}
`;
var relayQuery = fromGraphQL.Node(query);
var graphql = toGraphQL.Node(relayQuery);
var relayQuery = fromGraphQL.Query(query);
var graphql = toGraphQL.Query(relayQuery);
// GraphQL queries are static and must be traversed once to determine
// route-specific fragments and variable values
expect(fromGraphQL.Node(graphql).equals(relayQuery)).toBe(true);
expect(fromGraphQL.Query(graphql).equals(relayQuery)).toBe(true);
expect(graphql).not.toBe(query);
expect(toGraphQL.Node(relayQuery)).toBe(graphql);
expect(toGraphQL.Query(relayQuery)).toBe(graphql);
});

it('creates a new GraphQL-query if the relay query is a clone', () => {
var query = Relay.QL`
var fragment = Relay.QL`
fragment on StreetAddress {
city,
country,
}
`;
var relayQuery = fromGraphQL.Node(query);
var relayQueryChild = relayQuery.getChildren()[0];
var clonedRelayQuery = relayQuery.clone([relayQueryChild]);
var relayFragment = fromGraphQL.Fragment(fragment);
var relayFragmentChild = relayFragment.getChildren()[0];
var clonedRelayFragment = relayFragment.clone([relayFragmentChild]);

// GraphQL queries are static and must be traversed once to determine
// route-specific fragments and variable values
var graphql = toGraphQL.Node(clonedRelayQuery);
expect(fromGraphQL.Node(graphql).equals(clonedRelayQuery)).toBe(true);
expect(graphql).not.toBe(query);
expect(toGraphQL.Node(clonedRelayQuery)).toBe(graphql);
var graphql = toGraphQL.Fragment(clonedRelayFragment);
expect(fromGraphQL.Fragment(graphql).equals(clonedRelayFragment))
.toBe(true);
expect(graphql).not.toBe(fragment);
expect(toGraphQL.Fragment(clonedRelayFragment)).toBe(graphql);
});
});
Loading

0 comments on commit 5e77de5

Please sign in to comment.