Skip to content

Commit

Permalink
Allow @sourceField on fields of object types _and_ type extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Feb 1, 2024
1 parent 555d60d commit 693ff67
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
43 changes: 43 additions & 0 deletions composition-js/src/__tests__/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5206,5 +5206,48 @@ describe('@source* directives', () => {
'[renamed] @api(name: "not an identifier") must specify name using only [a-zA-Z0-9-_] characters'
);
});

const extendTypeQuerySchema = gql`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key"])
@link(
url: "https://specs.apollo.dev/source/v0.1"
import: ["@sourceAPI", "@sourceField"]
for: EXECUTION
)
@sourceAPI(
name: "json"
http: { baseURL: "https://jsonplaceholder.typicode.com/" }
)
type Query {
users: [User]
@sourceField(api: "json", http: { GET: "/users" }, selection: "id name")
}
# This test is a regression test for a spurious validation error triggered
# by using 'extend type ' here instead of just 'type ' (as above).
extend type Query {
user(id: ID!): User
@sourceField(
api: "json"
http: { GET: "/users/{id}" }
selection: "id name"
)
}
type User {
id: ID!
name: String
}
`;

it('allows @sourceField on extended Query type', () => {
const result = composeServices([{
name: 'extendTypeQuery',
typeDefs: extendTypeQuerySchema,
}]);
expect(result.errors ?? []).toEqual([]);
});
});
});
6 changes: 5 additions & 1 deletion internals-js/src/specs/sourceSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ export class SourceSpecDefinition extends FeatureDefinition {
));
} else {
const typeGrandparent = fieldParent.parent as SchemaElement<any, any>;
if (typeGrandparent.sourceAST?.kind !== Kind.OBJECT_TYPE_DEFINITION) {
const typeKind = typeGrandparent.sourceAST?.kind;
if (
typeKind !== Kind.OBJECT_TYPE_DEFINITION &&
typeKind !== Kind.OBJECT_TYPE_EXTENSION

Check warning on line 489 in internals-js/src/specs/sourceSpec.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/specs/sourceSpec.ts#L489

Added line #L489 was not covered by tests
) {
errors.push(ERRORS.SOURCE_FIELD_NOT_ON_ROOT_OR_ENTITY_FIELD.err(
`${sourceField} must be applied to field of object type`,
{ nodes: application.sourceAST },
Expand Down

0 comments on commit 693ff67

Please sign in to comment.