Please note, this is a STATIC archive of website www.javatpoint.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
Javatpoint Logo
Javatpoint Logo

GraphQL Schema

The GraphQL schema is the core building block of GraphQL server implementation. The GraphQL server uses a GraphQL schema to define and describe the shape of your data graph. The GraphQL schema establishes the hierarchy of types with fields and also describes functionality available to the client applications. In simple words, you can say that "A schema is used to define a collection of types and the relationships between these types." It is also used to specify the exact queries and mutations which are available for clients to execute against your data graph.

GraphQL schema can be created by using any programming language, and we can also build an interface around it.

Here, we shall describe how to create a GraphQL schema for our GraphQL server and also the fundamental building blocks of the schema.

What shall we use?

  • An Apollo server to execute GraphQL queries
  • A makeExecutableSchema function in graphql-tools to bind schema and resolvers

makeExecutableSchema Function Syntax

The makeExecutableSchema function is used to take a single argument {} of Object type. Following is the syntax of makeExecutableSchema Function:

Parameter Explanation

  • typeDefs: This argument is used to represent a GraphQL query as a UTF-8 string. (Required argument)
  • Resolvers: This argument has functions that are used to handle the query. (Optional argument)
  • logger: This argument is used to print errors to the server console. (Optional argument)
  • parseOptions: This argument is used to allow customization of parse when specifying typeDefs as a string. (Optional argument)
  • allowUndefinedInResolve: This argument is set true by default. When you set it to false, it makes your resolve functions to throw errors if they return undefined.
  • resolverValidationOptions: This argument is used to accept an object with Boolean properties. (Optional argument)
  • inheritResolversFromInterfaces: This argument is used to accept a Boolean argument to check resolvers object inheritance. (Optional argument)

Supported types of GraphQL Schema

A type definition in a GraphQL schema must belong to one of the following categories:

  • Scalar types
  • Object types
  • The Query type
  • The Mutation type
  • Input types

The performance and usage of the every declaration of the above fields is individually monitored by Apollo Studio. It also provides you the data that informs about the changes to your graph.

The Schema Definition Language

In GraphQL specification, there is a human-readable schema definition language (SDL) that is used to define your schema and store it as a string.

For example:

Let's see a simple schema example that defines two object types: Company and Owner:

In the above schema example, every Company has an owner, and every owner has a list of companies. When we define these relationships in a unified schema, we make client developers able to see the available data is and also request a specific subset of that data by using a single optimized query.

Note: The schema is not responsible for defining where data comes from or how data is stored. It is only responsible for the hierarchy implementation of types.

Example

Let's create a simple example to see how the schema works. In this example, we shall create a schema to query a list of employees from the server. The employee data will be stored in a flat-file, and we shall read data from the flat file by using a node module called notarealdb.

Download and Install Required Dependencies

First, create a folder named "schema-app" and navigate to the folder by using node.js command prompt.

Then, Create a package.json file and install the dependencies:

Now, install all dependencies by using the following command:


GraphQL Schema

Create a Flat File Database in Data Folder

Here, we shall create flat files to store and retrieve data. First, create a folder named data and add two files employees.json and companies.json.

companies.json file:

employees.json file:

Create a Data Access Layer

Let's create a datastore to store the data folder content. Here, we use collection variables, employees, and companies.

Create a file db.js within the "schema-app" folder:

Create a Schema

Now, create a schema.graphql file in the "schema-app" folder and use the following code:

Create Resolver

Create a file resolvers.js within the "schema-app" folder and use the following code:

Create Server.js and Configure GraphQL

Run the Server

Run the server and use the following query in the editor:

Output:

{
  "data": {
    "hello": "Welcome to JavaTpoint...."
    "employees": [    
   {
      "id": "E1001",
      "firstName":"Ajeet",
      "lastName":"Kumar",
   },   
   {
      "id": "E1002",
      "firstName":"Mohan",
      "lastName":"Bhargav",
   },
   {
      "id": "E1003",
      "firstName":"Rashmi",
      "lastName":"Bansal",   
   }
   ]
   }
   }






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA