diff --git a/src/operations/SnapshotEditor.ts b/src/operations/SnapshotEditor.ts index ee493800..42ec9350 100644 --- a/src/operations/SnapshotEditor.ts +++ b/src/operations/SnapshotEditor.ts @@ -225,7 +225,8 @@ export class SnapshotEditor { } if (this._context.addTypename && payloadName === '__typename') { - warnings.push(`Encountered undefined payload value for __typename which will override __typename value on existing fragment.`); + const message = `Encountered undefined payload value for __typename which will override __typename value on existing fragment`; + throw new InvalidPayloadError(message, prefixPath, containerId, path, payload); } } diff --git a/test/unit/Apollo/writeFragment/errorMissingTypename.ts b/test/unit/Apollo/writeFragment/errorMissingTypename.ts new file mode 100644 index 00000000..e7c02bc8 --- /dev/null +++ b/test/unit/Apollo/writeFragment/errorMissingTypename.ts @@ -0,0 +1,37 @@ +import gql from 'graphql-tag'; + +import { Hermes } from '../../../../src/apollo/Hermes'; +import { strictConfig } from '../../../helpers/context'; +import { StaticNodeId } from '../../../../src/schema'; + +const { QueryRoot: QueryRootId } = StaticNodeId; + +describe(`writeFragment with missing __typename`, () => { + + let hermes: Hermes; + beforeAll(() => { + hermes = new Hermes({ + ...strictConfig, + addTypename: true, + }); + }); + + it(`throws an error`, () => { + expect(() => { + hermes.writeFragment({ + id: QueryRootId, + fragment: gql(` + fragment viewer on Viewer { + id + name + } + `), + data: { + id: 123, + name: 'Gouda', + }, + }); + }).to.throw(/InvalidPayloadError: Encountered undefined payload value for __typename/i); + }); + +});