Skip to content

Commit

Permalink
Improve heuristic to determine when it is necessary to fetch an id
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Jun 30, 2020
1 parent 284ae24 commit 94d642d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-weeks-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ts-gql/eslint-plugin": patch
---

Improve heuristic to determine when it is necessary to fetch an id
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": false
}
}
35 changes: 28 additions & 7 deletions packages/eslint-plugin/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function handleTemplateTag(
visit(
ast,
visitWithTypeInfo(typeInfo, {
SelectionSet(selectionSetNode) {
SelectionSet(selectionSetNode, key, parent) {
let type = typeInfo.getType();
if (!type) {
throw new Error(
Expand All @@ -155,7 +155,6 @@ export function handleTemplateTag(
type = type.ofType;
}
}

if (
!(
type instanceof GraphQLInterfaceType ||
Expand All @@ -178,14 +177,36 @@ export function handleTemplateTag(
"Location not found for selection. This is an internal error. If you see this, this is most likely a bug in ts-gql"
);
}
let selectionSetAlreadyHasId = selectionSetNode.selections.some(
(x) => x.kind === "Field" && x.name.value === "id"
);
if (selectionSetAlreadyHasId) {
return;
}
let isOnlyFragmentSpread = selectionSetNode.selections.every(
(x) => x.kind === "FragmentSpread"
);
if (isOnlyFragmentSpread) {
return;
}

if (
type.getFields()["id"] &&
!selectionSetNode.selections.some(
(x) => x.kind === "Field" && x.name.value === "id"
) &&
!selectionSetNode.selections.every((x) => x.kind !== "Field")
parent &&
!Array.isArray(parent) &&
(parent as any).kind === "InlineFragment"
) {
let parentType = (typeInfo as any)._parentTypeStack[
(typeInfo as any)._parentTypeStack.length - 2
];
if (
parentType instanceof GraphQLInterfaceType &&
parentType.getFields().id
) {
return;
}
}

if (type.getFields().id) {
let fixPosition =
selectionSetNode.loc?.start + node.quasi.range[0] + 2;
let fixText =
Expand Down
6 changes: 6 additions & 0 deletions test-app/pages/apollo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const someFragment = gql`
id
other
}
thing {
id
... on ImplementationOfThing {
something
}
}
}
` as import("../__generated__/ts-gql/Something2Apollo_x").type;

Expand Down
10 changes: 10 additions & 0 deletions test-app/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Query {
something: String
someObj: OutputThing!
arr: [OutputThing!]!
thing: Thing!
optional(thing: String): String!
oneMore(thing: String, other: Something!): String!
}
Expand All @@ -18,6 +19,15 @@ type Mutation {
oneMore(thing: String, other: Something!): String!
}

interface Thing {
id: ID!
}

type ImplementationOfThing implements Thing {
id: ID!
something: String!
}

type OutputThing {
id: ID!
other: String!
Expand Down

0 comments on commit 94d642d

Please sign in to comment.