Skip to content

Commit eaf45d1

Browse files
authoredNov 4, 2020
fix(near-operation-file): fix issue with inline fragment without typeCondition (#5036)
1 parent 5d9a039 commit eaf45d1

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed
 

‎.changeset/wild-buttons-sparkle.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/near-operation-file-preset': patch
3+
'@graphql-codegen/plugin-helpers': patch
4+
---
5+
6+
fix issue with inline fragment without typeCondition

‎packages/presets/near-operation-file/tests/near-operation-file.spec.ts

+88
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,94 @@ describe('near-operation-file preset', () => {
7575
];
7676

7777
describe('Issues', () => {
78+
it('#5002 - error when inline fragment does not specify the name of the type', async () => {
79+
const testSchema = parse(/* GraphQL */ `
80+
scalar Date
81+
82+
schema {
83+
query: Query
84+
}
85+
86+
type Query {
87+
me: User!
88+
user(id: ID!): User
89+
allUsers: [User]
90+
search(term: String!): [SearchResult!]!
91+
myChats: [Chat!]!
92+
}
93+
94+
enum Role {
95+
USER
96+
ADMIN
97+
}
98+
99+
interface Node {
100+
id: ID!
101+
}
102+
103+
union SearchResult = User | Chat | ChatMessage
104+
105+
type User implements Node {
106+
id: ID!
107+
username: String!
108+
email: String!
109+
role: Role!
110+
}
111+
112+
type Chat implements Node {
113+
id: ID!
114+
users: [User!]!
115+
messages: [ChatMessage!]!
116+
}
117+
118+
type ChatMessage implements Node {
119+
id: ID!
120+
content: String!
121+
time: Date!
122+
user: User!
123+
}
124+
`);
125+
126+
const operations = [
127+
{
128+
location: 'test.graphql',
129+
document: parse(/* GraphQL */ `
130+
query chats {
131+
myChats {
132+
...ChatFields
133+
}
134+
}
135+
136+
fragment ChatFields on Chat {
137+
id
138+
... @include(if: true) {
139+
id
140+
messages {
141+
id
142+
}
143+
}
144+
}
145+
`),
146+
},
147+
];
148+
149+
expect(async () => {
150+
await preset.buildGeneratesSection({
151+
baseOutputDir: './src/',
152+
config: {},
153+
presetConfig: {
154+
folder: '__generated__',
155+
baseTypesPath: 'types.ts',
156+
},
157+
schema: testSchema,
158+
schemaAst: buildASTSchema(testSchema),
159+
documents: operations,
160+
plugins: [],
161+
pluginMap: {},
162+
});
163+
}).not.toThrow();
164+
});
165+
78166
it('#3066 - should respect higher level of fragments usage, and ignore fragments per input', async () => {
79167
const doTest = async (operationsStr: string, expected: string) => {
80168
const testSchema = buildSchema(/* GraphQL */ `

‎packages/utils/plugins-helpers/src/helpers.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@ export function isUsingTypes(document: DocumentNode, externalFragments: string[]
124124
} else if (parent.operation === 'subscription') {
125125
return schema.getSubscriptionType().name;
126126
}
127-
} else if (parent.kind === Kind.INLINE_FRAGMENT && parent.typeCondition) {
128-
return parent.typeCondition.name.value;
127+
} else if (parent.kind === Kind.INLINE_FRAGMENT) {
128+
if (parent.typeCondition) {
129+
return parent.typeCondition.name.value;
130+
} else {
131+
return typesStack[typesStack.length - 1].name;
132+
}
129133
}
130134

131135
return null;

0 commit comments

Comments
 (0)
Please sign in to comment.