Skip to content

Commit

Permalink
feat: Implemented alias
Browse files Browse the repository at this point in the history
  • Loading branch information
cliedeman authored and DanielMSchmidt committed Oct 20, 2018
1 parent d0bacea commit 3b942a9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/__tests__/__snapshots__/integration-test.ts.snap
Original file line number Diff line number Diff line change
@@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/__tests__/integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TContext extends SpanContext>
implements GraphQLExtension<TContext> {
private serverTracer: Tracer;
Expand Down Expand Up @@ -117,7 +129,7 @@ export default class OpentracingExtension<TContext extends SpanContext>
// 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)
Expand Down

0 comments on commit 3b942a9

Please sign in to comment.