-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
@inaccessible
elements when converting to API schema (#807)
This is an initial implementation of support for @inaccessible when converting a composed schema to an API schema. For now, it removes any fields, object types, or interface types with an @inaccessible directive. As part of this change, buildComposedSchema no longer returns an API schema. Instead, a separate toAPISchema function is used to convert the composed schema. The reason for this is that the gateway needs the complete schema for query planning, including elements that will be removed by @inaccessible. Co-authored-by: Trevor Scheer <trevor.scheer@gmail.com> Co-authored-by: Jesse Rosenberger <git@jro.cc>
- Loading branch information
1 parent
c3bd6dd
commit 623658b
Showing
18 changed files
with
802 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
federation-integration-testsuite-js/src/fixtures/special-cases/supergraphWithInaccessible.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { composeAndValidate } from '@apollo/federation'; | ||
import { assertCompositionSuccess } from '@apollo/federation/dist/composition/utils'; | ||
import { | ||
DirectiveDefinitionNode, | ||
SchemaDefinitionNode, | ||
DocumentNode, | ||
DirectiveNode, | ||
parse, | ||
visit, | ||
} from 'graphql'; | ||
import { fixtures } from '..'; | ||
|
||
const compositionResult = composeAndValidate(fixtures); | ||
assertCompositionSuccess(compositionResult); | ||
const parsed = parse(compositionResult.supergraphSdl); | ||
|
||
// We need to collect the AST for the inaccessible definition as well | ||
// as the @core and @inaccessible usages. Parsing SDL is a fairly | ||
// clean approach to this and easier to update than handwriting the AST. | ||
const [inaccessibleDefinition, schemaDefinition] = parse(`#graphql | ||
# inaccessibleDefinition | ||
directive @inaccessible on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ||
schema | ||
# inaccessibleCoreUsage | ||
@core(feature: "https://specs.apollo.dev/inaccessible/v0.1") | ||
# inaccessibleUsage | ||
@inaccessible { | ||
query: Query | ||
} | ||
`).definitions as [DirectiveDefinitionNode, SchemaDefinitionNode]; | ||
|
||
const [inaccessibleCoreUsage, inaccessibleUsage] = | ||
schemaDefinition.directives as [DirectiveNode, DirectiveNode]; | ||
|
||
// Append the AST with the inaccessible definition, | ||
// @core inaccessible usage, and @inaccessible usage on the `ssn` field | ||
const superGraphWithInaccessible: DocumentNode = visit(parsed, { | ||
Document(node) { | ||
return { | ||
...node, | ||
definitions: [...node.definitions, inaccessibleDefinition], | ||
}; | ||
}, | ||
SchemaDefinition(node) { | ||
return { | ||
...node, | ||
directives: [...(node.directives ?? []), inaccessibleCoreUsage], | ||
}; | ||
}, | ||
ObjectTypeDefinition(node) { | ||
return { | ||
...node, | ||
fields: | ||
node.fields?.map((field) => { | ||
if (field.name.value === 'ssn') { | ||
return { | ||
...field, | ||
directives: [...(field.directives ?? []), inaccessibleUsage], | ||
}; | ||
} | ||
return field; | ||
}) ?? [], | ||
}; | ||
}, | ||
}); | ||
|
||
export { superGraphWithInaccessible }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.