DEV Community

Cover image for GraphQL CRUD Java: Overview
Anisha Mohanty
Anisha Mohanty

Posted on

GraphQL CRUD Java: Overview

What is GraphQL CRUD Java?

GraphQLCRUD java is an extensive framework built using the GraphQLCRUD spec which provides a specification for common operation on top of GraphQL. It provides numerous options for users to access and modify their data from any data sources they are connected to.

Common Features

  • Check specifications for patterns and pick only those that will really work for your specific database or business requirements.
  • It provides canonical versions and other variants giving you an overview of different approaches used in most schemas.
  • Focus on your business logic and data and generate GraphQL CRUD compliant schemas in any language of your choice.
  • It abstracts from large GraphQL vendors giving you flexibility, and the ability to migrate without rebuilding your clients and resolves.
  • It focuses on productivity by giving developers powerful query capabilities that are not specific to any GraphQL solution providers.

CRUD methods

GraphQL CRUD java defines different CRUD capabilities that represent various operations that can be executed on a set of objects:

  • Create: create an object
  • Update: update a specific object's properties
  • Delete: delete a specific object by its ID
  • Get: get a specific object by its ID
  • Find: find multiple objects

Capabilities

GraphQL CRUD java defines different capabilities that developers can enable to modify what queries can be made against the service. Examples of these capabilities include:

  • Pagination: Ability to paginate content
  • Filtering: Ability to perform filtering on specific fields
  • Countability: Ability to count the total number of objects
  • Consistency: Ability to verify whether a write operation is overriding data

How it works?

Take any database, the GraphQL CRUD java engine can automatically generate a GraphQL schema and process GraphQL queries and mutations. Here’s what the engine does under the hood.

  • The GraphQL CRUD java engine automatically generates GraphQL schema components and creates relationships between them.
  • It defines a GraphQL type definition for your database.
  • Provides Query with filter,order_by, and page arguments.
  • Provides Insert mutations with the input argument that supports insert operation.
  • Provides Update mutation with filter and input argument that supports bulk updates.
  • Provides Delete mutation with a filter argument that supports bulk deletes.
Example: Fetch list of all accounts
#Input
{
  accounts {
    account_id
    status
    ssn
    }
}
Enter fullscreen mode Exit fullscreen mode
#Output
{
  "data": {
    "accounts": [
      {
        "account_id": "19980001",
        "status": "Personal",
        "ssn": "CST01002"
      },
      {
        "account_id": "19980002",
        "status": "Personal",
        "ssn": "CST01002"
      },
      {
        "account_id": "19980003",
        "status": "Personal",
        "ssn": "CST01003"
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode
Example: Fetch an account using the primary key
#Input
{
  account(account_id: 19980001) {
    account_id
    ssn
    status
    type
  }
}
Enter fullscreen mode Exit fullscreen mode
#Output
{
  "data": {
    "account": {
      "account_id": "19980001",
      "ssn": "CST01002",
      "status": "Personal",
      "type": "Active"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
Example: Fetch a customer whose name is James
#Input
{
  customers(filter: {
    firstname: {
      eq: "James"
    }
  }) {
    firstname
    lastname
    ssn
    phone
  }
}
Enter fullscreen mode Exit fullscreen mode
#Output
{
  "data": {
    "customers": [
      {
        "firstname": "James",
        "lastname": "Drew",
        "ssn": "CST01036",
        "phone": "(216)555-6523"
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode
Example: Fetch 2 customers from the list of all customers, starting from first
#Input
{
  customers(page: {
    limit: 2
  }) {
    firstname
    lastname
    ssn
    phone
  }
}
Enter fullscreen mode Exit fullscreen mode
#Output
{
  "data": {
    "customers": [
      {
        "firstname": "John",
        "lastname": "Doe",
        "ssn": "CST01002  ",
        "phone": "(646)555-1776"
      },
      {
        "firstname": "Bob",
        "lastname": "Smith",
        "ssn": "CST01003  ",
        "phone": "(412)555-4327"
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode
Example: Fetch 2 customers from the list of all customers, starting from the 2nd customer
#Input
{
  customers(filter: {
    firstname: {
      startsWith: "J"
    }
  }, page: {
    limit: 2,
    offset: 2
  }) {
    firstname
    lastname
    ssn
    phone
  }
}
Enter fullscreen mode Exit fullscreen mode
#Output
{
  "data": {
    "customers": [
      {
        "firstname": "Jack",
        "lastname": "Corby",
        "ssn": "CST01015  ",
        "phone": "(469)555-8023"
      },
      {
        "firstname": "James",
        "lastname": "Drew",
        "ssn": "CST01036  ",
        "phone": "(216)555-6523"
      }
    ]
  }
}

Enter fullscreen mode Exit fullscreen mode
Example: Insert a new customer object and return the inserted customer object in the response
#Input
mutation {
  createCustomer(input: {
    firstname: "James",
    lastname: "Corners",
    ssn: "CST00989"
  }) {
    firstname
    lastname
    ssn
  }
}
Enter fullscreen mode Exit fullscreen mode
#Output
{
  "data": {
    "createCustomer": {
      "firstname": "James",
      "lastname": "Corners",
      "ssn": "CST00989"
    }
  }
} 
Enter fullscreen mode Exit fullscreen mode
Example: Update a customer with the name Bob and return the updated customer object in the response
#Input
mutation {
  updateCustomer (input: {
    firstname: "Andrew",
    lastname: "Moore"
  }, filter: {
    firstname: {
      eq: "Bob"
    }
  }) {
    firstname
    lastname
    ssn
  }
}
Enter fullscreen mode Exit fullscreen mode
#Output
{
  "data": {
    "updateCustomer": {
      "firstname": "Andrew",
      "lastname": "Moore",
      "ssn": "CST01003"
    }
  }
} 
Enter fullscreen mode Exit fullscreen mode
Example: Delete a customer where name is Andrew
#Input
mutation {
  deleteCustomer(filter: {
    firstname: {
      eq: "Andrew"
    }
  }) {
    firstname
    lastname
    ssn
    phone
  }
}
Enter fullscreen mode Exit fullscreen mode
#Output
{
  "data": {
    "deleteCustomer": {
      "firstname": "Andrew",
      "lastname": "Moore",
      "ssn": "CST01003"
    }
  }
} 

Enter fullscreen mode Exit fullscreen mode

You can explore the entire schema and the available queries using the GraphiQL interface. You can expand the framework as per your needs to make it more robust.

To get a detailed understanding or to contribute check out the GitHub repository here and if you like the work drop a ⭐ in the repository.

Happy Reading! ❀️

Top comments (6)

Collapse
 
renu944 profile image
renu944

Hi ,
I have a requirement to generate dynamic query of GraphQL from schema. I need to create dynamic schema and its query by passing table name from the database. I am able to create schema dynamically. Can you help me to create dynamic queries on it?

Thanks

Collapse
 
christianblos profile image
Info Comment hidden by post author - thread only accessible via permalink
Christian Blos

Unfortunately there is no documentation in the repository. It's hard to get a big picture of this library and how to use it.

What about authentication? Is there any protection, so a user can only access own data? Or is it just a layer on top of the database that exposes everything?

Collapse
 
christianblos profile image
Christian Blos

The first example looks wrong. You ask for two fields, account_id and status but the output contains ssn as well.

Collapse
 
anisha profile image
Anisha Mohanty

Ah, Fixed it.

Collapse
 
ama profile image
Adrian Matei

The Github repo link is broken

Collapse
 
anisha profile image
Anisha Mohanty

Thanks for pointing out. Fixed it.😁

Some comments have been hidden by the post's author - find out more