In this section you learn how to create a database and GraphQL service in Fauna. You learn about the resources that Fauna automatically generates from your GraphQL schema. Finally, you use the GraphQL playground to invoke a mutation and a query.
Navigate to the Fauna Dashboard in your browser and create a new database by choosing Create Database. Note the options shown in the following screenshot.
GraphQL_Workshop
. Database names cannot contain spaces or the % character and cannot be events, sets, self, documents, or _ (the underscore character). You can change the name of your database at any time.Create a GraphQL schema with a single type Store as shown in the following example.
type Store {
name: String!
email: String!
paymentMethods: [String]
categories: [String]
}
Save your schema as schema-1a.graphql or download a copy using the following link.
Navigate to the GraphQL tab in the Fauna dashboard, choose Import Schema, and select the GraphQL schema you created or downloaded.
The GraphQL playground displays after your schema upload completes. Choose the Docs tab in the GraphQL playground and notice that Fauna automatically generates the following queries and mutations based on your schema.
Navigate to the Collections tab in the Fauna dashboard and notice that Fauna also generates a Store collection for you based on the Store type you define in your schema. Your Store collection should be empty.
Your GraphQL backend is ready to store and retrieve data! Return to the GraphQL tab and create a new store by running the following mutation in the playground.
mutation {
createStore(data: {
name: "Fauna Labs"
email: "owner@fauna-labs.com"
paymentMethods: ["Fauna bucks", "Credit Card"]
categories: ["Software and Internet"]
}) {
_id
name
email
paymentMethods
categories
}
}
{
"data": {
"createStore": {
"_id": "316700907109614160",
"name": "Fauna Labs",
"email": "owner@fauna-labs.com",
"paymentMethods": [
"Fauna bucks",
"Credit Card"
],
"categories": [
"Software and Internet"
]
}
}
}
Return to the Collections tab, choose the Store collection, and expand the new store document. Copy the id to use in the next step. The id is the string enclosed in quotes highlighted in the following screenshot.
Next, use a generated query to find your new store by its id. Return to the GraphQL tab and paste the following query into your GraphQL playground, replacing the
query {
findStoreByID(id: "<store-id>") {
_id
name
email
paymentMethods
categories
}
}
{
"data": {
"findStoreByID": {
"_id": "<store-id>",
"name": "Fauna Labs",
"email": "owner@fauna-labs.com",
"paymentMethods": [
"Fauna bucks",
"Credit Card"
],
"categories": [
"Software and Internet"
]
}
}
}
In this section you learned how to create a database and upload a GraphQL schema using Fauna. You explored the mutations, queries, and collections that Fauna automatically generates from a GraphQL schema. Finally, you used the GraphQL playground to invoke a mutation and a query.
In the next section, you learn about various data access patterns and how to create custom resolvers in Fauna.