Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove error tooltip for populate directives #175

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/panel/context/Request.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ describe("on debug message", () => {
"@skip",
"@include",
"@deprecated",
"@populate",
],
"_implementations": Object {},
"_mutationType": null,
Expand All @@ -209,7 +210,7 @@ describe("on debug message", () => {
"__TypeKind": "__TypeKind",
},
"astNode": undefined,
"extensionASTNodes": undefined,
"extensionASTNodes": Array [],
"extensions": undefined,
},
"setQuery": [Function],
Expand Down
5 changes: 4 additions & 1 deletion src/panel/context/Request.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React, {
import { visit, buildClientSchema, DocumentNode } from "graphql";
import { GraphQLSchema, getIntrospectionQuery } from "graphql";
import { useDevtoolsContext } from "./Devtools";
import { appendPopulateDirective } from "./schema-transforms";

interface RequestContextValue {
query?: string;
Expand Down Expand Up @@ -61,7 +62,9 @@ export const RequestProvider: FC = ({ children }) => {
debugEvent.type === "update" &&
isIntrospectionQuery(debugEvent.operation.query)
) {
setSchema(buildClientSchema(debugEvent.data.value));
setSchema(
appendPopulateDirective(buildClientSchema(debugEvent.data.value))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply the transform on incoming schemas

);
return;
}

Expand Down
67 changes: 67 additions & 0 deletions src/panel/context/schema-transforms.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { buildSchema, GraphQLSchema, printSchema } from "graphql";
import { appendPopulateDirective } from "./schema-transforms";

describe("#appendPopulateDirective", () => {
let schema: GraphQLSchema;
beforeEach(() => {
schema = buildSchema(`
type Post {
id: ID
title: String!
content: String!
}
type Query {
posts: [Post]
}
type Mutation {
createPost(title: String!, content: String!): Post!
}
`);
});

it("should create an additional populate directive", () => {
const modifiedSchema = appendPopulateDirective(schema);
expect(printSchema(modifiedSchema)).toMatchInlineSnapshot(`
"directive @populate on FIELD

type Mutation {
createPost(title: String!, content: String!): Post!
}

type Post {
id: ID
title: String!
content: String!
}

type Query {
posts: [Post]
}
"
`);
});

it("should be idempotent", () => {
const modifiedSchema = appendPopulateDirective(
appendPopulateDirective(schema)
);
expect(printSchema(modifiedSchema)).toMatchInlineSnapshot(`
"directive @populate on FIELD

type Mutation {
createPost(title: String!, content: String!): Post!
}

type Post {
id: ID
title: String!
content: String!
}

type Query {
posts: [Post]
}
"
`);
});
});
21 changes: 21 additions & 0 deletions src/panel/context/schema-transforms.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { GraphQLSchema, extendSchema, parse } from "graphql";

const populateSchema = parse(`
directive @populate on FIELD
`);

export const appendPopulateDirective = (
schema: GraphQLSchema
): GraphQLSchema => {
try {
return extendSchema(schema, populateSchema);
} catch (err) {
if (
err.message.startsWith(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If populate exists already extendSchema will throw. In this case we want to return the schema as is

'Directive "populate" already exists in the schema'
)
)
return schema;
throw err;
}
};
3 changes: 2 additions & 1 deletion src/panel/pages/request/Request.fixture.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FC, ContextType, useState, useMemo } from "react";
import { buildSchema } from "graphql";
import { RequestContext } from "../../context";
import { appendPopulateDirective } from "../../context/schema-transforms";
import { Request } from "./Request";

export const schema = buildSchema(`
Expand Down Expand Up @@ -31,7 +32,7 @@ const RequestProviderMock: FC<Partial<ContextType<typeof RequestContext>>> = ({
execute: () => null as any,
fetching: false,
response: undefined,
schema,
schema: appendPopulateDirective(schema),
...value,
}),
[value]
Expand Down
Loading