The first essential thing to a GraphQL server is the schemas to define the type system of your application. You define
Types, Queries, Mutations, etc in a schema. Schema files must be stored in the
src/graphql/schemas directory of your
You need to load them at runtime, so they will be transpiled to their JS object notation and pass them to the
main Silvie bootstrap function. It must be assigned to an object property named
schemas like this:
A schema file is file with
.graphql extension. Entities in schema files need to be defined with the
GraphQL Type Language.
You can either create a
.gql file by your self and start writing everything from scratch, or
create a schema using the Silvie CLI.
This will result into a file named
book.gql in the
src/graphql/schemas directory with a
Book type and two queries
to fetch a single book and all books.
Note that if you are building your system on GraphQL, You can create schemas and resolvers when you are creating entities. For example, the following command will create a Book model, a books migration, a book schema and a book resolver.
A GraphQL type will be identified with the
type keyword followed by the type name. The properties will be specified in
a pair of curly braces.
GraphQL comes with a few basic data types (according to official docs) which you can use without any configuration:
- Int: Signed 32-bit integer
- Float: Signed double-precision floating-point value
- String: Signed UTF-8 Character sequence
- ID: An scalar type representing a unique identifier. It
Also, Silvie has a couple of useful built-in types in case you need to use them in your application. Keep in mind that
you need to enable
JSON types if you want to use them. GraphQL Configuration
tells your more about enabling these types.
- Query: A wrapper to contain all your queries
- Mutation: A wrapper to contain all your mutations
- Upload: Represents a Upload value
- File: Represents a File value
- JSON: Represents any valid JSON value
- JSONObject: Represents a JSON Object value
An example type would be like this:
A GraphQL query should be defined in the
Query type. GraphQL allows you to add methods and properties to your types by
extending them. This will help you split your GraphQL schemas into separate files. Silvie implemented an empty Query
type behind the scenes. So you just need to extend the query type and add your query definitions.
The following query will take no parameters and returns an array of
Users as the result:
The following query will take a
key to search for and returns an array of
SearchResult as the result, which is a
Mutations are another kind of queries that are meant to be used for manipulating the data on the server. They should be
defined in the
Mutation type. Silvie also creates an empty
Mutation type when it is initializing the GraphQL server
to let you add your mutations by extending the type.
This is an example of a mutation that takes user credentials and returns the login results as a
JSONObject in the