Skip to content

Commit

Permalink
Add @oneOf support to introspection query
Browse files Browse the repository at this point in the history
  • Loading branch information
maciesielka committed May 2, 2024
1 parent d811c97 commit 9bae8e8
Show file tree
Hide file tree
Showing 5 changed files with 34 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
9 changes: 9 additions & 0 deletions src/utilities/getIntrospectionQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export interface IntrospectionOptions {
* Default: false
*/
inputValueDeprecation?: boolean;

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

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

Expand All @@ -62,6 +67,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 +96,7 @@ export function getIntrospectionQuery(options?: IntrospectionOptions): string {
name
${descriptions}
${specifiedByUrl}
${inputObjectOneOf}
fields(includeDeprecated: true) {
name
${descriptions}
Expand Down Expand Up @@ -223,6 +230,7 @@ export interface IntrospectionObjectType {
readonly interfaces: ReadonlyArray<
IntrospectionNamedTypeRef<IntrospectionInterfaceType>
>;
readonly isOneOf: boolean;
}

export interface IntrospectionInterfaceType {
Expand Down Expand Up @@ -259,6 +267,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 9bae8e8

Please sign in to comment.