Skip to content

Commit

Permalink
Add __typename to root operations, too.
Browse files Browse the repository at this point in the history
As @jbaxleyiii pointed out in this comment, the root query and mutation
types do not necessarily have to be called "Query" or "Mutation", and the
only way to find their real names is to ask for the __typename property:
#5146 (comment)
  • Loading branch information
benjamn committed Aug 9, 2019
1 parent d53f118 commit 4377e02
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('fragment matching', () => {
`;

const data = {
__typename: 'Query',
animals: [
{
__typename: 'Cat',
Expand Down Expand Up @@ -73,6 +74,7 @@ describe('fragment matching', () => {
`;

const data = {
__typename: 'Query',
bestFriend: {
__typename: 'Dog',
id: 2,
Expand Down Expand Up @@ -109,6 +111,7 @@ describe('fragment matching', () => {
`;

const data = {
__typename: 'Query',
testResults: [
{
__typename: 'TestResult',
Expand Down Expand Up @@ -160,6 +163,7 @@ describe('fragment matching', () => {
`;

const data = {
__typename: 'Query',
animals: [
{
__typename: 'Sphynx',
Expand Down Expand Up @@ -204,6 +208,7 @@ describe('fragment matching', () => {
`;

const data = {
__typename: 'Query',
people: [
{
__typename: 'Person',
Expand Down
8 changes: 8 additions & 0 deletions packages/apollo-utilities/src/__tests__/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ describe('query transforms', () => {
}
__typename
}
__typename
}
`;
const expectedQueryStr = print(expectedQuery);
Expand All @@ -564,6 +565,7 @@ describe('query transforms', () => {
it('should not add duplicates', () => {
let testQuery = gql`
query {
__typename
author {
name {
firstName
Expand All @@ -577,6 +579,7 @@ describe('query transforms', () => {

const expectedQuery = gql`
query {
__typename
author {
name {
firstName
Expand Down Expand Up @@ -611,6 +614,7 @@ describe('query transforms', () => {
}
__typename
}
__typename
}
`);
const modifiedQuery = addTypenameToDocument(testQuery);
Expand Down Expand Up @@ -644,6 +648,7 @@ describe('query transforms', () => {
}
__typename
}
__typename
}
fragment friendFields on User {
Expand Down Expand Up @@ -673,6 +678,7 @@ describe('query transforms', () => {
lastName
__typename
}
__typename
}
`);

Expand All @@ -696,6 +702,7 @@ describe('query transforms', () => {
lastName
__typename
}
__typename
}
`;

Expand Down Expand Up @@ -768,6 +775,7 @@ describe('query transforms', () => {
}
__typename
}
__typename
}
`);
const modifiedQuery = addTypenameToDocument(testQuery);
Expand Down
8 changes: 0 additions & 8 deletions packages/apollo-utilities/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,6 @@ export function addTypenameToDocument(doc: DocumentNode): DocumentNode {
return visit(checkDocument(doc), {
SelectionSet: {
enter(node, _key, parent) {
// Don't add __typename to OperationDefinitions.
if (
parent &&
(parent as OperationDefinitionNode).kind === 'OperationDefinition'
) {
return;
}

// No changes if no selections.
const { selections } = node;
if (!selections) {
Expand Down

0 comments on commit 4377e02

Please sign in to comment.