GraphQL CRUD Java: Overview 
Tags: graphql, java, showdev, beginners
Published: 2020-11-05 14:54:31

## 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
    }
}
```
```
#Output
{
  "data": {
    "accounts": [
      {
        "account_id": "19980001",
        "status": "Personal",
        "ssn": "CST01002"
      },
      {
        "account_id": "19980002",
        "status": "Personal",
        "ssn": "CST01002"
      },
      {
        "account_id": "19980003",
        "status": "Personal",
        "ssn": "CST01003"
      }
    ]
  }
}
```

##### Example: Fetch an account using the primary key

```
#Input
{
  account(account_id: 19980001) {
    account_id
    ssn
    status
    type
  }
}
```
```
#Output
{
  "data": {
    "account": {
      "account_id": "19980001",
      "ssn": "CST01002",
      "status": "Personal",
      "type": "Active"
    }
  }
}
```
##### Example: Fetch a customer whose name is James

```
#Input
{
  customers(filter: {
    firstname: {
      eq: "James"
    }
  }) {
    firstname
    lastname
    ssn
    phone
  }
}
```
```
#Output
{
  "data": {
    "customers": [
      {
        "firstname": "James",
        "lastname": "Drew",
        "ssn": "CST01036",
        "phone": "(216)555-6523"
      }
    ]
  }
}
```
##### Example: Fetch 2 customers from the list of all customers, starting from first
```
#Input
{
  customers(page: {
    limit: 2
  }) {
    firstname
    lastname
    ssn
    phone
  }
}
```
```
#Output
{
  "data": {
    "customers": [
      {
        "firstname": "John",
        "lastname": "Doe",
        "ssn": "CST01002  ",
        "phone": "(646)555-1776"
      },
      {
        "firstname": "Bob",
        "lastname": "Smith",
        "ssn": "CST01003  ",
        "phone": "(412)555-4327"
      }
    ]
  }
}
```

##### 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
  }
}
```
```
#Output
{
  "data": {
    "customers": [
      {
        "firstname": "Jack",
        "lastname": "Corby",
        "ssn": "CST01015  ",
        "phone": "(469)555-8023"
      },
      {
        "firstname": "James",
        "lastname": "Drew",
        "ssn": "CST01036  ",
        "phone": "(216)555-6523"
      }
    ]
  }
}

```
##### 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
  }
}
```
```
#Output
{
  "data": {
    "createCustomer": {
      "firstname": "James",
      "lastname": "Corners",
      "ssn": "CST00989"
    }
  }
} 
```

##### 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
  }
}
```
```
#Output
{
  "data": {
    "updateCustomer": {
      "firstname": "Andrew",
      "lastname": "Moore",
      "ssn": "CST01003"
    }
  }
} 
```
##### Example: Delete a customer where name is Andrew
``` 
#Input
mutation {
  deleteCustomer(filter: {
    firstname: {
      eq: "Andrew"
    }
  }) {
    firstname
    lastname
    ssn
    phone
  }
}
```
```
#Output
{
  "data": {
    "deleteCustomer": {
      "firstname": "Andrew",
      "lastname": "Moore",
      "ssn": "CST01003"
    }
  }
} 

```
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](https://github.com/graphqlcrud/graphqlcrud-java) and if you like the work drop a :star: in the repository.

Happy Reading! :heart: