From 3b942a9f48c2e17feaab95bfd07c5669b6db7b7d Mon Sep 17 00:00:00 2001 From: Ciaran Liedeman Date: Sat, 20 Oct 2018 12:55:15 +0200 Subject: [PATCH] feat: Implemented alias --- .../__snapshots__/integration-test.ts.snap | 36 +++++++++++++++ src/__tests__/integration-test.ts | 44 +++++++++++++++++++ src/index.ts | 14 +++++- 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src/__tests__/__snapshots__/integration-test.ts.snap b/src/__tests__/__snapshots__/integration-test.ts.snap index c5a2444b..544ef11b 100644 --- a/src/__tests__/__snapshots__/integration-test.ts.snap +++ b/src/__tests__/__snapshots__/integration-test.ts.snap @@ -1,5 +1,41 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`integration with apollo-server alias with fragment works 1`] = ` +request:1 + finished: true + logs: + 1. {"queryString":"\\n fragment F on A {\\n dos: two\\n }\\n\\n query {\\n a {\\n ...F\\n }\\n }"} ++-- a:2 + finished: true + logs: + 1. {"result":"{one:1,two:2,three:[{four:4},{four:IV}]}"} + +-- dos:3 + finished: true + logs: + 1. {"result":"2"} + +`; + +exports[`integration with apollo-server alias works 1`] = ` +request:1 + finished: true + logs: + 1. {"queryString":"query {\\n a {\\n uno: one\\n two\\n }\\n }"} ++-- a:2 + finished: true + logs: + 1. {"result":"{one:1,two:2,three:[{four:4},{four:IV}]}"} + +-- uno:3 + finished: true + logs: + 1. {"result":"1"} + +-- two:4 + finished: true + logs: + 1. {"result":"2"} + +`; + exports[`integration with apollo-server correct span nesting 1`] = ` request:1 finished: true diff --git a/src/__tests__/integration-test.ts b/src/__tests__/integration-test.ts index 69a16143..27471fb4 100644 --- a/src/__tests__/integration-test.ts +++ b/src/__tests__/integration-test.ts @@ -266,4 +266,48 @@ describe("integration with apollo-server", () => { const tree = buildSpanTree(tracer.spans); expect(tree).toMatchSnapshot(); }); + + it("alias works", async () => { + const tracer = new MockTracer(); + const app = createApp({ tracer }); + await request(app) + .post("/graphql") + .set("Accept", "application/json") + .send({ + query: `query { + a { + uno: one + two + } + }` + }) + .expect(200); + + const tree = buildSpanTree(tracer.spans); + expect(tree).toMatchSnapshot(); + }); + + it("alias with fragment works", async () => { + const tracer = new MockTracer(); + const app = createApp({ tracer }); + await request(app) + .post("/graphql") + .set("Accept", "application/json") + .send({ + query: ` + fragment F on A { + dos: two + } + + query { + a { + ...F + } + }` + }) + .expect(200); + + const tree = buildSpanTree(tracer.spans); + expect(tree).toMatchSnapshot(); + }); }); diff --git a/src/index.ts b/src/index.ts index 4546ab0d..a291b962 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,6 +32,18 @@ interface RequestStart { persistedQueryRegister?: boolean; } +function getFieldName(info: GraphQLResolveInfo) { + if ( + info.fieldNodes && + info.fieldNodes.length > 0 && + info.fieldNodes[0].alias + ) { + return info.fieldNodes[0].alias.value; + } + + return info.fieldName || "field"; +} + export default class OpentracingExtension implements GraphQLExtension { private serverTracer: Tracer; @@ -117,7 +129,7 @@ export default class OpentracingExtension // idempotent method to add helpers to the first context available (which will be propagated by apollo) addContextHelpers(context); - const name = info.fieldName || "field"; + const name = getFieldName(info); const parentSpan = info.path && info.path.prev ? context.getSpanByPath(info.path.prev)