DEV Community

Cover image for GraphQL - TypeDefs
Shubham Tiwari
Shubham Tiwari

Posted on • Updated on

GraphQL - TypeDefs

Hello Everyone, in this part of the GraphQL series, I will discuss what are typeDefs in Graphs.

TypeDefs -

The typeDefs is a string or a schema language document that describes the data structure of your GraphQL API. It defines the available types, their fields, and the relationships between them. This includes defining object types, scalar types, input types, interfaces, unions, and enumerations.

Creating a schema using typeDef -

  • Create another folder inside your root folder named "schema", then inside "schema" folder, create a file called "type-defs.js" and paste this code inside that file
const { gql } = require("apollo-server")

const typeDefs = gql`

    type User {
        id: ID! # uniquely identifies but similar to strings
        name: String!
        age: Int!
        isEmployee: Boolean!
        role: Role!
        friends: [User] # It will have a list of Users with same data as User schema
    }

    input CreateUser {
        name: String!
        age: Int!
        isEmployee: Boolean = true
        role: Role!
    }

    input UpdateUser {
        id: ID!
        name: String!
        age: Int!
        isEmployee: Boolean = true
        role: Role!
    }

    input DeleteUser {
        id: ID!
    }

    type Query {
        users: [User!]! 
        user(id: ID!): User! 
        userByName(name: String!): User! 
    }

    type Mutation {
        createUser(newUser:CreateUser!):User
        updateUser(updatedUser:UpdateUser!):User
        deleteUser(delUser:DeleteUser!):User
    }


    # Enums to define some fix values for a field
    enum Role {
        WebDeveloper
        Tester
        SoftwareEngineer
    }

`;

module.exports = { typeDefs };
Enter fullscreen mode Exit fullscreen mode
  • Here we have created the schema for User Data with the datatypes required for every field.
  • "!", this symbol after datatype means that the value is required and cannot be null.
  • "friends" property is assigned the User Schema, which means friends will be an Array of Users with the same properties except for the "friends" itself.
  • Then we defined the query using "Query", which will create a schema for the handler function, there we have 3 schemas, 1 for all users, 1 for finding users by id, and the other for finding users by name, return type of these handlers functions will "User".
  • We also have Mutation Type which is used to create a schema for Mutation handlers whose type is created using "input" type.
  • Enum is used to provide a set of possible values for a field

So this is how we can create Schema for our GraphQL API, in the next part I will discuss resolvers, where we implement the logic for our API Calls.

CREDITS - https://www.youtube.com/results?search_query=pedrotech

THANK YOU FOR CHECKING THIS POST
You can contact me on -
Instagram - https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - shubhmtiwri00@gmail.com

^^You can help me with some donation at the link below Thank you👇👇 ^^
☕ --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/website-components-you-should-know-25nm

https://dev.to/shubhamtiwari909/smooth-scrolling-with-js-n56

https://dev.to/shubhamtiwari909/swiperjs-3802

https://dev.to/shubhamtiwari909/custom-tabs-with-sass-and-javascript-4dej

Top comments (0)