Skip to content

Commit 2fdd5fd

Browse files
move remaining doc explorer component to @graphiql/react (#2587)
1 parent beab030 commit 2fdd5fd

File tree

16 files changed

+125
-193
lines changed

16 files changed

+125
-193
lines changed

.changeset/five-pillows-fail.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Add new components:
66
- UI components (`Dropdown`, `Spinner`, `UnStyledButton` and lots of icon components)
77
- Editor components (`QueryEditor`, `VariableEditor`, `HeaderEditor` and `ResponseEditor`)
88
- Toolbar components (`ExecuteButton` and `ToolbarButton`)
9-
- Docs components (`Argument`, `DefaultValue`, `Directive`, `FieldLink`, `Search` and `TypeLink`)
9+
- Docs components (`Argument`, `DefaultValue`, `Directive`, `DocExplorer`, `FieldDocumentation`, `FieldLink`, `SchemaDocumentation`, `Search`, `TypeDocumentation` and `TypeLink`)
1010
- `History` component

.changeset/red-zoos-divide.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'graphiql': major
3+
---
4+
5+
BREAKING: The following exports of the `graphiql` package have been removed:
6+
- `DocExplorer`: Now exported from `@graphiql/react` as `DocExplorer`

packages/graphiql/src/components/__tests__/DocExplorer.spec.tsx renamed to packages/graphiql-react/src/explorer/components/__tests__/doc-explorer.spec.tsx

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
/**
2-
* Copyright (c) 2021 GraphQL Contributors.
3-
*
4-
* This source code is licensed under the MIT license found in the
5-
* LICENSE file in the root directory of this source tree.
6-
*/
7-
import React from 'react';
81
import { render } from '@testing-library/react';
9-
import {
10-
ExplorerContextProvider,
11-
SchemaContext,
12-
SchemaContextType,
13-
} from '@graphiql/react';
142

15-
import { DocExplorer } from '../DocExplorer';
16-
import { ExampleSchema } from './ExampleSchema';
3+
import { SchemaContext, SchemaContextType } from '../../../schema';
4+
import { ExplorerContextProvider } from '../../context';
5+
import { DocExplorer } from '../doc-explorer';
6+
import { ExampleSchema } from './example-schema';
177

188
const defaultSchemaContext: SchemaContextType = {
199
fetchError: null,

packages/graphiql/src/components/__tests__/ExampleSchema.ts renamed to packages/graphiql-react/src/explorer/components/__tests__/example-schema.ts

+8-34
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import {
1111
export const ExampleInterface = new GraphQLInterfaceType({
1212
name: 'exampleInterface',
1313
fields: {
14-
name: {
15-
name: 'nameField',
16-
type: GraphQLString,
17-
},
14+
name: { type: GraphQLString },
1815
},
1916
});
2017

@@ -31,29 +28,17 @@ export const ExampleUnionType1 = new GraphQLObjectType({
3128
name: 'Union_Type_1',
3229
interfaces: [ExampleInterface],
3330
fields: {
34-
name: {
35-
name: 'nameField',
36-
type: GraphQLString,
37-
},
38-
enum: {
39-
name: 'enumField',
40-
type: ExampleEnum,
41-
},
31+
name: { type: GraphQLString },
32+
enum: { type: ExampleEnum },
4233
},
4334
});
4435

4536
export const ExampleUnionType2 = new GraphQLObjectType({
4637
name: 'Union_Type_2',
4738
interfaces: [ExampleInterface],
4839
fields: {
49-
name: {
50-
name: 'nameField',
51-
type: GraphQLString,
52-
},
53-
string: {
54-
name: 'stringField',
55-
type: GraphQLString,
56-
},
40+
name: { type: GraphQLString },
41+
string: { type: GraphQLString },
5742
},
5843
});
5944

@@ -66,26 +51,15 @@ export const ExampleQuery = new GraphQLObjectType({
6651
name: 'Query',
6752
description: 'Query description\n Second line',
6853
fields: {
69-
string: {
70-
name: 'exampleString',
71-
type: GraphQLString,
72-
},
73-
union: {
74-
name: 'exampleUnion',
75-
type: ExampleUnion,
76-
},
54+
string: { type: GraphQLString },
55+
union: { type: ExampleUnion },
7756
fieldWithArgs: {
78-
name: 'exampleWithArgs',
7957
type: GraphQLString,
8058
args: {
81-
stringArg: {
82-
name: 'exampleStringArg',
83-
type: GraphQLString,
84-
},
59+
stringArg: { type: GraphQLString },
8560
},
8661
},
8762
deprecatedField: {
88-
name: 'booleanField',
8963
type: GraphQLBoolean,
9064
deprecationReason: 'example deprecation reason',
9165
},

packages/graphiql/src/components/DocExplorer/__tests__/FieldDoc.spec.tsx renamed to packages/graphiql-react/src/explorer/components/__tests__/field-documentation.spec.tsx

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
1-
/**
2-
* Copyright (c) 2021 GraphQL Contributors.
3-
*
4-
* This source code is licensed under the MIT license found in the
5-
* LICENSE file in the root directory of this source tree.
6-
*/
7-
8-
import { ExplorerContext, ExplorerFieldDef } from '@graphiql/react';
91
import {
102
// @ts-expect-error
113
fireEvent,
124
render,
135
} from '@testing-library/react';
146
import { GraphQLString, GraphQLObjectType, Kind } from 'graphql';
15-
import React from 'react';
7+
import { ExplorerContext, ExplorerFieldDef } from '../../context';
168

17-
import FieldDoc from '../FieldDoc';
9+
import { FieldDocumentation } from '../field-documentation';
1810
import { mockExplorerContextValue } from './test-utils';
1911

2012
const exampleObject = new GraphQLObjectType({
@@ -65,22 +57,24 @@ const exampleObject = new GraphQLObjectType({
6557
},
6658
});
6759

68-
function FieldDocWithContext(props: { field: ExplorerFieldDef }) {
60+
function FieldDocumentationWithContext(props: { field: ExplorerFieldDef }) {
6961
return (
7062
<ExplorerContext.Provider
7163
value={mockExplorerContextValue({
7264
name: props.field.name,
7365
def: props.field,
7466
})}>
75-
<FieldDoc />
67+
<FieldDocumentation />
7668
</ExplorerContext.Provider>
7769
);
7870
}
7971

8072
describe('FieldDoc', () => {
8173
it('should render a simple string field', () => {
8274
const { container } = render(
83-
<FieldDocWithContext field={exampleObject.getFields().string} />,
75+
<FieldDocumentationWithContext
76+
field={exampleObject.getFields().string}
77+
/>,
8478
);
8579
expect(
8680
container.querySelector('.graphiql-markdown-description'),
@@ -95,7 +89,9 @@ describe('FieldDoc', () => {
9589

9690
it('should re-render on field change', () => {
9791
const { container, rerender } = render(
98-
<FieldDocWithContext field={exampleObject.getFields().string} />,
92+
<FieldDocumentationWithContext
93+
field={exampleObject.getFields().string}
94+
/>,
9995
);
10096
expect(
10197
container.querySelector('.graphiql-markdown-description'),
@@ -108,7 +104,9 @@ describe('FieldDoc', () => {
108104
).not.toBeInTheDocument();
109105

110106
rerender(
111-
<FieldDocWithContext field={exampleObject.getFields().stringWithArgs} />,
107+
<FieldDocumentationWithContext
108+
field={exampleObject.getFields().stringWithArgs}
109+
/>,
112110
);
113111
expect(
114112
container.querySelector('.graphiql-doc-explorer-type-name'),
@@ -120,7 +118,9 @@ describe('FieldDoc', () => {
120118

121119
it('should render a string field with arguments', () => {
122120
const { container } = render(
123-
<FieldDocWithContext field={exampleObject.getFields().stringWithArgs} />,
121+
<FieldDocumentationWithContext
122+
field={exampleObject.getFields().stringWithArgs}
123+
/>,
124124
);
125125
expect(
126126
container.querySelector('.graphiql-doc-explorer-type-name'),
@@ -149,7 +149,7 @@ describe('FieldDoc', () => {
149149

150150
it('should render a string field with directives', () => {
151151
const { container } = render(
152-
<FieldDocWithContext
152+
<FieldDocumentationWithContext
153153
field={exampleObject.getFields().stringWithDirective}
154154
/>,
155155
);

packages/graphiql/src/components/DocExplorer/__tests__/TypeDoc.spec.tsx renamed to packages/graphiql-react/src/explorer/components/__tests__/type-documentation.spec.tsx

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
/**
2-
* Copyright (c) 2021 GraphQL Contributors.
3-
*
4-
* This source code is licensed under the MIT license found in the
5-
* LICENSE file in the root directory of this source tree.
6-
*/
7-
8-
import { ExplorerContext, SchemaContext } from '@graphiql/react';
91
import {
102
// @ts-expect-error
113
fireEvent,
124
render,
135
} from '@testing-library/react';
146
import { GraphQLNamedType } from 'graphql';
15-
import React from 'react';
167

8+
import { SchemaContext } from '../../../schema';
9+
import { ExplorerContext } from '../../context';
10+
import { TypeDocumentation } from '../type-documentation';
1711
import {
18-
ExampleSchema,
12+
ExampleEnum,
1913
ExampleQuery,
14+
ExampleSchema,
2015
ExampleUnion,
21-
ExampleEnum,
22-
} from '../../__tests__/ExampleSchema';
23-
import TypeDoc from '../TypeDoc';
16+
} from './example-schema';
2417
import { mockExplorerContextValue, unwrapType } from './test-utils';
2518

26-
function TypeDocWithContext(props: { type: GraphQLNamedType }) {
19+
function TypeDocumentationWithContext(props: { type: GraphQLNamedType }) {
2720
return (
2821
<SchemaContext.Provider
2922
value={{
@@ -40,15 +33,17 @@ function TypeDocWithContext(props: { type: GraphQLNamedType }) {
4033
name: unwrapType(props.type).name,
4134
def: props.type,
4235
})}>
43-
<TypeDoc />
36+
<TypeDocumentation />
4437
</ExplorerContext.Provider>
4538
</SchemaContext.Provider>
4639
);
4740
}
4841

4942
describe('TypeDoc', () => {
5043
it('renders a top-level query object type', () => {
51-
const { container } = render(<TypeDocWithContext type={ExampleQuery} />);
44+
const { container } = render(
45+
<TypeDocumentationWithContext type={ExampleQuery} />,
46+
);
5247
const description = container.querySelectorAll(
5348
'.graphiql-markdown-description',
5449
);
@@ -66,7 +61,9 @@ describe('TypeDoc', () => {
6661
});
6762

6863
it('renders deprecated fields when you click to see them', () => {
69-
const { container } = render(<TypeDocWithContext type={ExampleQuery} />);
64+
const { container } = render(
65+
<TypeDocumentationWithContext type={ExampleQuery} />,
66+
);
7067
let cats = container.querySelectorAll('.doc-category-item');
7168
expect(cats).toHaveLength(3);
7269

@@ -83,14 +80,18 @@ describe('TypeDoc', () => {
8380
});
8481

8582
it('renders a Union type', () => {
86-
const { container } = render(<TypeDocWithContext type={ExampleUnion} />);
83+
const { container } = render(
84+
<TypeDocumentationWithContext type={ExampleUnion} />,
85+
);
8786
expect(container.querySelector('.doc-category-title')).toHaveTextContent(
8887
'possible types',
8988
);
9089
});
9190

9291
it('renders an Enum type', () => {
93-
const { container } = render(<TypeDocWithContext type={ExampleEnum} />);
92+
const { container } = render(
93+
<TypeDocumentationWithContext type={ExampleEnum} />,
94+
);
9495
expect(container.querySelector('.doc-category-title')).toHaveTextContent(
9596
'values',
9697
);
@@ -101,7 +102,7 @@ describe('TypeDoc', () => {
101102

102103
it('shows deprecated enum values on click', () => {
103104
const { getByText, container } = render(
104-
<TypeDocWithContext type={ExampleEnum} />,
105+
<TypeDocumentationWithContext type={ExampleEnum} />,
105106
);
106107
const showBtn = getByText('Show deprecated values...');
107108
expect(showBtn).toBeInTheDocument();

packages/graphiql/src/components/DocExplorer.tsx renamed to packages/graphiql-react/src/explorer/components/doc-explorer.tsx

+13-27
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
/**
2-
* Copyright (c) 2021 GraphQL Contributors.
3-
*
4-
* This source code is licensed under the MIT license found in the
5-
* LICENSE file in the root directory of this source tree.
6-
*/
7-
8-
import {
9-
Search,
10-
Spinner,
11-
useExplorerContext,
12-
useSchemaContext,
13-
} from '@graphiql/react';
141
import { GraphQLSchema, isType } from 'graphql';
15-
import React, { ReactNode } from 'react';
2+
import { ReactNode } from 'react';
163

17-
import FieldDoc from './DocExplorer/FieldDoc';
18-
import SchemaDoc from './DocExplorer/SchemaDoc';
19-
import TypeDoc from './DocExplorer/TypeDoc';
4+
import { useSchemaContext } from '../../schema';
5+
import { Spinner } from '../../ui';
6+
import { useExplorerContext } from '../context';
7+
import { FieldDocumentation } from './field-documentation';
8+
import { SchemaDocumentation } from './schema-documentation';
9+
import { Search } from './search';
10+
import { TypeDocumentation } from './type-documentation';
2011

2112
type DocExplorerProps = {
2213
onClose?(): void;
@@ -30,21 +21,16 @@ type DocExplorerProps = {
3021
schema?: GraphQLSchema | null;
3122
};
3223

33-
/**
34-
* DocExplorer
35-
*
36-
* Shows documentations for GraphQL definitions from the schema.
37-
*
38-
*/
3924
export function DocExplorer(props: DocExplorerProps) {
4025
const {
4126
fetchError,
4227
isFetching,
4328
schema: schemaFromContext,
4429
validationErrors,
45-
} = useSchemaContext({ nonNull: true });
30+
} = useSchemaContext({ nonNull: true, caller: DocExplorer });
4631
const { explorerNavStack, hide, pop } = useExplorerContext({
4732
nonNull: true,
33+
caller: DocExplorer,
4834
});
4935

5036
const navItem = explorerNavStack[explorerNavStack.length - 1];
@@ -69,11 +55,11 @@ export function DocExplorer(props: DocExplorerProps) {
6955
// an error during introspection.
7056
content = <div className="error-container">No Schema Available</div>;
7157
} else if (explorerNavStack.length === 1) {
72-
content = <SchemaDoc />;
58+
content = <SchemaDocumentation />;
7359
} else if (isType(navItem.def)) {
74-
content = <TypeDoc />;
60+
content = <TypeDocumentation />;
7561
} else if (navItem.def) {
76-
content = <FieldDoc />;
62+
content = <FieldDocumentation />;
7763
}
7864

7965
let prevName;

0 commit comments

Comments
 (0)