@ardatan/relay-compiler
Version:
A compiler tool for building GraphQL-driven applications.
46 lines (36 loc) • 1.08 kB
Flow
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
// flowlint ambiguous-object-type:error
;
const IRValidator = require('../core/IRValidator');
const {createUserError} = require('../core/CompilerError');
import type CompilerContext from '../core/CompilerContext';
import type {Root} from '../core/IR';
function visitRoot(node: Root) {
for (const selection of node.selections) {
if (selection.kind === 'ScalarField' && selection.name === '__typename') {
throw createUserError(
'Relay does not allow `__typename` field on Query, Mutation or Subscription',
[selection.loc],
);
}
}
}
function stopVisit() {}
function disallowTypenameOnRoot(context: CompilerContext): CompilerContext {
IRValidator.validate(context, {
Root: visitRoot,
Fragment: stopVisit,
});
return context;
}
module.exports = {
transform: disallowTypenameOnRoot,
};