Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- `apollo-language-server`
- <First `apollo-language-server` related entry goes here>
- Fix issue where fragment definitions only included in `@client` fields would not be stripped ((AP-682)(https://golinks.io/AP-682), [#1454](https://github.com/apollographql/apollo-tooling/pull/1454))
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-language-server/src/project/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export class GraphQLClientProject extends GraphQLProject {
for (const operationName in current) {
const document = current[operationName];

let serviceOnly: DocumentNode = removeDirectiveAnnotatedFields(
let serviceOnly = removeDirectiveAnnotatedFields(
Comment thread
justinanastos marked this conversation as resolved.
removeDirectives(document, clientOnlyDirectives as string[]),
clientSchemaDirectives as string[]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import gql from "graphql-tag";
import { print } from "graphql";
import { withTypenameFieldAddedWhereNeeded } from "../graphql";
import { parse, print } from "graphql";
import {
withTypenameFieldAddedWhereNeeded,
removeDirectiveAnnotatedFields
} from "../graphql";

describe("withTypenameFieldAddedWhereNeeded", () => {
it("properly adds __typename to each selectionSet", () => {
Expand Down Expand Up @@ -73,3 +76,336 @@ describe("withTypenameFieldAddedWhereNeeded", () => {
`);
});
});

describe("removeDirectiveAnnotatedFields", () => {
it("should remove fields with matching directives", () => {
expect(
print(
removeDirectiveAnnotatedFields(
parse(`query Query { fieldToKeep fieldToRemove @client }`),
["client"]
)
)
).toMatchInlineSnapshot(`
"query Query {
fieldToKeep
}
"
`);
});

it("trim selections sets that are client only", () => {
expect(
print(
removeDirectiveAnnotatedFields(
parse(`
query Query {
fieldToKeep
fieldToRemove @client {
childField
}
}
`),
["client"]
)
)
).toMatchInlineSnapshot(`
"query Query {
fieldToKeep
}
"
`);
});

it("should remove fragments when a directive is used on a fragment spread", () => {
expect(
print(
removeDirectiveAnnotatedFields(
parse(`
{
me { name }
...ClientFields @client
}
fragment ClientFields on Query {
hello
}
`),
["client"]
)
)
).toMatchInlineSnapshot(`
"{
me {
name
}
}
"
`);
});

it("should remove fragments when client directive is used inline", () => {
expect(
print(
removeDirectiveAnnotatedFields(
parse(`
{
me { name }
... on Query @client {
hello
}
}
`),
["client"]
)
)
).toMatchInlineSnapshot(`
"{
me {
name
}
}
"
`);
});

it("should remove fragments when the client directive is on the definition", () => {
expect(
print(
removeDirectiveAnnotatedFields(
parse(`
fragment ClientObject on Query @client {
hello
}
{
me { name }
... ClientObject
}
`),
["client"]
)
)
).toMatchInlineSnapshot(`
"{
me {
name
}
}
"
`);
});

it("should remove fragments that become unused when antecendant directives are removed", () => {
Comment thread
justinanastos marked this conversation as resolved.
expect(
print(
removeDirectiveAnnotatedFields(
parse(`
fragment ClientObjectFragment on ClientObject {
string
number
}

fragment LaunchTile on Launch {
__typename
id
isBooked
rocket {
id
name
}
mission {
name
missionPatch
}
}

query LaunchDetails($launchId: ID!) {
launch(id: $launchId) {
isInCart @client
clientObject @client {
...ClientObjectFragment
}
site
rocket {
type
}
...LaunchTile
}
}
`),
["client"]
)
)
).toMatchInlineSnapshot(`
"fragment LaunchTile on Launch {
__typename
id
isBooked
rocket {
id
name
}
mission {
name
missionPatch
}
}

query LaunchDetails($launchId: ID!) {
launch(id: $launchId) {
site
rocket {
type
}
...LaunchTile
}
}
"
`);
});

it("should recursively remove fragments that become unused when antecendant directives are removed", () => {
expect(
print(
removeDirectiveAnnotatedFields(
parse(`
fragment One on Node {
...Two
user {
friends {
name
...Two @client
}
}
}
fragment Two on Node {
id
}

query {
me {
...One
}
}
`),
["client"]
)
)
).toMatchInlineSnapshot(`
"fragment One on Node {
...Two
user {
friends {
name
}
}
}

fragment Two on Node {
id
}

{
me {
...One
}
}
"
`);
});

it("should remove fragment spreads from @client fragment definitions", () => {
expect(
print(
removeDirectiveAnnotatedFields(
parse(`
fragment One on Node @client {
...Two
}

fragment Two on Node {
id
}

query {
me {
name
...One
}
}
`),
["client"]
)
)
).toMatchInlineSnapshot(`
"{
me {
name
}
}
"
`);
});

it("should remove all operations that have no selection set after fragments are removed", () => {
expect(
print(
removeDirectiveAnnotatedFields(
parse(`
fragment One on Node @client {
...Two
}

fragment Two on Node {
id
}

{
name
me {
...One
}
}
`),
["client"]
)
)
).toMatchInlineSnapshot(`
"{
name
}
"
`);
});

it("should not remove fragment definitions that weren't removed by `removeDirectiveAnnotatedFields`", () => {
expect(
print(
removeDirectiveAnnotatedFields(
parse(`
fragment One on Node {
id
}

{
me {
name
}
}
`),
["client"]
)
)
).toMatchInlineSnapshot(`
"fragment One on Node {
id
}

{
me {
name
}
}
"
`);
});
});
Loading