it('should be able to alias a document containing a query and a named fragment', () => { const doc = gql` query authorStuff { author { ...authorDetails } } fragment authorDetails on Author { firstName lastName }`; const exp = gql` query authorStuff { ___authorStuff___requestIndex_2___fieldIndex_0: author { ...___authorStuff___requestIndex_2___authorDetails } } fragment ___authorStuff___requestIndex_2___authorDetails on Author { ___authorStuff___requestIndex_2___fieldIndex_1: firstName ___authorStuff___requestIndex_2___fieldIndex_2: lastName } `; const aliasName = getOperationDefinitionName(getQueryDefinition(doc), 2); const aliasedDoc = applyAliasNameToDocument(doc, aliasName); assert.equal(print(aliasedDoc), print(exp)); });
it('should be able to merge queries with inline fragments', () => { const query1 = gql` query nameOfQuery { ... on RootQuery { user } }`; const query2 = gql` query otherQuery { ... on RootQuery { author } }`; const exp = gql` query ___composed { ... on RootQuery { ___nameOfQuery___requestIndex_0___fieldIndex_0: user } ... on RootQuery { ___otherQuery___requestIndex_1___fieldIndex_0: author } }`; const mergedQuery = mergeQueryDocuments([query1, query2]); assert.equal(print(mergedQuery), print(exp)); });
it('should be able to add a query to a root query', () => { const doc = gql` query authorStuff { author { firstName lastName ...moreAuthorDetails } } fragment moreAuthorDetails on Author { address }`; const exp = gql` query ___composed { ___authorStuff___requestIndex_0___fieldIndex_0: author { firstName lastName ...___authorStuff___requestIndex_0___moreAuthorDetails } } fragment ___authorStuff___requestIndex_0___moreAuthorDetails on Author { ___authorStuff___requestIndex_0___fieldIndex_1: address } `; const mergedQuery = mergeQueryDocuments([doc]); assert.equal(print(mergedQuery), print(exp)); });
it('should stack multiple queries on an empty root query correctly', () => { const query1 = gql` query authorInfo { author { firstName lastName } }`; const query2 = gql` query personAddress { person { address } }`; const exp = gql` query ___composed { ___authorInfo___requestIndex_0___fieldIndex_0: author { firstName lastName } ___personAddress___requestIndex_1___fieldIndex_0: person { address } }`; const queries = [query1, query2]; const mergedQuery = mergeQueryDocuments(queries); assert.equal(print(mergedQuery), print(exp)); });
it('should also remove variables when removing empty selection sets', async () => { const query = parse(` query customerQuery($id: ID!, $limit: Int) { customerById(id: $id) { id name bookings(limit: $limit) { paid } } } `); const filteredQuery = filter.transformRequest({ document: query, variables: { id: 'c1', limit: 10 } }); const expected = parse(` query customerQuery($id: ID!) { customerById(id: $id) { id name } } `); expect(print(filteredQuery.document)).to.equal(print(expected)); });
it('should remove empty selection sets on wrapped objects (non-nullable/lists)', async () => { const query = parse(` query bookingQuery($id: ID!) { bookingById(id: $id) { id propertyId customer { favoriteFood } } } `); const filteredQuery = filter.transformRequest({ document: query, variables: { id: 'b1' } }); const expected = parse(` query bookingQuery($id: ID!) { bookingById(id: $id) { id propertyId } } `); expect(print(filteredQuery.document)).to.equal(print(expected)); });
it('should remove empty selection sets on objects', async () => { const query = parse(` query customerQuery($id: ID!) { customerById(id: $id) { id name address { planet } } } `); const filteredQuery = filter.transformRequest({ document: query, variables: { id: 'c1' } }); const expected = parse(` query customerQuery($id: ID!) { customerById(id: $id) { id name } } `); expect(print(filteredQuery.document)).to.equal(print(expected)); });
it('should be able to merge queries that have fragments with the same name', () => { const query1 = gql` query authorInfo { ...authorDetails } fragment authorDetails on Author { author { firstName lastName } }`; const query2 = gql` query authors { ...authorDetails } fragment authorDetails on Author { author }`; const exp = gql` query ___composed { ...___authorInfo___requestIndex_0___authorDetails ...___authors___requestIndex_1___authorDetails } fragment ___authorInfo___requestIndex_0___authorDetails on Author { ___authorInfo___requestIndex_0___fieldIndex_1: author { firstName lastName } } fragment ___authors___requestIndex_1___authorDetails on Author { ___authors___requestIndex_1___fieldIndex_1: author }`; const mergedQuery = mergeQueryDocuments([query1, query2]); assert.equal(print(mergedQuery), print(exp)); });
test("simple case", () => { const operation = createOperation({}, { query: gql` subscription Foo { commentAdded { id text } } `, }); operation.operationName = "Foo"; const result = subscriptionToQuery(introspectionResult, operation); expect(result).toBeDefined(); const query = result!["commentAdded"]; expect(query).toBeDefined(); const expected = gql` query Resolve_Foo_commentAdded ($id: ID!) { commentAdded: node(id: $id) { ... on Comment { id text } } } `; delete (query as any).loc; delete expected.loc; expect(print(query)).toEqual(print(expected)); expect(query).toEqual(expected); });
it('should correctly merge two queries that are the same other than variable values', () => { const query1 = gql` query authorStuff($id: Int) { author(id: $id) { name } }`; const query2 = gql` query authorStuff($id: Int) { author(id: $id) { name } }`; const expQuery = gql` query ___composed($___authorStuff___requestIndex_0___id: Int, $___authorStuff___requestIndex_1___id: Int) { ___authorStuff___requestIndex_0___fieldIndex_0: author(id: $___authorStuff___requestIndex_0___id) { name } ___authorStuff___requestIndex_1___fieldIndex_0: author(id: $___authorStuff___requestIndex_1___id) { name } }`; const mergedRequest = mergeRequests([{query: query1}, {query: query2}]); assert.equal(print(mergedRequest.query), print(expQuery)); });