Every Relay application needs a GraphQL server and a GraphQL server needs a schema. The schema defines all the types of data in your application.
Typically you would have to write your schema as code and set up the server yourself, but with Reindex you can just define the types, push them to Reindex and your GraphQL server is ready for use.
Your Reindex app has a minimal schema by default. Go to your project folder and fetch it:
reindex schema-fetch
Reindex Schema is a JSON file. We use JSON5 as our parser,
so it’s much nicer to manually edit than normal JSON, as it supports comments,
unquoted property names and trailing commas. Let’s take a look the
ReindexSchema.json
that we just fetched. It includes the initial Reindex
schema for your app:
[
{
name: "User",
kind: "OBJECT",
interfaces: [
"Node"
],
fields: [
{
name: "id",
type: "ID",
nonNull: true,
unique: true
}
]
},
]
Every Reindex app has a type named User
, which represents a user of the app.
It implements the Node
interface, which means it is an object with an ID
and it can be fetched using the node
root field. The field id
must be
defined as nonNull
. unique
means that all values of the id
field must be
unique within User
nodes.
Let’s add a new type Todo
, which we will use to represent todo items in our
app. Replace the contents of ReindexSchema.json
with this new schema:
[
{
name: "User",
kind: "OBJECT",
interfaces: [
"Node"
],
fields: [
{
name: "id",
type: "ID",
nonNull: true,
unique: true
}
]
},
{
name: "Todo",
kind: "OBJECT",
interfaces: [
"Node"
],
fields: [
{
name: "id",
type: "ID",
nonNull: true,
unique: true
},
{
name: "text",
type: "String"
},
{
name: "complete",
type: "Boolean"
}
]
}
]
Then you can push this schema to your Reindex app:
reindex schema-push
Congratulations! You now have a GraphQL server. Next, let’s make some GraphQL queries.
GraphiQL is an interactive console for working with GraphQL servers. You can
quickly open GraphiQL by using the graphiql
command of Reindex CLI:
reindex graphiql
Now make some queries to your brand new GraphQL server. Let’s add a todo. Type this mutation in GraphiQL and run it:
mutation AddTodo {
createTodo(input: { text: "Do the dishes", complete: false }) {
changedTodo {
text
complete
}
}
}
Add a few todos. You can then see all of them by running this query:
{
viewer {
allTodos {
nodes {
text
}
}
}
}
We’ve created a schema and inserted some data with GraphiQL. In the next section we’ll make Relay get this data for us.