Beispiel #1
0
  it('returns ref params', () => {
    // non-ref query:
    expect(me.getBatchCall()).toBe(null);

    // ref query:
    var root = getNode({
      ...Relay.QL`
        query {
          node(id: "123") {
            id
          }
        }
      `,
      calls: [QueryBuilder.createCall(
        'id',
        QueryBuilder.createBatchCallVariable('q0', '$.*.actor.id')
      )],
    });
    var batchCall = root.getBatchCall();
    expect(batchCall).toEqual({
      refParamName: 'ref_q0',
      sourceQueryID: 'q0',
      sourceQueryPath: '$.*.actor.id',
    });
  });
Beispiel #2
0
 it('equals identical ref queries with matching ref params', () => {
   var node = getNode({
     ...Relay.QL`query { node(id: "123") }`,
     calls: [QueryBuilder.createCall(
       'id',
       QueryBuilder.createBatchCallVariable('q0', '$.*.actor.id')
     )],
   });
   var other = getNode({
     ...Relay.QL`query { node(id: "123") }`,
     calls: [QueryBuilder.createCall(
       'id',
       QueryBuilder.createBatchCallVariable('q0', '$.*.actor.id')
     )],
   });
   expect(node.equals(other)).toBe(true);
 });
/**
 * Wraps `descriptor` in a new top-level ref query.
 */
function createRefQuery(
  descriptor: RelayRefQueryDescriptor,
  context: RelayQuery.Root
): RelayQuery.Root {
  const node = descriptor.node;
  invariant(
    node instanceof RelayQuery.Field ||
    node instanceof RelayQuery.Fragment,
    'splitDeferredRelayQueries(): Ref query requires a field or fragment.'
  );

  // Build up JSONPath.
  const jsonPath = ['$', '*'];
  let parent;
  for (let ii = 0; ii < descriptor.nodePath.length; ii++) {
    parent = descriptor.nodePath[ii];
    if (parent instanceof RelayQuery.Field) {
      jsonPath.push(parent.getSerializationKey());
      if (parent.isPlural()) {
        jsonPath.push('*');
      }
    }
  }
  invariant(
    jsonPath.length > 2,
    'splitDeferredRelayQueries(): Ref query requires a complete path.'
  );
  const field: RelayQuery.Field = (parent: any); // Flow
  const primaryKey = field.getInferredPrimaryKey();
  invariant(
    primaryKey,
    'splitDeferredRelayQueries(): Ref query requires a primary key.'
  );
  jsonPath.push(primaryKey);

  // Create the wrapper root query.
  const root = RelayQuery.Root.build(
    context.getName(),
    RelayNodeInterface.NODES,
    QueryBuilder.createBatchCallVariable(context.getID(), jsonPath.join('.')),
    [node],
    {
      identifyingArgName: RelayNodeInterface.ID,
      identifyingArgType: RelayNodeInterface.ID_TYPE,
      isAbstract: true,
      isDeferred: true,
      isPlural: false,
    },
    RelayNodeInterface.NODE_TYPE
  );

  const result: RelayQuery.Root = (root: any); // Flow
  return result;
}
Beispiel #4
0
 it('does not equal queries with different ref params', () => {
   var node = getNode({
     ...Relay.QL`query { node(id: "123") }`,
     calls: [QueryBuilder.createCall(
       'id',
       QueryBuilder.createBatchCallVariable('q0', '$.*.actor.id')
     )],
   });
   var other = getNode({
     ...Relay.QL`query { node(id: "123") }`,
     calls: [QueryBuilder.createCall(
       'id',
       QueryBuilder.createBatchCallVariable(
         'q0',
         '$.*.actor.current_city.id'
       )
     )],
   });
   expect(node.equals(other)).toBe(false);
 });
Beispiel #5
0
 it('creates roots with batch calls', () => {
   const root = RelayQuery.Root.build(
     'RelayQueryTest',
     'node',
     QueryBuilder.createBatchCallVariable('q0', '$.*.id'),
     []
   );
   expect(root instanceof RelayQuery.Root).toBe(true);
   expect(root.getBatchCall()).toEqual({
     refParamName: 'ref_q0',
     sourceQueryID: 'q0',
     sourceQueryPath: '$.*.id',
   });
 });
Beispiel #6
0
    const match = name.match(/^ref_(q\d+)$/);
    invariant(
      match,
      'getRefNode(): Expected call variable of the form `<ref_q\\d+>`.'
    );
    // e.g. `q0`
    const id = match[1];
    // e.g. `{ref_q0: '<ref_q0>'}`
    const variables = {[name]: '<' + callValue.callVariableName + '>'};

    return RelayQuery.Root.create(
      {
        ...node,
        calls: [QueryBuilder.createCall(
          'id',
          QueryBuilder.createBatchCallVariable(id, refParam.path)
        )],
        isDeferred: true,
      },
      RelayMetaRoute.get('$RelayTestUtils'),
      variables
    );
  },

  getVerbatimNode(node, variables) {
    return RelayTestUtils.filterGeneratedFields(
      RelayTestUtils.getNode(node, variables)
    );
  },

  filterGeneratedFields(query) {
Beispiel #7
0
/**
 * @internal
 *
 * Converts a RelayQuery.Node into a plain object representation. This is
 * equivalent to the AST produced by `babel-relay-plugin` and is intended for
 * use in serializing RelayQuery nodes.
 *
 * NOTE: This is used by external open source projects.
 */
const toGraphQL = {
  Query(node: RelayQuery.Root): ConcreteQuery {
    const batchCall = node.getBatchCall();
    let identifyingArgValue;
    if (batchCall) {
      identifyingArgValue = QueryBuilder.createBatchCallVariable(
        batchCall.sourceQueryID,
        batchCall.sourceQueryPath
      );
    } else {
      const identifyingArg = node.getIdentifyingArg();
      if (identifyingArg) {
        if (Array.isArray(identifyingArg.value)) {
          identifyingArgValue = identifyingArg.value.map(
            QueryBuilder.createCallValue
          );
        } else {
          identifyingArgValue = QueryBuilder.createCallValue(
            identifyingArg.value
          );
        }
      }
    }