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

fix: support opting out of the includeDeprecated argument #192

Merged
merged 5 commits into from
Apr 12, 2024
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
5 changes: 5 additions & 0 deletions .changeset/plenty-games-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gql.tada/internal": patch
---

Support opting out of `includeDeprecated` on `__Directive` and `__Field` in accordance with the October 2021 spec
47 changes: 44 additions & 3 deletions packages/internal/src/loaders/__tests__/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ describe('makeIntrospectSupportQuery', () => {
directive: __type(name: "__Directive") {
fields {
name
args {
name
}
}
}
field: __type(name: "__Field") {
fields {
name
args {
name
}
}
}
type: __type(name: "__Type") {
Expand All @@ -29,31 +40,56 @@ describe('makeIntrospectSupportQuery', () => {

describe('toSupportedFeatures', () => {
it('outputs default with no features enabled', () => {
expect(toSupportedFeatures({ type: null, inputValue: null, directive: null })).toEqual({
expect(
toSupportedFeatures({ type: null, inputValue: null, directive: null, field: null })
).toEqual({
directiveIsRepeatable: false,
specifiedByURL: false,
inputValueDeprecation: false,
directiveArgumentsIsDeprecated: false,
fieldArgumentsIsDeprecated: false,
});
});

it('detects `isRepeatable` support on directives', () => {
it('detects `isRepeatable` and `includeDeprectaed` support on directives', () => {
const input = {
type: null, // stubbed
inputValue: null, // stubbed
field: null, // stubbed
directive: {
fields: [{ name: 'isRepeatable' }],
fields: [
{ name: 'isRepeatable', args: [] },
{ name: 'args', args: [{ name: 'includeDeprecated' }] },
],
},
};

expect(toSupportedFeatures(input)).toMatchObject({
directiveIsRepeatable: true,
directiveArgumentsIsDeprecated: true,
});
});

it('detects `includeDeprectaed` support on fields', () => {
const input = {
type: null, // stubbed
inputValue: null, // stubbed
directive: null, // stubbed
field: {
fields: [{ name: 'args', args: [{ name: 'includeDeprecated' }] }],
},
};

expect(toSupportedFeatures(input)).toMatchObject({
fieldArgumentsIsDeprecated: true,
});
});

it('detects `specifiedByURL` support on scalars', () => {
const input = {
inputValue: null, // stubbed
directive: null, // stubbed
field: null, // stubbed
type: {
fields: [{ name: 'specifiedByURL' }],
},
Expand All @@ -68,6 +104,7 @@ describe('toSupportedFeatures', () => {
const input = {
type: null, // stubbed
directive: null, // stubbed
field: null, // stubbed
inputValue: {
fields: [{ name: 'isDeprecated' }],
},
Expand All @@ -85,6 +122,8 @@ describe('makeIntrospectionQuery', () => {
directiveIsRepeatable: true,
specifiedByURL: true,
inputValueDeprecation: true,
directiveArgumentsIsDeprecated: true,
fieldArgumentsIsDeprecated: true,
};

const output = print(makeIntrospectionQuery(support));
Expand Down Expand Up @@ -213,6 +252,8 @@ describe('makeIntrospectionQuery', () => {
directiveIsRepeatable: false,
specifiedByURL: false,
inputValueDeprecation: false,
directiveArgumentsIsDeprecated: false,
fieldArgumentsIsDeprecated: false,
};

const output = print(makeIntrospectionQuery(support));
Expand Down
64 changes: 55 additions & 9 deletions packages/internal/src/loaders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ export interface SupportedFeatures {
directiveIsRepeatable: boolean;
specifiedByURL: boolean;
inputValueDeprecation: boolean;
directiveArgumentsIsDeprecated: boolean;
fieldArgumentsIsDeprecated: boolean;
}

/** Data from a {@link makeIntrospectSupportQuery} result */
export interface IntrospectSupportQueryData {
directive: { fields: { name: string }[] | null } | null;
directive: { fields: { name: string; args: { name: string }[] | null }[] | null } | null;
type: { fields: { name: string }[] | null } | null;
field: { fields: { name: string; args: { name: string }[] | null }[] | null } | null;
inputValue: { fields: { name: string }[] | null } | null;
}

Expand All @@ -27,11 +30,24 @@ const _hasField = (
fieldName: string
): boolean => !!data && !!data.fields && data.fields.some((field) => field.name === fieldName);

const _supportsDeprecatedArgumentsArg = (
data: IntrospectSupportQueryData['field' | 'directive']
): boolean => {
const argsField = data && data.fields && data.fields.find((field) => field.name === 'args');
return !!(
argsField &&
argsField.args &&
argsField.args.find((arg) => arg.name === 'includeDeprecated')
);
};

/** Evaluates data from a {@link makeIntrospectSupportQuery} result to {@link SupportedFeatures} */
export const toSupportedFeatures = (data: IntrospectSupportQueryData): SupportedFeatures => ({
directiveIsRepeatable: _hasField(data.directive, 'isRepeatable'),
specifiedByURL: _hasField(data.type, 'specifiedByURL'),
inputValueDeprecation: _hasField(data.inputValue, 'isDeprecated'),
directiveArgumentsIsDeprecated: _supportsDeprecatedArgumentsArg(data.directive),
fieldArgumentsIsDeprecated: _supportsDeprecatedArgumentsArg(data.field),
});

let _introspectionQuery: DocumentNode | undefined;
Expand Down Expand Up @@ -92,7 +108,20 @@ export const makeIntrospectSupportQuery = (): DocumentNode => ({
value: { kind: Kind.STRING, value: '__Directive' },
},
],
selectionSet: _makeFieldNamesSelection(),
selectionSet: _makeFieldNamesSelection({ includeArgs: true }),
},
{
kind: Kind.FIELD,
alias: { kind: Kind.NAME, value: 'field' },
name: { kind: Kind.NAME, value: '__type' },
arguments: [
{
kind: Kind.ARGUMENT,
name: { kind: Kind.NAME, value: 'name' },
value: { kind: Kind.STRING, value: '__Field' },
},
],
selectionSet: _makeFieldNamesSelection({ includeArgs: true }),
},
{
kind: Kind.FIELD,
Expand All @@ -105,7 +134,7 @@ export const makeIntrospectSupportQuery = (): DocumentNode => ({
value: { kind: Kind.STRING, value: '__Type' },
},
],
selectionSet: _makeFieldNamesSelection(),
selectionSet: _makeFieldNamesSelection({ includeArgs: false }),
},
{
kind: Kind.FIELD,
Expand All @@ -118,15 +147,15 @@ export const makeIntrospectSupportQuery = (): DocumentNode => ({
value: { kind: Kind.STRING, value: '__InputValue' },
},
],
selectionSet: _makeFieldNamesSelection(),
selectionSet: _makeFieldNamesSelection({ includeArgs: false }),
},
],
},
} satisfies OperationDefinitionNode,
],
});

const _makeFieldNamesSelection = (): SelectionSetNode => ({
const _makeFieldNamesSelection = (options: { includeArgs: boolean }): SelectionSetNode => ({
kind: Kind.SELECTION_SET,
selections: [
{
Expand All @@ -139,6 +168,23 @@ const _makeFieldNamesSelection = (): SelectionSetNode => ({
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: 'name' },
},
...(options.includeArgs
? ([
{
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: 'args' },
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: 'name' },
},
],
},
},
] as const)
: []),
],
},
},
Expand Down Expand Up @@ -223,7 +269,7 @@ const _makeSchemaSelection = (support: SupportedFeatures): SelectionSetNode => (
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: 'locations' },
},
_makeSchemaArgsField(support),
_makeSchemaArgsField(support.directiveArgumentsIsDeprecated),
...(support.directiveIsRepeatable
? ([
{
Expand Down Expand Up @@ -294,7 +340,7 @@ const _makeSchemaFullTypeFragment = (support: SupportedFeatures): FragmentDefini
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: 'deprecationReason' },
},
_makeSchemaArgsField(support),
_makeSchemaArgsField(support.fieldArgumentsIsDeprecated),
{
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: 'type' },
Expand Down Expand Up @@ -396,10 +442,10 @@ const _makeSchemaFullTypeFragment = (support: SupportedFeatures): FragmentDefini
},
});

const _makeSchemaArgsField = (support: SupportedFeatures): FieldNode => ({
const _makeSchemaArgsField = (supportsValueDeprecation: boolean): FieldNode => ({
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: 'args' },
arguments: support.inputValueDeprecation
arguments: supportsValueDeprecation
? [
{
kind: Kind.ARGUMENT,
Expand Down
2 changes: 2 additions & 0 deletions packages/internal/src/loaders/sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const ALL_SUPPORTED_FEATURES: SupportedFeatures = {
directiveIsRepeatable: true,
specifiedByURL: true,
inputValueDeprecation: true,
directiveArgumentsIsDeprecated: true,
fieldArgumentsIsDeprecated: true,
};

export function loadFromSDL(config: LoadFromSDLConfig): SchemaLoader {
Expand Down
4 changes: 4 additions & 0 deletions packages/internal/src/loaders/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ const ALL_SUPPORTED_FEATURES: SupportedFeatures = {
directiveIsRepeatable: true,
specifiedByURL: true,
inputValueDeprecation: true,
directiveArgumentsIsDeprecated: true,
fieldArgumentsIsDeprecated: true,
};

const NO_SUPPORTED_FEATURES: SupportedFeatures = {
directiveIsRepeatable: false,
specifiedByURL: false,
inputValueDeprecation: false,
directiveArgumentsIsDeprecated: false,
fieldArgumentsIsDeprecated: false,
};

export function loadFromURL(config: LoadFromURLConfig): SchemaLoader {
Expand Down
Loading