Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow @sourceField on fields of object types _and_ type extensions. #2925

Open
wants to merge 1 commit into
base: benjamn/fix-specs-module-load-order
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
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 @@
));
} 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