Skip to content

Commit 9f7efdd

Browse files
committed
Add a schema
1 parent 21fb521 commit 9f7efdd

File tree

2 files changed

+105
-19
lines changed

2 files changed

+105
-19
lines changed

src/components/index-page/how-it-works/interactive-editor.tsx

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from "react"
2-
import { graphql, GraphQLSchema } from "graphql"
2+
import { graphql } from "graphql"
33

44
import { QueryEditor } from "@/components/interactive-code-block/query-editor"
55
import { ResultViewer } from "@/components/interactive-code-block/result-viewer"
@@ -9,22 +9,9 @@ import { CodeBlockLabel } from "@/components/pre/code-block-label"
99

1010
import { HowItWorksListItem } from "./how-it-works-list-item"
1111
import { PlayButton } from "./play-button"
12+
import { projectsSchema as schema, INITIAL_QUERY_TEXT, INITIAL_RESULTS_TEXT } from "./schema"
1213

13-
const INITIAL_QUERY_TEXT = `{
14-
project(name: "GraphQL") {
15-
tagline
16-
}
17-
}`
18-
19-
const INITIAL_RESULTS_TEXT = `{
20-
"project": {
21-
"tagline": "A query language for APIs"
22-
}
23-
}`
2414

25-
const schema = new GraphQLSchema({
26-
types: [],
27-
})
2815

2916
export default function InteractiveEditor() {
3017
const [query, setQuery] = useState(INITIAL_QUERY_TEXT)
@@ -38,9 +25,9 @@ export default function InteractiveEditor() {
3825
const queryID = editorQueryId.current
3926
try {
4027
const result = await graphql({
41-
schema: schema,
28+
schema,
4229
source: query,
43-
variableValues: {},
30+
variableValues: JSON.parse(variables || "{}"),
4431
})
4532

4633
let resultToSerialize: any = result
@@ -75,8 +62,8 @@ export default function InteractiveEditor() {
7562
<QueryEditor
7663
value={query}
7764
schema={schema}
78-
onEdit={() => {
79-
setQuery(query)
65+
onEdit={newQuery => {
66+
setQuery(newQuery)
8067
runQuery({ manual: false })
8168
}}
8269
runQuery={() => {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import {
2+
GraphQLSchema,
3+
GraphQLObjectType,
4+
GraphQLString,
5+
GraphQLNonNull,
6+
GraphQLList,
7+
} from "graphql"
8+
9+
const PROJECT_NAME = "GraphQL"
10+
const PROJECT_TAGLINE = "A query language for APIs"
11+
const PROJECT_DESCRIPTION =
12+
"GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data."
13+
const PROJECT_WEBSITE = "https://graphql.org"
14+
15+
export const INITIAL_QUERY_TEXT = `{
16+
project(name: "${PROJECT_NAME}") {
17+
tagline
18+
}
19+
}`
20+
21+
export const INITIAL_RESULTS_TEXT = `{
22+
"project": {
23+
"tagline": "${PROJECT_TAGLINE}"
24+
}
25+
}`
26+
27+
interface Project {
28+
name: string
29+
tagline: string
30+
description: string
31+
website: string
32+
}
33+
34+
const projects: Project[] = [
35+
{
36+
name: PROJECT_NAME,
37+
tagline: PROJECT_TAGLINE,
38+
description: PROJECT_DESCRIPTION,
39+
website: PROJECT_WEBSITE,
40+
},
41+
{
42+
name: "GraphiQL",
43+
tagline: "Ecosystem for building browser & IDE tools.",
44+
description:
45+
"GraphiQL is the reference implementation of this monorepo, GraphQL IDE, an official project under the GraphQL Foundation. The code uses the permissive MIT license.",
46+
website: "https://github.com/graphql/graphiql",
47+
},
48+
]
49+
50+
const ProjectType = new GraphQLObjectType<Project>({
51+
name: "Project",
52+
fields: {
53+
name: {
54+
type: GraphQLString,
55+
description: "The name of the project",
56+
},
57+
tagline: {
58+
type: GraphQLString,
59+
description: "A short description of what the project does",
60+
},
61+
description: {
62+
type: GraphQLString,
63+
description: "A detailed description of the project",
64+
},
65+
website: {
66+
type: GraphQLString,
67+
description: "The project website URL",
68+
},
69+
},
70+
})
71+
72+
const QueryType = new GraphQLObjectType({
73+
name: "Query",
74+
fields: {
75+
project: {
76+
type: ProjectType,
77+
args: {
78+
name: {
79+
type: new GraphQLNonNull(GraphQLString),
80+
description: "The name of the project to retrieve",
81+
},
82+
},
83+
resolve: (_, args) => {
84+
return projects.find(
85+
project => project.name.toLowerCase() === args.name.toLowerCase(),
86+
)
87+
},
88+
},
89+
projects: {
90+
type: new GraphQLList(ProjectType),
91+
description: "Get all available projects",
92+
resolve: () => projects,
93+
},
94+
},
95+
})
96+
97+
export const projectsSchema = new GraphQLSchema({
98+
query: QueryType,
99+
})

0 commit comments

Comments
 (0)