Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 237 additions & 0 deletions composition-js/src/__tests__/connectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,243 @@ describe("connect spec and join__directive", () => {
}
});

it("does not require importing @connect", () => {
const subgraphs = [
{
name: "with-connectors",
typeDefs: parse(`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.10"
import: ["@key"]
)
@link(
url: "https://specs.apollo.dev/connect/v0.1"
import: ["@source"]
)
@source(name: "v1", http: { baseURL: "http://v1" })

type Query {
resources: [Resource!]!
@connect(source: "v1", http: { GET: "/resources" }, selection: "")
}

type Resource @key(fields: "id") {
id: ID!
name: String!
}
`),
},
];

const result = composeServices(subgraphs);
expect(result.errors ?? []).toEqual([]);
const printed = printSchema(result.schema!);
expect(printed).toMatchInlineSnapshot(`
"schema
@link(url: \\"https://specs.apollo.dev/link/v1.0\\")
@link(url: \\"https://specs.apollo.dev/join/v0.5\\", for: EXECUTION)
@link(url: \\"https://specs.apollo.dev/connect/v0.1\\", for: EXECUTION)
@join__directive(graphs: [WITH_CONNECTORS], name: \\"link\\", args: {url: \\"https://specs.apollo.dev/connect/v0.1\\", import: [\\"@source\\"]})
@join__directive(graphs: [WITH_CONNECTORS], name: \\"source\\", args: {name: \\"v1\\", http: {baseURL: \\"http://v1\\"}})
{
query: Query
}

directive @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA

directive @join__graph(name: String!, url: String!) on ENUM_VALUE

directive @join__type(graph: join__Graph!, key: join__FieldSet, extension: Boolean! = false, resolvable: Boolean! = true, isInterfaceObject: Boolean! = false) repeatable on OBJECT | INTERFACE | UNION | ENUM | INPUT_OBJECT | SCALAR

directive @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet, type: String, external: Boolean, override: String, usedOverridden: Boolean, overrideLabel: String, contextArguments: [join__ContextArgument!]) repeatable on FIELD_DEFINITION | INPUT_FIELD_DEFINITION

directive @join__implements(graph: join__Graph!, interface: String!) repeatable on OBJECT | INTERFACE

directive @join__unionMember(graph: join__Graph!, member: String!) repeatable on UNION

directive @join__enumValue(graph: join__Graph!) repeatable on ENUM_VALUE

directive @join__directive(graphs: [join__Graph!], name: String!, args: join__DirectiveArguments) repeatable on SCHEMA | OBJECT | INTERFACE | FIELD_DEFINITION

enum link__Purpose {
\\"\\"\\"
\`SECURITY\` features provide metadata necessary to securely resolve fields.
\\"\\"\\"
SECURITY

\\"\\"\\"
\`EXECUTION\` features provide metadata necessary for operation execution.
\\"\\"\\"
EXECUTION
}

scalar link__Import

enum join__Graph {
WITH_CONNECTORS @join__graph(name: \\"with-connectors\\", url: \\"\\")
}

scalar join__FieldSet

scalar join__DirectiveArguments

scalar join__FieldValue

input join__ContextArgument {
name: String!
type: String!
context: String!
selection: join__FieldValue!
}

type Query
@join__type(graph: WITH_CONNECTORS)
{
resources: [Resource!]! @join__directive(graphs: [WITH_CONNECTORS], name: \\"connect\\", args: {source: \\"v1\\", http: {GET: \\"/resources\\"}, selection: \\"\\"})
}

type Resource
@join__type(graph: WITH_CONNECTORS, key: \\"id\\")
{
id: ID!
name: String!
}"
`);

if (result.schema) {
expect(printSchema(result.schema.toAPISchema())).toMatchInlineSnapshot(`
"type Query {
resources: [Resource!]!
}

type Resource {
id: ID!
name: String!
}"
`);
}
});

it("using as:", () => {
const subgraphs = [
{
name: "with-connectors",
typeDefs: parse(`
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.10"
import: ["@key"]
)
@link(
url: "https://specs.apollo.dev/connect/v0.1"
as: "http"
import: ["@source"]
)
@source(name: "v1", http: { baseURL: "http://v1" })

type Query {
resources: [Resource!]!
@http(source: "v1", http: { GET: "/resources" }, selection: "")
}

type Resource @key(fields: "id") {
id: ID!
name: String!
}
`),
},
];

const result = composeServices(subgraphs);
expect(result.errors ?? []).toEqual([]);
const printed = printSchema(result.schema!);
expect(printed).toMatchInlineSnapshot(`
"schema
@link(url: \\"https://specs.apollo.dev/link/v1.0\\")
@link(url: \\"https://specs.apollo.dev/join/v0.5\\", for: EXECUTION)
@link(url: \\"https://specs.apollo.dev/connect/v0.1\\", for: EXECUTION)
@join__directive(graphs: [WITH_CONNECTORS], name: \\"link\\", args: {url: \\"https://specs.apollo.dev/connect/v0.1\\", as: \\"http\\", import: [\\"@source\\"]})
@join__directive(graphs: [WITH_CONNECTORS], name: \\"source\\", args: {name: \\"v1\\", http: {baseURL: \\"http://v1\\"}})
{
query: Query
}

directive @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA

directive @join__graph(name: String!, url: String!) on ENUM_VALUE

directive @join__type(graph: join__Graph!, key: join__FieldSet, extension: Boolean! = false, resolvable: Boolean! = true, isInterfaceObject: Boolean! = false) repeatable on OBJECT | INTERFACE | UNION | ENUM | INPUT_OBJECT | SCALAR

directive @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet, type: String, external: Boolean, override: String, usedOverridden: Boolean, overrideLabel: String, contextArguments: [join__ContextArgument!]) repeatable on FIELD_DEFINITION | INPUT_FIELD_DEFINITION

directive @join__implements(graph: join__Graph!, interface: String!) repeatable on OBJECT | INTERFACE

directive @join__unionMember(graph: join__Graph!, member: String!) repeatable on UNION

directive @join__enumValue(graph: join__Graph!) repeatable on ENUM_VALUE

directive @join__directive(graphs: [join__Graph!], name: String!, args: join__DirectiveArguments) repeatable on SCHEMA | OBJECT | INTERFACE | FIELD_DEFINITION

enum link__Purpose {
\\"\\"\\"
\`SECURITY\` features provide metadata necessary to securely resolve fields.
\\"\\"\\"
SECURITY

\\"\\"\\"
\`EXECUTION\` features provide metadata necessary for operation execution.
\\"\\"\\"
EXECUTION
}

scalar link__Import

enum join__Graph {
WITH_CONNECTORS @join__graph(name: \\"with-connectors\\", url: \\"\\")
}

scalar join__FieldSet

scalar join__DirectiveArguments

scalar join__FieldValue

input join__ContextArgument {
name: String!
type: String!
context: String!
selection: join__FieldValue!
}

type Query
@join__type(graph: WITH_CONNECTORS)
{
resources: [Resource!]! @join__directive(graphs: [WITH_CONNECTORS], name: \\"http\\", args: {source: \\"v1\\", http: {GET: \\"/resources\\"}, selection: \\"\\"})
}

type Resource
@join__type(graph: WITH_CONNECTORS, key: \\"id\\")
{
id: ID!
name: String!
}"
`);

if (result.schema) {
expect(printSchema(result.schema.toAPISchema())).toMatchInlineSnapshot(`
"type Query {
resources: [Resource!]!
}

type Resource {
id: ID!
name: String!
}"
`);
}
});

it("composes v0.2", () => {
const subgraphs = [
{
Expand Down
19 changes: 12 additions & 7 deletions composition-js/src/merging/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@
if (!source || !isInterfaceType(source)) {
continue;
}
const sourceMetadata = this.subgraphs.values()[idx].metadata();

Check warning on line 2377 in composition-js/src/merging/merge.ts

View check run for this annotation

Apollo SecOps / Static App Security Check

rules.providers.gitlab.security.eslint.detect-object-injection

Bracket object notation with user input is present, this might allow an attacker to access all properties of the object and even it's prototype, leading to possible code execution.
const keys = source.appliedDirectivesOf(sourceMetadata.keyDirective());
hasKey ||= keys.length > 0;
const resolvableKey = keys.find((k) => k.arguments().resolvable !== false);
Expand Down Expand Up @@ -3118,14 +3118,19 @@
// identity url in a Map, reachable from all its imported names.
const map = new Map<string, FeatureUrl>();
for (const linkDirective of schema.schemaDefinition.appliedDirectivesOf<LinkDirectiveArgs>('link')) {
const { url, import: imports } = linkDirective.arguments();
const { url, as, import: imports } = linkDirective.arguments();
const parsedUrl = FeatureUrl.maybeParse(url);
if (parsedUrl && imports) {
for (const i of imports) {
if (typeof i === 'string') {
map.set(i, parsedUrl);
} else {
map.set(i.as ?? i.name, parsedUrl);

if (parsedUrl) {
// always add the main directive to the map, regardless of whether it is imported
map.set(`@${as ?? parsedUrl.name}`, parsedUrl);
if (imports) {
for (const i of imports) {
if (typeof i === 'string') {
map.set(i, parsedUrl);
} else {
map.set(i.as ?? i.name, parsedUrl);
}
}
}
}
Expand Down