Skip to content
Open
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
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
) {
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