Skip to content

Commit e709818

Browse files
author
Paul Korzhyk
authored
Add user types to the bulk export (#233)
1 parent fad87ed commit e709818

File tree

3 files changed

+86
-93
lines changed

3 files changed

+86
-93
lines changed

client/src/components/schema/Schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ export default class Schema extends React.Component {
197197
<SchemaRawModeModal
198198
key={modalKey}
199199
schema={schema}
200+
types={types}
200201
executeQuery={this.executeSchemaQuery}
201202
onAfterUpdate={this.handleAfterUpdate}
202203
onCancel={this.handleCloseModal}

client/src/components/schema/SchemaRawModeModal.js

Lines changed: 70 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,104 +12,92 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import React from "react";
15+
import React, { useState } from "react";
1616
import Modal from "react-bootstrap/Modal";
1717
import Button from "react-bootstrap/Button";
1818

1919
import Editor from "../../containers/Editor";
2020
import { getRawSchema } from "../../lib/dgraph-syntax";
2121

22-
export default class SchemaRawModeModal extends React.Component {
23-
constructor(props) {
24-
super(props);
22+
export default function SchemaRawModeModal({
23+
executeQuery,
24+
onAfterUpdate,
25+
onCancel,
26+
onDropData,
27+
schema,
28+
types,
29+
}) {
30+
const [value, setValue] = useState(getRawSchema(schema, types));
31+
const [editorKey, setEditorKey] = useState(1);
32+
const [errorMsg, setErrorMsg] = useState(null);
33+
const [updating, setUpdating] = useState(false);
2534

26-
this.state = {
27-
value: getRawSchema(this.props.schema),
28-
editorKey: 1,
29-
};
30-
31-
this.handleUpdate = this.handleUpdate.bind(this);
32-
}
33-
34-
async handleUpdate() {
35-
const { executeQuery, onAfterUpdate } = this.props;
36-
37-
this.setState({
38-
errorMsg: "",
39-
updating: true,
40-
});
35+
const handleUpdate = async () => {
36+
setUpdating(true);
37+
setErrorMsg(null);
4138

4239
try {
43-
await executeQuery(this.state.value + "\n", "alter", true);
40+
await executeQuery(value + "\n", "alter", true);
4441
onAfterUpdate();
4542
} catch (error) {
46-
this.setState({
47-
errorMsg: `Could not alter schema: ${error?.message}`,
48-
});
43+
setErrorMsg(`Could not alter schema: ${error?.message}`);
4944
} finally {
50-
this.setState({ updating: false });
45+
setUpdating(false);
5146
}
52-
}
53-
54-
handleResetClick = () => {
55-
this.setState({
56-
value: getRawSchema(this.props.schema),
57-
editorKey: this.state.editorKey + 1,
58-
errorMsg: null,
59-
});
6047
};
6148

62-
render() {
63-
const { onCancel, onDropData } = this.props;
64-
const { editorKey, updating, errorMsg } = this.state;
49+
const handleResetClick = () => {
50+
setValue(getRawSchema(schema, types));
51+
setEditorKey(editorKey + 1);
52+
setErrorMsg(null);
53+
};
6554

66-
return (
67-
<Modal show={true} size="lg" onHide={onCancel}>
68-
<Modal.Header closeButton>
69-
<Modal.Title>Edit Schema File</Modal.Title>
70-
</Modal.Header>
71-
<Modal.Body style={{ padding: 0 }}>
72-
<div style={{ border: "1px solid #e5e5e5" }}>
73-
<Editor
74-
key={editorKey}
75-
query={this.state.value}
76-
maxHeight={314}
77-
onUpdateQuery={value => this.setState({ value })}
78-
/>
79-
</div>
80-
{!errorMsg ? null : (
81-
<div className="alert alert-danger">{errorMsg}</div>
82-
)}
83-
</Modal.Body>
84-
<Modal.Footer style={{ justifyContent: "space-between" }}>
85-
<Button
86-
className="pull-left"
87-
variant="secondary"
88-
onClick={onCancel}
89-
>
90-
Cancel
91-
</Button>
92-
<Button
93-
variant="light"
94-
onClick={this.handleResetClick}
95-
disabled={updating}
96-
>
97-
{updating ? "Updating..." : "Refresh Schema"}
98-
</Button>
55+
return (
56+
<Modal show={true} size="lg" onHide={onCancel}>
57+
<Modal.Header closeButton>
58+
<Modal.Title>Edit Schema File</Modal.Title>
59+
</Modal.Header>
60+
<Modal.Body style={{ padding: 0 }}>
61+
<div style={{ border: "1px solid #e5e5e5" }}>
62+
<Editor
63+
key={editorKey}
64+
query={value}
65+
maxHeight={314}
66+
onUpdateQuery={value => setValue(value)}
67+
/>
68+
</div>
69+
{!errorMsg ? null : (
70+
<div className="alert alert-danger">{errorMsg}</div>
71+
)}
72+
</Modal.Body>
73+
<Modal.Footer style={{ justifyContent: "space-between" }}>
74+
<Button
75+
className="pull-left"
76+
variant="secondary"
77+
onClick={onCancel}
78+
>
79+
Cancel
80+
</Button>
81+
<Button
82+
variant="light"
83+
onClick={handleResetClick}
84+
disabled={updating}
85+
>
86+
{updating ? "Updating..." : "Refresh Schema"}
87+
</Button>
9988

100-
<Button variant="danger" size="sm" onClick={onDropData}>
101-
Drop Data
102-
</Button>
89+
<Button variant="danger" size="sm" onClick={onDropData}>
90+
Drop Data
91+
</Button>
10392

104-
<Button
105-
variant="primary"
106-
onClick={this.handleUpdate}
107-
disabled={updating}
108-
>
109-
{updating ? "Updating..." : "Apply Schema"}
110-
</Button>
111-
</Modal.Footer>
112-
</Modal>
113-
);
114-
}
93+
<Button
94+
variant="primary"
95+
onClick={handleUpdate}
96+
disabled={updating}
97+
>
98+
{updating ? "Updating..." : "Apply Schema"}
99+
</Button>
100+
</Modal.Footer>
101+
</Modal>
102+
);
115103
}

client/src/lib/dgraph-syntax.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export function getPredicateQuery(predicate) {
5151
return `<${predicate.predicate}>: ${getPredicateTypeString(predicate)} .`;
5252
}
5353

54+
export const isUserType = typeName =>
55+
(typeName || "").indexOf("dgraph.type.") !== 0 &&
56+
typeName !== "dgraph.graphql";
57+
5458
export const isUserPredicate = name =>
5559
[
5660
"_predicate_",
@@ -67,16 +71,16 @@ export const isUserPredicate = name =>
6771
export const isAclPredicate = name =>
6872
isUserPredicate(name) || name === "dgraph.type";
6973

70-
export function getRawSchema(schema) {
71-
schema = schema.filter(p => isUserPredicate(p.predicate));
72-
const schemaModified = schema.filter(
73-
p => p.type !== "uid" || p.count || p.reverse || p.list,
74-
);
75-
const schemaStandard = schema.filter(
76-
p => p.type === "uid" && !p.count && !p.reverse && !p.list,
77-
);
78-
const schemaStrings = [...schemaModified, ...schemaStandard].map(p =>
79-
getPredicateQuery(p),
74+
export function getRawSchema(schema, types = []) {
75+
const schemaStrings = schema
76+
.filter(p => isUserPredicate(p.predicate))
77+
.map(p => getPredicateQuery(p));
78+
79+
const typeDefs = types.map(t =>
80+
`
81+
type <${t.name}> {
82+
${t.fields.map(f => `\t${f.name}`).join("\n")}
83+
}`.trim(),
8084
);
81-
return schemaStrings.sort().join("\n");
85+
return [...schemaStrings.sort(), ...typeDefs].join("\n");
8286
}

0 commit comments

Comments
 (0)