Skip to content

Commit

Permalink
Add @oneOf support to introspection query (#4078)
Browse files Browse the repository at this point in the history
  • Loading branch information
maciesielka authored Jun 21, 2024
1 parent 9e2e751 commit 7df786e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/utilities/__tests__/buildClientSchema-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,21 @@ describe('Type System: build schema from introspection', () => {
expect(cycleIntrospection(sdl)).to.equal(sdl);
});

it('builds a schema with @oneOf directive', () => {
const sdl = dedent`
type Query {
someField(someArg: SomeInputObject): String
}
input SomeInputObject @oneOf {
someInputField1: String
someInputField2: String
}
`;

expect(cycleIntrospection(sdl)).to.equal(sdl);
});

it('can use client schema for limited execution', () => {
const schema = buildSchema(`
scalar CustomScalar
Expand Down
8 changes: 8 additions & 0 deletions src/utilities/__tests__/getIntrospectionQuery-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ describe('getIntrospectionQuery', () => {
);
});

it('include "isOneOf" field on input objects', () => {
expectIntrospectionQuery().toNotMatch('isOneOf');

expectIntrospectionQuery({ inputObjectOneOf: true }).toMatch('isOneOf', 1);

expectIntrospectionQuery({ inputObjectOneOf: false }).toNotMatch('isOneOf');
});

it('include deprecated input field and args', () => {
expectIntrospectionQuery().toMatch('includeDeprecated: true', 2);

Expand Down
1 change: 1 addition & 0 deletions src/utilities/buildClientSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ export function buildClientSchema(
name: inputObjectIntrospection.name,
description: inputObjectIntrospection.description,
fields: () => buildInputValueDefMap(inputObjectIntrospection.inputFields),
isOneOf: inputObjectIntrospection.isOneOf,
});
}

Expand Down
10 changes: 10 additions & 0 deletions src/utilities/getIntrospectionQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export interface IntrospectionOptions {
* Default: false
*/
inputValueDeprecation?: boolean;

/**
* Whether target GraphQL server supports `@oneOf` input objects.
* Default: false
*/
inputObjectOneOf?: boolean;
}

/**
Expand All @@ -45,6 +51,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
directiveIsRepeatable: false,
schemaDescription: false,
inputValueDeprecation: false,
inputObjectOneOf: false,
...options,
};

Expand All @@ -62,6 +69,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
function inputDeprecation(str: string) {
return optionsWithDefault.inputValueDeprecation ? str : '';
}
const inputObjectOneOf = optionsWithDefault.inputObjectOneOf ? 'isOneOf' : '';

return `
query IntrospectionQuery {
Expand Down Expand Up @@ -90,6 +98,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
name
${descriptions}
${specifiedByUrl}
${inputObjectOneOf}
fields(includeDeprecated: true) {
name
${descriptions}
Expand Down Expand Up @@ -259,6 +268,7 @@ export interface IntrospectionInputObjectType {
readonly name: string;
readonly description?: Maybe<string>;
readonly inputFields: ReadonlyArray<IntrospectionInputValue>;
readonly isOneOf: boolean;
}

export interface IntrospectionListTypeRef<
Expand Down
1 change: 1 addition & 0 deletions src/utilities/introspectionFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function introspectionFromSchema(
directiveIsRepeatable: true,
schemaDescription: true,
inputValueDeprecation: true,
inputObjectOneOf: true,
...options,
};

Expand Down

0 comments on commit 7df786e

Please sign in to comment.